You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[ENH]: pre-set file name for GUI save dialog in rcParams #27774
there is no easy way to suggest the file base name, and it always defaults to practically useless "Figure_1" or so.
Rationale 1: I often process & plot a lot of scientific datasets with long names that encode a bunch of useful parameters. My scripts would therefore save me significant amount of time if e.g. for "abcd123.dat" they suggested to export into "abcd123.*".
I think it would be odd to have an rcParam for this because usually rcParams are things you want to have the same across multiple figures (and defaults are set in the matplotlibrc file). For file names you presumably want a different setting for each figure.
I'm do for fully understand you use case. Questions/comments:
You save a lot through the GUI save dialog? Even with a better suggested file name that's quite cumbersome. Can't you simply fig.savefig(filename) from your script?
IMHO a rcParam is not suitable for a figure filename. RcParams are global, but you'll want a figure-specific filename most of the time. It dosn't really help to be able to change the default filename to another constant like "my_plot.png"
you said
for "abcd123.dat" they suggested to export into "abcd123.*".
We cannot do this fully automatic. Matplotlib does not know where your data comes from. You would have to supply the name to Matplotlib. E.g. directly through savefig() or via the window title (see comment by @rcomer above), which gets used as filename suggestion.
So apparently there exist solutions for both my problems, although these have rather to be searched in the object model of Matplotlib and not in its RC params.
I must admit I don't find such a structure very user friendly, but let's consider this topic settled.
@timhoffm: you are right that for many files one should use non-interactive processing with savefig(), but I am developing cross-platform tools for a scientfic team and I want my colleagues to have full control over how and where the file gets exported.
I must admit I don't find such a structure very user friendly, but let's consider this topic settled.
This is through separation of concerns. The figure is a logical entity, the canvas is a backend specific rendering entity, e.g. a pixel buffer of your GUI framework. The manager is the controller if you want us to pop up a window Vito the canvas for you. So window information is naturally connected to the manager. But I agree, we could make this more discoverable. - and be it only by adding an example to https://matplotlib.org/devdocs/api/backend_bases_api.html#matplotlib.backend_bases.FigureManagerBase.set_window_title.
but I am developing cross-platform tools for a scientfic team and I want my colleagues to have full control over how and where the file gets exported.
If you anticipate, that your users need to save a lot, you may consider creating you own window in the gui framework of your choice, and embedding a Matplotlib figure into this. This would allow you to place a line edit with the suggested filename and a "save" button right next to the plot. (You'd wire the save button to call savefig with the name from the line edit.) That way, your users have full control, but saving with a default name is only a single click for them.
Thank you all. Today I finished the small application based on matplotlib's interactive figure https://github.com/FilipDominec/FZU14_Raman_maps, so anybody wanting some inspiration can simply copy the working code from there.
FWIW I've have had a def set_default_filename(fig, name) helper essentially implemented as in #27774 (comment) (with extra handling for the default directory and extension) since a long time ago in my utils.py.
defset_default_filename(fig, name):
"""Set the default filename when saving a figure to an image."""name=Path(name)
defget_default_filename():
old_default=Path(type(fig.canvas).get_default_filename(fig.canvas))
returnstr(old_default.parent/name.with_suffix(name.suffixorold_default.suffix))
fig.canvas.get_default_filename=get_default_filename
which, as mentioned above, specifically takes care of the default directory and extension as well (both when name specifies an extension and when it doesn't).
@anntzer since you seem to adapt the default filename as well: Would you say, it's reasonable to add a more direct way to set the default name? I.e. most simply add an attribute that get_default_filename() checks first before inferring a name.
Sure, I guess that would be nice, in the sense that at least two users could have made use of that? Ideally, the user should be able to set just a filename or an absolute path (overriding savefig.directory... I guess all relative paths should be relative to savefig.directory), and include an extension or not.
Activity
FilipDominec commentedon Feb 12, 2024
A related enhancement would be to add support for something like
as currently the window gets named as "Figure 1" and changing it requires further hacking. Both described settings should really be one-liners.
This both looks fairly easy to accomplish. With your approval, I can try making a PR.
[-][ENH]: [/-][+][ENH]: pre-set file name for GUI save dialog in rcParams[/+]FilipDominec commentedon Feb 12, 2024
Returning to the aforementioned Stack Overflow link, I found out that this one line will set suggested filename:
Yet it is not really systematic from programmer's point of view, so I still believe RC params should better do the job.
rcomer commentedon Feb 12, 2024
It looks like one option is
(tested with Matplotlib v3.8.2 and QtAgg).
I think it would be odd to have an rcParam for this because usually rcParams are things you want to have the same across multiple figures (and defaults are set in the matplotlibrc file). For file names you presumably want a different setting for each figure.
timhoffm commentedon Feb 12, 2024
I'm do for fully understand you use case. Questions/comments:
You save a lot through the GUI save dialog? Even with a better suggested file name that's quite cumbersome. Can't you simply
fig.savefig(filename)from your script?IMHO a rcParam is not suitable for a figure filename. RcParams are global, but you'll want a figure-specific filename most of the time. It dosn't really help to be able to change the default filename to another constant like "my_plot.png"
you said
We cannot do this fully automatic. Matplotlib does not know where your data comes from. You would have to supply the name to Matplotlib. E.g. directly through
savefig()or via the window title (see comment by @rcomer above), which gets used as filename suggestion.FilipDominec commentedon Feb 12, 2024
Thank you both for your quick answers!
So apparently there exist solutions for both my problems, although these have rather to be searched in the object model of Matplotlib and not in its RC params.
I must admit I don't find such a structure very user friendly, but let's consider this topic settled.
@timhoffm: you are right that for many files one should use non-interactive processing with
savefig(), but I am developing cross-platform tools for a scientfic team and I want my colleagues to have full control over how and where the file gets exported.timhoffm commentedon Feb 12, 2024
I must admit I don't find such a structure very user friendly, but let's consider this topic settled.
This is through separation of concerns. The figure is a logical entity, the canvas is a backend specific rendering entity, e.g. a pixel buffer of your GUI framework. The manager is the controller if you want us to pop up a window Vito the canvas for you. So window information is naturally connected to the manager. But I agree, we could make this more discoverable. - and be it only by adding an example to https://matplotlib.org/devdocs/api/backend_bases_api.html#matplotlib.backend_bases.FigureManagerBase.set_window_title.
If you anticipate, that your users need to save a lot, you may consider creating you own window in the gui framework of your choice, and embedding a Matplotlib figure into this. This would allow you to place a line edit with the suggested filename and a "save" button right next to the plot. (You'd wire the save button to call savefig with the name from the line edit.) That way, your users have full control, but saving with a default name is only a single click for them.
Better document the relation between figure and manager
FilipDominec commentedon Feb 12, 2024
Thank you all. Today I finished the small application based on matplotlib's interactive figure https://github.com/FilipDominec/FZU14_Raman_maps, so anybody wanting some inspiration can simply copy the working code from there.
anntzer commentedon Feb 12, 2024
FWIW I've have had a
def set_default_filename(fig, name)helper essentially implemented as in #27774 (comment) (with extra handling for the default directory and extension) since a long time ago in my utils.py.FilipDominec commentedon Feb 12, 2024
@anntzer I would be certainly interested in learning some tricks from your utils.py.
anntzer commentedon Feb 12, 2024
The actual implementation of my helper is
which, as mentioned above, specifically takes care of the default directory and extension as well (both when
namespecifies an extension and when it doesn't).timhoffm commentedon Feb 12, 2024
@anntzer since you seem to adapt the default filename as well: Would you say, it's reasonable to add a more direct way to set the default name? I.e. most simply add an attribute that
get_default_filename()checks first before inferring a name.anntzer commentedon Feb 12, 2024
Sure, I guess that would be nice, in the sense that at least two users could have made use of that? Ideally, the user should be able to set just a filename or an absolute path (overriding savefig.directory... I guess all relative paths should be relative to savefig.directory), and include an extension or not.
timhoffm commentedon Feb 13, 2024
Agreed. I assume an attribute
Canvas.default_filename(default None) and a bit of logic inCanvas.get_default_filename()should do the trick.Since you're a user, do you want to bring in a PR? 😀
anntzer commentedon Feb 13, 2024
no :) as usual, the suggested implementation above is freely donated to whoever wants to PR it.
timhoffm commentedon Feb 14, 2024
It's not important enough to me to pick this up, but if somebody is interested, #27774 (comment) is a reasonable extension and we would accept a PR.
Better document the relation between figure and manager