初学者关于Matplotlib OO Api的问题

时间:2015-11-16 11:40:26

标签: matplotlib

我是Matplotlib的新手,我正在努力区分OO和pyplot接口。我实际上正在使用Kivy GUI框架并尝试在单个图上绘制4个子图,由Kivy显示。这是我的代码片段:

def create_plot(self):

    self.fig, ((self.ax0, self.ax1), (self.ax2, self.ax3)) = plt.subplots(nrows=2, ncols=2)

    self.ax0.set_title("A")
    self.ax0.grid(True, lw = 2, ls = '--', c = '.75')

    self.ax1.set_title("B")
    self.ax1.grid(True, lw = 2, ls = '--', c = '.75')

    self.ax2.set_title("C")
    self.ax2.grid(True, lw = 2, ls = '--', c = '.75')

    self.ax3.set_title("D")
    self.ax3.grid(True, lw = 2, ls = '--', c = '.75')

    #plt.tight_layout()
    plt.show()

    canvas = self.fig.canvas
    self.add_widget(canvas)

让我担心的是我正在调用plt方法并将结果分配给我的对象。是pt状态机接口而不是OO接口,还是这样?

其次,我想定期更新绘制的线条,所以我有一个绘图方法:

def plot(self,  xCoords, yCoords):

    if len(self.ax0.lines) > 0:
        self.ax0.lines.pop(0)

    line = self.ax0.plot(xCoords, yCoords, color='blue')

    canvas = self.fig.canvas
    canvas.draw()

看起来不错吗?我可以只弹出现有的行,还是应该重用现有的行?

最后,也是最困难的,如果我启用:

plt.tight_layout()

我得到一个例外:

C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\matplotlib\tight_layout.py:225: UserWarning: tight_layout : falling back to Agg renderer
   warnings.warn("tight_layout : falling back to Agg renderer")
 Traceback (most recent call last):
   File "main.py", line 1117, in <module>
     GuiApp().run()
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\app.py", line 801, in run
     self.load_kv(filename=self.kv_file)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\app.py", line 598, in load_kv
     root = Builder.load_file(rfilename)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\lang.py", line 1801, in load_file
     return self.load_string(data, **kwargs)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\lang.py", line 1880, in load_string
     self._apply_rule(widget, parser.root, parser.root)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\lang.py", line 2038, in _apply_rule
     self._apply_rule(child, crule, rootrule)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\lang.py", line 2037, in _apply_rule
     self.apply(child)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\lang.py", line 1924, in apply
     self._apply_rule(widget, rule, rule)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\lang.py", line 2038, in _apply_rule
     self._apply_rule(child, crule, rootrule)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\lang.py", line 2038, in _apply_rule
     self._apply_rule(child, crule, rootrule)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\lang.py", line 2035, in _apply_rule
     child = cls(__no_builder=True)
   File "C:\SVNProj\Raggio\trunk\hostconsole\gui\mygraph.py", line 127, in __init__
     self.create_plot()
   File "C:\SVNProj\Raggio\trunk\hostconsole\gui\mygraph.py", line 224, in create_plot
     self.add_widget(canvas)
   File "C:\Kivy-1.9.0-py3.4-win32-x64\Python34\lib\site-packages\kivy\uix\boxlayout.py", line 211, in add_widget
     widget.bind(
 AttributeError: 'FigureCanvasAgg' object has no attribute 'bind'

有人可以帮忙吗?

祝你好运

大卫

1 个答案:

答案 0 :(得分:0)

原帖

  1. 关于def create_plot
  2. plt.subplots()只是创建一个新图形并在该图形中添加子图(轴)的包装器,因此可以安全地在任何上下文中使用AFAIK

    1. 关于更新行与创建新行。
    2. 通过调用axes.plot()弹出并创建新行会有效,但速度会慢于仅使用现有的线条艺术家并调用artist.set_data(x_data, y_data)

      1. 关于tight_layout问题
      2. 考虑只调用self.fig.tight_layout()而不是plt.tight_layout(),因为plt.tight_layout()可能会抓错图

        根据评论中的讨论进行更新:

        由于您要为每个面板绘制一行,我建议将这四行添加到create_plot方法中以保存由axes.plot()方法创建的LineArtist:

        self.line0, = self.ax0.plot([], [], 'b')
        self.line1, = self.ax1.plot([], [], 'b')
        self.line2, = self.ax2.plot([], [], 'b')
        self.line3, = self.ax3.plot([], [], 'b')
        

        然后在plot方法中你可以这样做:

        self.line0.set_data(xCoords, yCoords)
        

        虽然我似乎很难记住如何让ax0根据xCoordsyCoords自动更新其限制。我认为它就像self.ax0.relim()一样简单,但是现在我的jupyter笔记本中没有这个功能。 HMM。