Matplotlib save button not working in embedded Tkinter window

Viewed 702

I can't seem to make the save button work in an embedded window. All other buttons have full functionality.

Here's a snippet of code:

pop = Tk()

fig, ax = plt.subplots()
ax.plot(root.x, root.y)
ax.set(xlabel='Wavenumber', ylabel='Intensity', title=os.path.basename(root.filename))
ax.grid()

root.plot_canvas = FigureCanvasTkAgg(fig, master=pop)
root.plot_canvas.draw()

toolbar = NavigationToolbar2Tk(root.plot_canvas, pop)
toolbar.update()
root.plot_canvas.get_tk_widget().pack(side=TOP, fill=Y)

pop.mainloop()
2 Answers

This is a bug in matplotlib 3.1.0.

It will be fixed in matplotlib 3.1.1, which is to appear very soon hopefully.

In the meantime you can revert to matplotlib 3.0.3.

matplotlib documentation is followed correctly and save button works fine, but probably you are working in a restricted folder. If that's not the case, matplotlib.pyplot.savefig function would save the figure:

from tkinter import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
import matplotlib.pyplot as plt
import numpy as np

def save():
    plt.savefig('plot.png')

pop = Tk()

fig, ax = plt.subplots()
ax.plot(np.arange(1,10,5), np.arange(1,10,5))

plot_canvas = FigureCanvasTkAgg(fig, master=pop)
plot_canvas.draw()

toolbar = NavigationToolbar2Tk(plot_canvas, pop)
toolbar.update()
plot_canvas.get_tk_widget().pack(side=TOP, fill=Y)

b = Button(pop, text="SAVE", bg="red", fg = 'white', command=save)
b.pack()

pop.mainloop()
Related