我尝试了PyQt5教程和本站。
在这个站点中,有一个导入matplotlib的示例代码。
我尝试了这段代码,然后我就可以得到PyQt窗口了。在那个地方也是一样的。
但我收到了警告。
MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.warnings.warn(message, mplDeprecation, stacklevel=1)下面是与站点matplotlib相关的代码。
class PlotCanvas(FigureCanvas):
def __init__(self, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = fig.add_subplot(111)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,
QSizePolicy.Expanding,
QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
self.plot()
def plot(self):
data = [random.random() for i in range(25)]
ax = self.figure.add_subplot(111)
ax.plot(data, 'r-')
ax.set_title('PyQt Matplotlib Example')
self.draw()为什么会出现这种警告?
发布于 2018-10-28 10:38:37
我删除了ax = self.figure.add_subplot(111),
并将属性ax替换为self.axes。
这解决了警告!
发布于 2018-10-26 10:22:16
您正在创建两个轴实例:
self.axes = fig.add_subplot(111) 它是在创建类对象时创建的。第二个存在
ax = self.figure.add_subplot(111)它是在使用类对象调用plot方法时创建的。很可能是因为这个原因,你会收到这个警告。另外,我相信plot方法中的下面一行
self.figure.add_subplot(111)应该编写为(使用self.fig)
self.fig.add_subplot(111)https://stackoverflow.com/questions/53005496
复制相似问题