我正在尝试使用matplotlib更新Tkinter画布中的双面板图。这是显示我当前理解的最小代码。主要问题是导航工具栏适用于初始图(y = sin(x),y = cos(x)),但是当我按下更新按钮进行更新时,它会失败。例如,如果我放大曲线,我不能使用主页按钮返回其原始状态。我一直在尝试不同的方式,但无济于事。我很感激任何人的建议。 我注意到的一个小问题是,如果我要杀死情节,我应该去菜单栏并选择python / quit Python,否则如果我只是点击绘图窗口左上角的X,终端会冻结(我必须杀死终端) 我使用的是Python 2.7.14和matplotlob 2.1.0。
from Tkinter import *
import Tkinter as tk
import ttk
from math import exp
import os # for loading files or exporting files
import tkFileDialog
##loading matplotlib modules
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.gridspec as gridspec
import numpy as np
top = tk.Tk()
top.title("Intermolecular PDFs")
top_frame = ttk.Frame(top, padding = (10, 10))
top_frame.pack()
fig = plt.figure(figsize=(10, 6), dpi=100) ##create a figure; modify the size here
x = np.linspace(0,1)
y = np.sin(x)
z = np.cos(x)
fig.add_subplot(211)
plt.title("Individual PDFs")
plt.xlabel(ur"r (\u00c5)", labelpad = 3, fontsize = 15)
plt.ylabel(ur"PDF, G (\u00c5$^{-2})$", labelpad = 10, fontsize = 15)
plt.plot(x,y, "r-", lw=2)
plt.xticks(fontsize = 11)
plt.yticks(fontsize = 11)
fig.add_subplot(212)
plt.title("Difference PDFs")
plt.xlabel(ur"r (\u00c5)", labelpad = 3, fontsize = 15)
plt.ylabel(ur"PDF, G (\u00c5$^{-2})$", labelpad = 10, fontsize = 15)
plt.plot(x,z,"g-", lw=2)
plt.xticks(fontsize = 11)
plt.yticks(fontsize = 11)
fig.tight_layout()
canvas = FigureCanvasTkAgg(fig, master = top_frame)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
#self.canvas.draw()
toolbar = NavigationToolbar2TkAgg(canvas, top_frame)
#self.toolbar.pack()
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
def update():
fig.clf()
new_x = np.linspace(1,100)
new_y = new_x**2
new_z = new_x**3
fig.add_subplot(211)
plt.title("Individual PDFs")
plt.xlabel(ur"r (\u00c5)", labelpad = 3, fontsize = 15)
plt.ylabel(ur"PDF, G (\u00c5$^{-2})$", labelpad = 10, fontsize = 15)
plt.plot(new_x,new_y, "r-", lw=2)
plt.xticks(fontsize = 11)
plt.yticks(fontsize = 11)
fig.add_subplot(212)
plt.title("Difference PDFs")
plt.xlabel(ur"r (\u00c5)", labelpad = 3, fontsize = 15)
plt.ylabel(ur"PDF, G (\u00c5$^{-2})$", labelpad = 10, fontsize = 15)
plt.plot(new_x,new_z,"g-", lw=2)
plt.xticks(fontsize = 11)
plt.yticks(fontsize = 11)
fig.tight_layout()
canvas.show()
ttk.Button(top_frame, text = "update",command = update).pack()
top.mainloop()
答案 0 :(得分:1)
主要问题是主页按钮不知道按下时应该引用哪个状态。它所引用的原始状态甚至不再存在,因为该数字在此期间已被清除。解决方法是致电
toolbar.update()
除其他外,它将为按钮创建一个新的主页状态,以便在按下时恢复。
代码还有一些其他小问题:
以下是一个干净的版本,其中包含以上所有内容:
import Tkinter as tk
import ttk
##loading matplotlib modules
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import numpy as np
top = tk.Tk()
top.title("Intermolecular PDFs")
top_frame = ttk.Frame(top, padding = (10, 10))
top_frame.pack()
matplotlib.rcParams["xtick.labelsize"] = 11
matplotlib.rcParams["ytick.labelsize"] = 11
fig = Figure(figsize=(10, 6), dpi=100) ##create a figure; modify the size here
x = np.linspace(0,1)
y = np.sin(x)
z = np.cos(x)
ax = fig.add_subplot(211)
ax.set_title("Individual PDFs")
ax.set_xlabel(ur"r (\u00c5)", labelpad = 3, fontsize = 15)
ax.set_ylabel(ur"PDF, G (\u00c5$^{-2})$", labelpad = 10, fontsize = 15)
line, = ax.plot(x,y, "r-", lw=2)
ax2 = fig.add_subplot(212)
ax2.set_title("Difference PDFs")
ax2.set_xlabel(ur"r (\u00c5)", labelpad = 3, fontsize = 15)
ax2.set_ylabel(ur"PDF, G (\u00c5$^{-2})$", labelpad = 10, fontsize = 15)
line2, = ax2.plot(x,z,"g-", lw=2)
canvas = FigureCanvasTkAgg(fig, master = top_frame)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
fig.tight_layout()
toolbar = NavigationToolbar2TkAgg(canvas, top_frame)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
def update():
new_x = np.linspace(1,100)
new_y = new_x**2
new_z = new_x**3
line.set_data(new_x,new_y)
line2.set_data(new_x,new_z)
ax.relim()
ax.autoscale()
ax2.relim()
ax2.autoscale()
fig.tight_layout()
canvas.draw_idle()
toolbar.update()
ttk.Button(top_frame, text = "update",command = update).pack()
top.mainloop()
注意:在较新版本的matplotlib中,您应使用NavigationToolbar2Tk
代替NavigationToolbar2TkAgg
。