Save ERDS to image file instead of plotting it

  • MNE-Python version: 0.23.0
  • operating system: macOS 11.5.2 (20G95)

I’m looking at Compute and visualize ERDS maps — MNE 0.23.4 documentation and can only see the plot of an AverageTFR plot by calling tfr_ev.average().plot.

Is there a way to save it as an image file, using matplotlib.pyplot.imsave for example?


I’ve also noticed that the example is using tfr_ev.average() to plot but the plot() method already has a default mode="mean". Does it mean the plot is calculating the average and then the mean? Is that expected/correct?

Hi @henrique,
you can save any matplotlib figure in this way:

import matplotlib.pyplot as plt

plt.savefig(file_name, dpi=300)

This saves the currently open figure with file name that is defined by the file_name varaible and using dpi resolution 300 (you can change that to 150 and see how the saved image is smaller).

The mode argument in TFR plotting controls how baseline correction is done. Please take a look at the relevant entry in the docs and let us know if it is not clear enough and could be improved!

Hey @mmagnuski thanks for your reply!

Got it. I managed to save individual images for each channel with savefig.

I noticed the X and Y labels are hardcoded. Is there a way to get ONLY the graphed data, without markers, color bar, etc? Example:


Context

What I’m trying to achieve is to export all epochs as png images and also apply some custom filtering like choosing the type of events to export, and also export each channel as a single image or merging them into one (in this case all images would be stacked vertically, therefore, the y axis wouldn’t be a “valid chart axis” anymore but that’s expected for my use case)

My current approach is to get the tfr_ev.average().data shaped as (3 channels, F frequencies, S samples) and reshape it into a (3 * F, S) matrix to then plot that into a 2D image - the idea of this reshaping is equivalent to simply stacking each channel vertically on top of each other as I mentioned before.

I’m not sure if I’ll be able to achieve what I need with savefig. Do you think I should stick with my approach or switch to savefig?


About the average + mean, my question is more conceptual and about the correctness of that code. Is it really expected/wanted to apply the average to the data and ALSO the mean when plotting? Seems duplicated to me, but I don’t know if it is the expected use case…Also, why would one want to plot a mean by default? If one is calling plot without specifying anything I guess one would expect it to plot whatever is there - see, it’s called plot and not do_something_with_my_data_then_plot().

I apologize for my redundancy on this paragraph, I’m just trying to make myself as clear as possible :sweat_smile:

Thanks in advance!

Hi @henrique, sorry for the delay in responding.

Is there a way to get ONLY the graphed data, without markers, color bar, etc?

You can: a) remove the labels with standard matplotlib commands if you want (ax.set_ylabel("") where ax is the axis object); b) use colorbar=False when using TFR’s .plot() method (take a look at the link to the docs I pasted before); c) or you may access the data array (.data attribute or .get_data() method) and plot them your preferred way using standard matplotlib (for example using plt.imshow).

I’m not sure if I’ll be able to achieve what I need with savefig . Do you think I should stick with my approach or switch to savefig ?

plt.savefig is standard matplotlib way of saving images, there is no need to switch to something else if you create the figure your own way (not using mne).

About the average + mean, my question is more conceptual and about the correctness of that code.

.average() method averages epochs, while mode='mean' says how baseline correction should be done (personally, I prefer mode='percent'). These are different things. The baseline correction options are available in .plot() for convenience, but as the docs state, baseline correction is not applied by default (see the link I pasted before).