我还没有找到调整底部和顶部图高度的解决方案断轴示例 https://matplotlib.org/stable/gallery/subplots_axes_and_figures/broken_axis.html of 绘图库 http://matplotlib.org/examples.
顺便说一句:两个图之间的空间可以通过以下方式调整:
plt.subplots_adjust(hspace=0.03)
UPDATE:
我几乎已经使用 gridspec 弄清楚了:
"""
Broken axis example, where the y-axis will have a portion cut out.
"""
import matplotlib.pylab as plt
# NEW:
import matplotlib.gridspec as gridspec
import numpy as np
pts = np.array([ 0.015, 0.166, 0.133, 0.159, 0.041, 0.024, 0.195,
0.039, 0.161, 0.018, 0.143, 0.056, 0.125, 0.096, 0.094, 0.051,
0.043, 0.021, 0.138, 0.075, 0.109, 0.195, 0.05 , 0.074, 0.079,
0.155, 0.02 , 0.01 , 0.061, 0.008])
pts[[3,14]] += .8
# NEW:
gs = gridspec.GridSpec(2, 1, height_ratios=[1, 3])
ax = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])
plt.subplots_adjust(hspace=0.03)
ax.plot(pts)
ax2.plot(pts)
ax.set_ylim(.78,1.)
ax2.set_ylim(.0,.22)
ax.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax.xaxis.tick_top()
ax.tick_params(labeltop='off')
ax2.xaxis.tick_bottom()
d = .03
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot((0-d,0+d), (0-d,0+d), **kwargs) # top-left diagonal
ax.plot((1-d,1+d), (0-d,0+d), **kwargs) # top-right diagonal
kwargs.update(transform=ax2.transAxes) # switch to the bottom axes
ax2.plot((0-d,0+d),(1-d,1+d), **kwargs) # bottom-left diagonal
ax2.plot((1-d,1+d),(1-d,1+d), **kwargs) # bottom-right diagonal
plt.show()
剩下的问题是:
-
两条平行线段(断轴标记)
由于 y 轴长度不同,不再平行。
-
进一步的问题是如何方便地定位 ylabel。
最后没看到这个选项sharex=True
在网格规范中。这有关系吗?
UPDATE:
添加了 ylim 和 xlim v2 参数来确定高度比,使数据单位相等:
ylim = [0.8, 1.0]
ylim2 = [0.0, 0.3]
ylimratio = (ylim[1]-ylim[0])/(ylim2[1]-ylim2[0]+ylim[1]-ylim[0])
ylim2ratio = (ylim2[1]-ylim2[0])/(ylim2[1]-ylim2[0]+ylim[1]-ylim[0])
gs = gridspec.GridSpec(2, 1, height_ratios=[ylimratio, ylim2ratio])
ax = plt.subplot(gs[0])
ax2 = plt.subplot(gs[1])
ax.set_ylim(ylim)
ax2.set_ylim(ylim2)