【问题标题】:NetworkX and Matplotlib - Axes errorNetworkX 和 Matplotlib - 轴错误
【发布时间】:2011-12-27 11:40:00
【问题描述】:

我在 Windows 上有 Networkx 1.6 和 Matplotlib 1.1.0 这是我的代码:

self.figure = Figure()
self.axes = self.figure.add_subplot(1,1,1)
self.canvas = FigureCanvas(self, -1, self.figure)
G = nx.Graph()
G.add_node(6)
pos = nx.spring_layout(G)
nx.draw(G, pos, ax = self.axes)

我得到了错误:

File "C:\Python27\lib\site-packages\matplotlib\axes.py, line 1374, in _sci
"Argument must be an image, collection, or ContourSet in this Axes"
ValueError: Argument must be an image, collection, or ContourSet in this Axes

有人知道怎么解决吗?

【问题讨论】:

  • networkx网站上有如何绘制networkx图的例子:networkx.lanl.gov/examples/drawing
  • 这只是代码的非功能部分。请发布一个功能性的、最少的代码示例,以便我们重现您的问题
  • 图形画布可能有问题。但 joaquin 是对的——我们需要查看重现问题的功能代码。

标签: python matplotlib networkx


【解决方案1】:

对于 matplotlib 1.0+,不要使用 Figure(),使用 pyplot.figure()。 Figure() 生成了一个 Figure 但没有在 pyplot 中的 figManager 中注册它,pyplot.figure() 可以。

在绘图函数中,他们通过调用 gcf() 来获取图形,gcf() 返回当前图形,如果不存在则创建一个新的

稍后调用 sci() 将尝试通过调用 gca() 来验证您应用于绘图函数的位置(集合)确实已经注册到轴上,但是由于您有一个 new 图,因此没有轴,它会引发异常。

我将其称为 matplotlib 错误。

我还没有阅读 matplotlib 的更改说明,可能在那里进行了描述。我是通过调试matplotlib代码发现的。

【讨论】:

    【解决方案2】:

    有没有办法在 tkinter 面板/主窗口中停靠/嵌入 pyplot.show() 命令?还是它总是在自己的窗口中弹出?

    def Embedded_Graph(Parent, G):
     Parent.figure = Figure()
     Parent.axe = Parent.figure.add_subplot(1,1,1)
     pos = nx.spring_layout(G)
     nx.draw(G, pos)
     pyplot.show()
    

    【讨论】:

      【解决方案3】:

      @Carel,我希望您找到了您要搜索的内容。如果没有,这里是如何在 Tkinter 画布上嵌入 networkx 图的示例:

      def embed_graph(G):
          pos = nx.spring_layout(G)
          nx.draw(G, pos)
          canvas = FigureCanvasTkAgg(plt.figure(1), master=self)
          canvas.show()
          canvas.get_tk_widget().pack(side="top")
      

      【讨论】:

        【解决方案4】:

        我不确定你想准确地画什么,但你得到了你的节点在做什么的图:

            self.figure = Figure()
            self.axe = self.figure.add_subplot(1,1,1)
            G = nx.Graph()
            G.add_node(6)
            pos = nx.spring_layout(G)
            nx.draw(G, pos)
            pyplot.show()
        

        因此,删除明显正确的 ax 参数可以绘制图形。我发现 post here 显示与 ax 参数相关的相同错误。它似乎在 mpl 0.99 上工作,但在 mpl 1.0 上没有

        【讨论】:

          猜你喜欢
          • 2017-09-02
          • 2019-11-21
          • 1970-01-01
          • 1970-01-01
          • 2014-07-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多