簡體   English   中英

在圖中未找到軸實例參數

[英]Axes instance argument was not found in a figure

def plot(self, fig):
    ax = fig.gca()

當在Qt MatPlotLib小部件上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應當使用什么來更新繪圖, figureaxes具有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_())

Python(x,y)中的matplotlibwidget的實現似乎已被破壞。

我相信所討論的文件就是這個文件。 如果您將該文件的第67行更改為self.figure = pypolt.figure(figsize=(width, height), dpi=dpi)那么您的代碼將可以根據需要工作。 我在下面提供了修改后的代碼的完整副本,因此您只需將其復制/粘貼到您的項目中,然后使用該matplotlibwidget即可,而不是從python(x,y)導入

問題似乎是直接實例化Figure對象,跳過了Figure Manager構建的整個負載,這就是引發該錯誤的原因。 我建議您使用Python(x,y)提交錯誤報告,並鏈接到這篇文章!

完整代碼和修改后的行(請參閱上面的存儲庫鏈接以獲取許可證)

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM