0

I am using multiple matplotlib figures to visualize data in a tkinter application and so far that has been going great by using the explicit interface, i.e. by working on the axis objects.

Now I would like to save some figures to files. For this I can only find the pyplot function plt.savefig which is using the pyplot interface. Hence, since I have multiple figures I need to run

plt.figure(somefig)

to choose the figure I'd like to save. Unfortunately, this breaks in my usecase since I'm using FigureCanvasTkAgg to get the canvas for tkinter. As discribed in this bugreport, that call ruins the figure mangager so the call plt.figure(somefig) results in the error

ValueError: The passed figure is not managed by pyplot

So I guess my question is: Is there a way to save a figure witout using the implicit pyplot module?

obachtos
  • 977
  • 1
  • 12
  • 30
  • Have you tried all of the suggestions [here](https://stackoverflow.com/q/9886274/13629335) ? – Thingamabobs Dec 14 '22 at 09:37
  • Indeed I have, and for various reasons they are all not an option. And even if so, I feel like there should be a more direct way without going the detour over canvas and potentially even temporary files. After all, matplotlib does have the functionality already build in – obachtos Dec 14 '22 at 09:48
  • So you are asking for a matplot inherent solution? I think you should update your question then, to be more exact about what you looking for. – Thingamabobs Dec 14 '22 at 09:57
  • [This answer](https://stackoverflow.com/a/21464691/13629335) does not use `savefig`. Is that what you are looking for ? And [here](https://stackoverflow.com/q/5391026/13629335) are some alternatives. – Thingamabobs Dec 14 '22 at 10:02
  • Ah, yes, `print_png` from you second link was what I was looking for. That bypasses `savefig` and works directly on the figure object. If you would like to post that as an answer I'd be happy to accept that. – obachtos Dec 14 '22 at 10:50
  • I don't feel comfortable to copy and paste another ones answer and get rewarded for. I'd rather mark it as duplicate or would suggest you to post it yourself as answer with a link to the original author. But thank you for the offer, glad I could help. – Thingamabobs Dec 14 '22 at 11:01

0 Answers0