Python 向地物动态添加新的子地块2Grid

Python 向地物动态添加新的子地块2Grid,python,matplotlib,Python,Matplotlib,我有一个有3个数字的应用程序,动态地更改它们。目前,使用add_subplot()对图形进行操作效果良好。但现在我必须使图形更复杂,需要使用subplot2grid() 这是添加它的代码,以及到目前为止我得到的 #ax = self.figure1.add_subplot(2,1,1) <---- What I used to do fig = self.figure1 ax = plt.subplot2grid((4,4), (0,0), rowspan=3, colspan=4) #

我有一个有3个数字的应用程序,动态地更改它们。目前,使用
add_subplot()
对图形进行操作效果良好。但现在我必须使图形更复杂,需要使用
subplot2grid()

这是添加它的代码,以及到目前为止我得到的

#ax = self.figure1.add_subplot(2,1,1) <---- What I used to do

fig = self.figure1
ax = plt.subplot2grid((4,4), (0,0), rowspan=3, colspan=4) # <--- what I'm trying to do

ax.hold(False)
ax.plot(df['Close'], 'b-')
ax.legend(loc=0)
ax.set_xlabel("Date")
ax.set_ylabel("Price")
ax.grid(True)

for tick in ax.get_xticklabels():
     tick.set_rotation(20)            

self.canvas1.draw()

#ax=self.figure1.add_subplot(2,1,1)查看
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
现在应该可以完成您的工作了,只需修改您的函数调用即可

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

希望有帮助

查看
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
现在应该可以完成您的工作了,只需修改您的函数调用即可

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

希望有帮助

谢谢你指出。我发现我可以在实例化这些数字时给它们一个数字。self.figure1=plt.figure(1),然后用plt.figure(1)设置cfg。你说得对!我觉得我有点过头了,更新一下当前的数据应该会更好。它建议您甚至可以通过字符串调用您的数字,而不仅仅是
图(1)
。感谢您指出。我发现我可以在实例化这些数字时给它们一个数字。self.figure1=plt.figure(1),然后用plt.figure(1)设置cfg。你说得对!我觉得我有点过头了,更新一下当前的数据应该会更好。它建议您甚至可以通过字符串调用数字,而不仅仅是
figure(1)