Feature request: Method to copy an artist and / or return the arg values to instatiate an artist of the same type

I have written an extensive post on stackoverflow about this already, so I think it’s best to just link to it. Should I do it differently, please let me know.

Please do not just post links to SO, their content tends to change / be deleted over time and it makes the converastion hard to follow if you have to bounce between two sites.

A bit of context of why you are trying to do this would be very helpful in giving you an answer that is responsive in the right direction.

The short answer is that my figure canvas is ebedded in a PyQt UI and I would like to add a way to safely save images from the currently displayed figure without having to mess with it, therefore, I want to copy the figure and use the copy to make some potential changes and save it. I can elaborate on all the different angles I have tried to approach this problem, but I don’t know if that is helpful at this point.

I would like to add a way to safely save images from the currently displayed figure without having to mess with it

You can definitely safely save a figure that is being shown in a GUI without affecting the screen. There were some issues with flickering if you saved at a different size than it was displayed, but I think those have been fixed as of 3.2.

I want to copy the figure and use the copy to make some potential changes and save it.

I think this is the key point that you are not trying to just save your figure, but to apply arbitrary changes to the figure before you save it which is a very different from the first sentence.

If you control the code making the changes, presumable you could cache the current value before you change it and then re-set it on the way out.

If you want to reliably make an independent copy of the whole figure I believe that

import pickle
fig_copy = pickle.loads(pickle.dumps(fig))

should work. If you have a case where that either fails or is some how coupled to the orginal figure I think that is a bug and should be reported as such.

You can definitely safely save a figure that is being shown in a GUI without affecting the screen. There were some issues with flickering if you saved at a different size than it was displayed, but I think those have been fixed as of 3.2.

It’s interesting that you mention that. I’m not sure if we are thinking about the same thing, but that is definitely something I encountered and actually annoyed me. So my current workaround is to have the figure on a stacked widget, and when I want to save it, I get the current pixmap, switch to a different level of the stack and display the pixmap there, so for a user it feels like the figure is frozen. Once the figure is saved, I just switch back.

If you control the code making the changes, presumable you could cache the current value before you change it and then re-set it on the way out.

That is also my current approach (stackoverflow .com/a/62305937/5472354 , appearently, I cannot link to SO anymore). It’s just a bummer though, that changing ‘just’ the edgecolor e.g. works so differently for different artists. It’s for example unintuitive that spines come with a set_color method (which, as far as I undertand sets the edge color), but not with a get_color method.
My implementation is probably not very good, it would be awesome to have these kinds of context managers from the matplotlib library.

If you have a case where that either fails or is some how coupled to the orginal figure I think that is a bug and should be reported as such.

There kind of was one once, but if I’m not mistaken, you’ve already come across it, and it got resolved.