动态地向figu添加一个新的子blot2grid

2024-09-30 16:26:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个应用程序,有3个数字,并动态改变他们。目前,通过在图中使用add_subplot()可以很好地工作。但是现在我必须使我的图更复杂,并且需要使用subplot2grid()

self.figure1 = plt.figure()
self.canvas1 = FigureCanvas(self.figure1)
self.graphtoolbar1 = NavigationToolbar(self.canvas1, frameGraph1)

self.figure3 = plt.figure()
self.canvas3 = FigureCanvas(self.figure3)
self.graphtoolbar3 = NavigationToolbar(self.canvas3, frameGraph3)

self.figure4 = plt.figure()
self.canvas4 = FigureCanvas(self.figure4)
self.graphtoolbar4 = NavigationToolbar(self.canvas4, frameGraph4)

下面是添加它的代码,以及到目前为止我所得到的。在

^{pr2}$

上图将其添加到图4中。可能是因为这是plt最新的实例。但是,我希望上面的代码在self.figure1中添加一个子blot2grid,同时仍像以前一样具有动态功能。在


Tags: 代码self应用程序动态plt数字figurefigurecanvas
1条回答
网友
1楼 · 发布于 2024-09-30 16:26:09

查看matplotlib的源代码,subplot2grid-函数定义如下:

def subplot2grid(shape, loc, rowspan=1, colspan=1, **kwargs):
    """
    Create a subplot in a grid.  The grid is specified by *shape*, at
    location of *loc*, spanning *rowspan*, *colspan* cells in each
    direction.  The index for loc is 0-based. ::

      subplot2grid(shape, loc, rowspan=1, colspan=1)

    is identical to ::

      gridspec=GridSpec(shape[0], shape[2])
      subplotspec=gridspec.new_subplotspec(loc, rowspan, colspan)
      subplot(subplotspec)
    """

    fig = gcf()  #  <     HERE
    s1, s2 = shape
    subplotspec = GridSpec(s1, s2).new_subplotspec(loc,
                                                   rowspan=rowspan,
                                                   colspan=colspan)
    a = fig.add_subplot(subplotspec, **kwargs)
    bbox = a.bbox
    byebye = []
    for other in fig.axes:
        if other==a: continue
        if bbox.fully_overlaps(other.bbox):
            byebye.append(other)
    for ax in byebye: delaxes(ax)

    draw_if_interactive()
    return a

正如您从代码片段中的注释“HERE”中看到的,它只使用活动图形,即fig = gcf()(gcf是“getcurrent figure”的缩写)。在

通过稍微修改函数并将其放入脚本中,应该很容易实现目标。在

^{pr2}$

现在应该可以做你的事情了,修改你的函数调用

ax = plt.my_subplot2grid(self.figure1, (4,4), (0,0), rowspan=3, colspan=4)

希望有帮助!在

相关问题 更多 >