[英]How can I plot figures from pandas series in different windows?
[英]How can I open two different pylab figures from the same button from a wxpython gui?
我是 python 新手,我有一个无法克服的严重问题。 我使用 wxpython 创建了一个 gui,它有两个文本字段和一个按钮。 当按下此按钮时,我调用一个函数,该函数调用另一个函数,该函数根据文本框中的输入创建一个饼图。 问题是,如果用户不关闭图形并在文本框中输入新值并再次按下按钮,程序将崩溃而不是显示第二个图形。 我试图在按下按钮时创建不同的线程结果是一样的。
进一步来说:
这是单击按钮时调用的函数:
def statistics_button(self,event):
t=threading.Thread(target=self.m_button21OnButtonClick,args=(event,))
t.start()
print t
def m_button21OnButtonClick( self, event ):
'''some code here'''
fig=statistics.mmr_dist(mmr1,mmr2)
show()
首先调用 statistics_button,然后调用 m_button21OnButtonClick。 statistics.mmr_dist 函数如下:
'''some code'''
fig=pylab.figure(tit,figsize=(8,8),frameon=False)
ax=pylab.axes([0.1, 0.1, 0.8, 0.8])
pita=pylab.pie(db.values(), labels=tuple(db.keys()), autopct='%1.1f%%', shadow=True)
ti="%s-%s\n Maximum Distance = %s m\n Minimum Distance = %s m" % (ddf1.label,ddf2.label,maxdist,mindist)
title(ti, bbox={'facecolor':'0.8', 'pad':5})
'''some code'''
return fig
到目前为止,我已经了解到 show() 命令会阻止函数 m_button21OnButtonClick 完成,因此除非图形关闭,否则在单击按钮时无法再次调用它。 但这就是我实现不同线程的原因。 虽然它似乎不起作用。
请参阅此页面以获取有关让 pylab 与 wxPython 一起工作的建议——您可能不应该真正尝试(请参阅下一段)。 问题是 pylab 使用的 Tkinter 与正在运行的 wxPython 副本不兼容。
最终,您应该将您的绘图嵌入 wxPython 。 这非常有效,并且无论如何都是更好的用户体验。
在导入 pylab 后尝试发出命令pylab.ion()
,看看是否可以显示多个图。 当需要在不关闭窗口的情况下重复显示更新图时,这一直是我的方法。
请注意,您需要为每个不同的绘图窗口创建新的图形和轴对象,否则绘图将覆盖旧绘图。
例如,以下代码可以为我生成两个具有不同绘图的窗口:
import pylab
pylab.ion()
fig1 = pylab.figure()
fig1.add_subplot(111).plot([1,2],[3,4])
pylab.draw()
fig2 = pylab.figure()
fig2.add_subplot(111).plot([5,6],[10,9])
pylab.draw()
添加
鉴于您的后续评论,这是一个使用show()
的新脚本,但每次调用pylab.draw()
时pylab.draw()
显示不同的图,并且会无限期地显示图窗口。 它使用简单的输入逻辑来决定何时关闭图形(因为使用show()
意味着 pylab 不会处理 windows x 按钮上的点击),但是作为另一个按钮或文本字段添加到您的 gui 应该很简单.
import numpy as np
import pylab
pylab.ion()
def get_fig(fig_num, some_data, some_labels):
fig = pylab.figure(fig_num,figsize=(8,8),frameon=False)
ax = fig.add_subplot(111)
ax.set_ylim([0.1,0.8]); ax.set_xlim([0.1, 0.8]);
ax.set_title("Quarterly Stapler Thefts")
ax.pie(some_data, labels=some_labels, autopct='%1.1f%%', shadow=True);
return fig
my_labels = ("You", "Me", "Some guy", "Bob")
# To ensure first plot is always made.
do_plot = 1; num_plots = 0;
while do_plot:
num_plots = num_plots + 1;
data = np.random.rand(1,4).tolist()[0]
fig = get_fig(num_plots,data,my_labels)
fig.canvas.draw()
pylab.draw()
print "Close any of the previous plots? If yes, enter its number, otherwise enter 0..."
close_plot = raw_input()
if int(close_plot) > 0:
pylab.close(int(close_plot))
print "Create another random plot? 1 for yes; 0 for no."
do_plot = raw_input();
# Don't allow plots to go over 10.
if num_plots > 10:
do_plot = 0
pylab.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.