pythonpython-3.xpandasmatplotlibasammdf

Convert MDF4 to Dataframe, plot it and save as image


I have logger measurement data as a mdf4 file. I need to visualize some of the signals and save it as an image. While the asammdf GUI can plot signals, I couldn't find a way to save the plot as an image. That's why I'm trying to convert the mdf4 object to pandas dataframe with the build-in asammdf function, plot it with matplotlib and save the plot as an image. Here is the code:

mdf_object = MDF(mdf_file_path)

signals_to_keep = ['v_spd', 'ax_xacc', 'ay_yacc']
mdf_to_plot = mdf_object.filter(signals_to_keep).cut(start=100, stop=240)
mdf_dataframe = mdf_to_plot.to_dataframe()
plt.figure(figsize=(20, 10), dpi=220)
mdf_dataframe.plot()
plt.savefig('foo.png', bbox_inches='tight')

I'm stuck at plotting. mdf_dataframe.plot() doesn't plot anything. Do I need to pass some arguments to the function or plot the signals one by one?

Or maybe i don't need to convert mdf4 to dataframe in the first place, because asammdf can save the plot as image?


Solution

  • mdf_object = MDF(mdf_file_path)
    
    signals_to_keep = ['v_spd', 'ax_xacc', 'ay_yacc']
    mdf_to_plot = mdf_object.filter(signals_to_keep).cut(start=100, stop=240)
    mdf_dataframe = mdf_to_plot.to_dataframe()
    ax = mdf_dataframe.plot(figsize=(20, 10), title='Test', grid=True)
    ax.figure.savefig(path)