简体   繁体   English

为什么matplotlib.gridspec的紧密布局不能与pyplot.subplot一起正常工作,而与fig.add_subplot一起工作呢?

[英]Why does matplotlib.gridspec's tight_layout not work correctly with pyplot.subplot whereas it does with fig.add_subplot?

I am trying to create two subplots next to one another using gridspec (the real plot is more complicated but this is the basic idea). 我正在尝试使用gridspec创建两个彼此相邻的子图(实际图更为复杂,但这是基本思想)。 I am creating the subplots using plt.subplot , but this does not seem to produce the expected result. 我正在使用plt.subplot创建子图,但这似乎无法产生预期的结果。 I get only the second subplot on the right side of the figure, and not the first one. 我仅在图的右侧获得第二个子图,而没有获得第一个子图。

On the other hand, if I create the subplots by using fig.add_subplot , the plot appears correctly although I get a tight_layout warning. 另一方面,如果我使用fig.add_subplot创建了子图,尽管得到了tight_layout警告,该图仍可以正确显示。 I am a little surprised at this behaviour, since pyplot's subplot function internally calls fig.add_subplot (and then does some checking to see if the subplot already exists -- I guess?). 我对此行为感到有些惊讶,因为pyplot的subplot函数在内部调用了fig.add_subplot(然后进行一些检查以查看该子图是否已经存在-我猜是吗?)。 Here is the code that produces only the rightmost subplot. 这是仅产生最右边子图的代码。 Switching the comments to use plt.gcf().add_subplot gets it to work as expected. 切换注释以使用plt.gcf().add_subplot可使注释按预期工作。

from matplotlib import gridspec,pyplot as plt

gs = gridspec.GridSpec(1,1)
ax1 = plt.subplot(gs[0,0])
# ax1 = plt.gcf().add_subplot(gs[0,0])
ax1.plot(range(3),range(3))
gs.tight_layout(plt.gcf(),rect=[0,0,0.45,1])

gs = gridspec.GridSpec(1,1)
ax1 = plt.subplot(gs[0,0])
# ax1 = plt.gcf().add_subplot(gs[0,0])
ax1.plot(range(3),range(3))
gs.tight_layout(plt.gcf(),rect=[0.55,0,1,1])

plt.show()

Am I doing something wrong, or am I missing something here? 我是在做错什么,还是我在这里想念什么? For reference, here is what pyplot.subplot primarily does: 作为参考,这是pyplot.subplot的主要作用:

fig = gcf()
a = fig.add_subplot(*args, **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)

return a

My question is: why does using pyplot.subplot not produce the same result as fig.add_subplot? 我的问题是:为什么使用pyplot.subplot不能产生与fig.add_subplot相同的结果?

What subplot does is checking for overlapping plots and deleting them if they exists. subplot作用是检查重叠的图并删除它们(如果存在)。 Your second subplot overlaps the first one so your second plt.subplot will delete the first one. 您的第二plt.subplot图与第一plt.subplot图重叠,因此您的第二个plt.subplot将删除第一个。 Figure.add_subplot does not delete overlapping subplots. Figure.add_subplot不会删除重叠的子图。

Note that you are creating subplots that fill the whole figure: 请注意,您正在创建填充整个图形的子图:

gs = gridspec.GridSpec(1,1)
ax1 = plt.subplot(gs[0,0])

This is a subplot in a 1x1 grid so it will fill the whole figure and overlap any previous subplot. 这是1x1网格中的子图,因此它将填充整个图形并与以前的任何子图重叠。

BTW the issue has nothing to do with tight_layout which is working correctly.. 顺便说一句,这个问题与可以正常工作的tight_layout无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM