James Fowler
James Fowler

Reputation: 39

Matplotlib save button not working in embedded Tkinter window

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()

Upvotes: 2

Views: 938

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339795

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.

Upvotes: 1

Masoud
Masoud

Reputation: 1280

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()

Upvotes: 0

Related Questions