From my basic understanding about using matplotlib you store your desired plt in some ‘fig’ and then you can draw said ‘fig’ using the canvas.draw() operation. If this is the case then I shouldn’t have any problems but since I do, what exactly is going on and what’s the logic behind getting something on the canvas. Also, my end goal is to display this plot within a QtPy window. The results so far are that I can get the window and canvas to show but the canvas shows up empty. I have been looking at http://matplotlib.sourceforge.net/users/artists.html and feel like what I’m doing isn’t entirely wrong, but perhaps I’m overlooking some nuance. Here is the code I’m referencing:
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()
I don’t have any experience with matplotlib, but in looking at your code, I wonder if your use of
pyplotis correct? The way your code looks to me, is that you are using pyplot to generate data (which you dont keep the return values of), and then you plot it, yet I think its not actually operating on your axis instance.An example of how I have seen matplotlib being used can be found here: Segfault using matplotlib with PyQt .. where he is actually directly creating a PyQt4 FigureCanvas and plotting directly to his axis instance.
It seems that the
pyplot.plot()method can take a figure and axis parameter to tell it which instance to use. I wonder if its not using your axis, since I can’t see in your example how you create the axis itself. Take at look at the docs hereMy guess is you might try doing something like this:
Or, maybe confirm that you have createe
self.axisby usingself.axis = plt.axis(), or even try doing all your plotting directly with the axis instance?