根据我对使用matplotlib的基本理解,您将所需的plt存储在某个'fig'中,然后可以使用canvas.draw()操作绘制所述'fig'。如果是这种情况,那么我不应该有任何问题,但是因为我这样做了,到底发生了什么以及在画布上获得某些东西的逻辑是什么。另外,我的最终目标是在QtPy窗口中显示此图。迄今为止的结果是,我可以让窗口和画布显示,但画布显示为空。我一直在寻找http://matplotlib.sourceforge.net/users/artists.html,觉得我所做的并不完全错误,但也许我忽略了一些细微差别。这里是代码,我引用:Can not get canvas to draw fig in matplotlib
def drawThis(self):
self.axes.clear()
self.axes.grid(self.grid_cb.isChecked())
self.fig = plt.figure(figsize=(11,7),dpi=self.dpi)
file = fileList[selFile]
valid = [sColumn]
matrix = np.loadtxt(file, skiprows=1, usecols=valid)
colCount = np.loadtxt(file, dtype=object)
totalCols = colCount.shape[1]
kdeData = np.array(matrix)
dataRange = (Decimal(max(abs(kdeData)))/10).quantize(1, rounding=ROUND_UP) * 10
gkde = stats.gaussian_kde(kdeData)
ind = np.linspace(-int(dataRange), int(dataRange), len(kdeData) * sSamples)
kdepdf = gkde.evaluate(ind)
##plot histogram of sample
plt.hist(kdeData, len(kdeData), normed=1, alpha=0.20)
##plot data generating density
plt.plot(ind, stats.norm.pdf(ind), 'r', linewidth=0.8, label='DGP normal')
##plot estimated density
plt.plot(ind, kdepdf, 'g', linewidth=0.8, label='kde')
plt.title('KDE for '+ str(nameList[selFile]))
plt.legend()
self.fig.canvas.draw()
我想我误解了在我正在创建的窗口中实例化图的过程。我实际上使用了在示例中找到的一部分代码来创建我正在做的事情,但我一直将plt中的情节存储在不在“self.axes”上,这是实际显示在画布上的那个。在你提到我没有对我使用pyplot生成的数据做任何事情后,终于意识到这一点,谢谢。 – DamianJ 2012-03-08 05:08:15