def plot(self, fig):
ax = fig.gca()
在Qt
MatPlotLib
窗口小部件上放置项目时,将调用此绘图函数。最后,所有内容都将由.draw()
更新。发生的问题如下:
调用外部函数,完成绘图,ax
必须是当前轴(图/轴不作为参数传递。因此我必须添加
pyplot.sca(ax)
一切都很好。不知怎的,也许因为更新到python(x,y)2.7.5.1(mpl是1.3.1),我得到这个错误Axes instance argument was not found in a figure.
只是在这种情况下,当我想要这个外部函数时(scipy树状图函数) )在预定义的轴上绘制。我试着跟着它
[Dbg]>>> fig
<matplotlib.figure.Figure object at 0x0A119A90>
[Dbg]>>> fig.gca()
<matplotlib.axes.AxesSubplot object at 0x0A119CD0>
然后进入子程序pyplot.sca(ax)
managers = _pylab_helpers.Gcf.get_all_fig_managers()
for m in managers:
if ax in m.canvas.figure.axes:
_pylab_helpers.Gcf.set_active(m)
m.canvas.figure.sca(ax)
return
raise ValueError("Axes instance argument was not found in a figure.")
列表似乎是空的
[Dbg]>>> managers
[]
也许有些人有想法,可能是什么问题,但远程诊断可能很困难。在我想要的图/轴上制作dendrogram
图的另一种方法也是有用的。
请同时提示我应该使用哪些内容来更新情节,MatplotlibWidget
,figure
和axes
使用draw
方法。
编辑:尝试创建一个MWE。是不是有人遇到同样的错误,或者谁能告诉我这里的问题是什么?
import sys
from matplotlibwidget import MatplotlibWidget
from matplotlib import pyplot
from PyQt4.QtGui import QMainWindow, QApplication
import scipy.cluster.hierarchy as hac
import numpy as np
class ApplicationWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.mplwidget = MatplotlibWidget(self, title='Example',
xlabel='Observation', ylabel='Distance', hold=True)
self.mplwidget.setFocus()
self.setCentralWidget(self.mplwidget)
def plotScree(self, Z, fig):
ax = fig.gca()
ax.plot(range(len(Z)), Z[::-1,2])
def plot(self, Z, fig):
ax = fig.gca()
pyplot.sca(ax)
hac.dendrogram(Z)
app = QApplication(sys.argv)
win = ApplicationWindow()
X = np.random.random(100).reshape(25, 4)
Z = hac.linkage(X)
#win.plotScree(Z, win.mplwidget.figure)
win.plot(Z, win.mplwidget.figure)
win.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
Python(x,y)中matplotlibwidget
的实现似乎被打破了。
我相信有问题的文件是this一个。如果您将该文件的第67行更改为self.figure = pypolt.figure(figsize=(width, height), dpi=dpi)
,那么您的代码将按您的意愿运行。我已经在下面包含了修改后代码的完整副本,因此您可以将其复制/粘贴到项目中并使用该matplotlibwidget而不是从python(x,y)中导入一个
问题似乎是直接实例化Figure对象,跳过整个图形管理器构造的负载,这就是提出错误的原因。我建议你用Python(x,y)提交bug报告并链接到这篇文章!
带有修改行的完整代码(请参阅上面的存储库链接以获取许可证)
from PyQt4.QtGui import QSizePolicy
from PyQt4.QtCore import QSize
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as Canvas
from matplotlib.figure import Figure
from matplotlib import rcParams
rcParams['font.size'] = 9
from matplotlib import pyplot
class MatplotlibWidget(Canvas):
"""
MatplotlibWidget inherits PyQt4.QtGui.QWidget
and matplotlib.backend_bases.FigureCanvasBase
Options: option_name (default_value)
-------
parent (None): parent widget
title (''): figure title
xlabel (''): X-axis label
ylabel (''): Y-axis label
xlim (None): X-axis limits ([min, max])
ylim (None): Y-axis limits ([min, max])
xscale ('linear'): X-axis scale
yscale ('linear'): Y-axis scale
width (4): width in inches
height (3): height in inches
dpi (100): resolution in dpi
hold (False): if False, figure will be cleared each time plot is called
Widget attributes:
-----------------
figure: instance of matplotlib.figure.Figure
axes: figure axes
Example:
-------
self.widget = MatplotlibWidget(self, yscale='log', hold=True)
from numpy import linspace
x = linspace(-10, 10)
self.widget.axes.plot(x, x**2)
self.wdiget.axes.plot(x, x**3)
"""
def __init__(self, parent=None, title='', xlabel='', ylabel='',
xlim=None, ylim=None, xscale='linear', yscale='linear',
width=4, height=3, dpi=100, hold=False):
self.figure = pyplot.figure(figsize=(width, height), dpi=dpi)
self.axes = self.figure.add_subplot(111)
self.axes.set_title(title)
self.axes.set_xlabel(xlabel)
self.axes.set_ylabel(ylabel)
if xscale is not None:
self.axes.set_xscale(xscale)
if yscale is not None:
self.axes.set_yscale(yscale)
if xlim is not None:
self.axes.set_xlim(*xlim)
if ylim is not None:
self.axes.set_ylim(*ylim)
self.axes.hold(hold)
Canvas.__init__(self, self.figure)
self.setParent(parent)
Canvas.setSizePolicy(self, QSizePolicy.Expanding, QSizePolicy.Expanding)
Canvas.updateGeometry(self)
def sizeHint(self):
w, h = self.get_width_height()
return QSize(w, h)
def minimumSizeHint(self):
return QSize(10, 10)