在初始调用后重新调用 matplotlib 图形



我有一个Tkinter GUI,它可以创建一个预算计算器并存储交易。GUI 上的一个按钮是对从交易数据创建图表的脚本的调用。

我第一次单击按钮时,图表打开正常。如果我尝试再次单击该按钮,则会出现一个空白数字。我相信这来自我最初的,

plt.show()

打电话给我画图表的地方。以前有人遇到过这样的问题吗?

图形用户界面代码:

ViewCharts = Button(win,text = 'View Spending Charts')
ViewCharts.grid(row = 21,column = 1)
def view_charts():
Graphs.plot_charts()
ViewCharts.configure(command = view_charts)

图表代码:

global fig
fig = plt.figure(figsize=(12, 9))
fig.subplots_adjust(hspace=.5)
global ax1
ax1 = fig.add_subplot(1,2,1)
global ax2
ax2 = fig.add_subplot(1,2,2)

def plot_charts():
category_bar_chart()
category_pie_chart()
fig = plt.show()

其中 category_bar_chart(( 和 category_pie_chart(( 只是将绘图添加到 ax1 和 ax2 中的函数。

任何帮助或建议将不胜感激!我正在Mac/PC上处理Python3,具体取决于一天中的时间。谢谢!

问题是当你这样做时,你正在重新定义figfig = plt.show()所以在第一次调用之后,你不再将绘图存储在任何地方。只需plt.show()即可。

最新更新