使用subplot2grid时如何sharex [英] How to sharex when using subplot2grid
问题描述
我是最近转换为Python的Matlab用户.我自己管理大部分的Python技能,但是通过绘图,我已经不知所措了,需要一些帮助.
I'm a Matlab user recently converted to Python. Most of the Python skills I manage on my own, but with plotting I have hit the wall and need some help.
这就是我想要做的...
This is what I'm trying to do...
我需要制作一个包含3个具有以下属性的子图的图形:
I need to make a figure that consists of 3 subplots with following properties:
- 子图布局为311、312、313
- 312和313的高度大约是311的一半
- 所有子图共享相同的X轴
- 子图之间的间距为0(它们在X轴上彼此接触)
顺便说一下,我知道如何制作所有这些,只是不是在一个数字中.那就是我现在面临的问题.
By the way I know how to make all this, only not in a single figure. That is the problem I'm facing now.
例如,这是我理想的子图布局:
For example, this is my ideal subplot layout:
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0.0, 2.0, 0.01)
s1 = np.sin(2*np.pi*t)
s2 = np.exp(-t)
s3 = s1*s2
fig = plt.figure()
ax1 = plt.subplot2grid((4,3), (0,0), colspan=3, rowspan=2)
ax2 = plt.subplot2grid((4,3), (2,0), colspan=3)
ax3 = plt.subplot2grid((4,3), (3,0), colspan=3)
ax1.plot(t,s1)
ax2.plot(t[:150],s2[:150])
ax3.plot(t[30:],s3[30:])
plt.tight_layout()
plt.show()
注意不同子图的x轴未对齐.我不知道如何在此图中对齐x轴,但是如果我做这样的事情:
Notice how the x axis of different subplots is misaligned. I do not know how to align the x axis in this figure, but if I do something like this:
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0.0, 2.0, 0.01)
s1 = np.sin(2*np.pi*t)
s2 = np.exp(-t)
s3 = s1*s2
fig2, (ax1, ax2, ax3) = plt.subplots(nrows=3, ncols=1, sharex=True)
ax1.plot(t,s1)
ax2.plot(t[:150],s2[:150])
ax3.plot(t[30:],s3[30:])
plt.tight_layout()
plt.show()
现在,x轴在子图之间对齐,但是所有子图都具有相同的大小(这不是我想要的大小)
Now the x axis is aligned between the subplots, but all subplots are the same size (which is not what I want)
此外,我希望子图在 x 轴上像这样接触:
Furthermore, I would like that the subplots are touching at x axis like this:
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0.0, 2.0, 0.01)
s1 = np.sin(2*np.pi*t)
s2 = np.exp(-t)
s3 = s1*s2
fig1 = plt.figure()
plt.subplots_adjust(hspace=0)
ax1 = plt.subplot(311)
ax2 = plt.subplot(312, sharex=ax1)
ax3 = plt.subplot(313, sharex=ax1)
ax1.plot(t,s1)
ax2.plot(t[:150],s2[:150])
ax3.plot(t[30:],s3[30:])
xticklabels = ax1.get_xticklabels()+ax2.get_xticklabels()
plt.setp(xticklabels, visible=False)
plt.show()
所以重新表述我的问题:
So to rephrase my question:
我想使用
plt.subplot2grid(..., colspan=3, rowspan=2)
plt.subplots(..., sharex=True)
plt.subplots_adjust(hspace=0)
和
plt.tight_layout()
一起出现在同一张图中.如何做到这一点?
together in the same figure. How to do that?
推荐答案
在创建第二和第三子图时只需指定 sharex = ax1
.
Just specify sharex=ax1
when creating your second and third subplots.
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0.0, 2.0, 0.01)
s1 = np.sin(2*np.pi*t)
s2 = np.exp(-t)
s3 = s1*s2
fig = plt.figure()
ax1 = plt.subplot2grid((4,3), (0,0), colspan=3, rowspan=2)
ax2 = plt.subplot2grid((4,3), (2,0), colspan=3, sharex=ax1)
ax3 = plt.subplot2grid((4,3), (3,0), colspan=3, sharex=ax1)
ax1.plot(t,s1)
ax2.plot(t[:150],s2[:150])
ax3.plot(t[30:],s3[30:])
fig.subplots_adjust(hspace=0)
for ax in [ax1, ax2]:
plt.setp(ax.get_xticklabels(), visible=False)
# The y-ticks will overlap with "hspace=0", so we'll hide the bottom tick
ax.set_yticks(ax.get_yticks()[1:])
plt.show()
如果仍然使用 fig.tight_layout()
,则需要在之前 fig.subplots_adjust(hspace = 0).这样做的原因是
tight_layout
通过自动计算 subplots_adjust
的参数然后调用它来工作,所以如果 subplots_adjust
首先被手动调用,那么对它的第一次调用将被 tight_layout
覆盖.
If you still what to use fig.tight_layout()
, you'll need to call it before fig.subplots_adjust(hspace=0)
. The reason for this is that tight_layout
works by automatically calculating parameters for subplots_adjust
and then calling it, so if subplots_adjust
is manually called first, anything in the first call to it will be overridden by tight_layout
.
例如
fig.tight_layout()
fig.subplots_adjust(hspace=0)
这篇关于使用subplot2grid时如何sharex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!