简体   繁体   English

子图中的不连续轴-python matplotlib

[英]discontinous axis in subplot - python matplotlib

I would like to have plot with an y axis that is devided into two parts. 我想将y轴分为两个部分。 The lower part should have a normal scale while the upper one should scale with a factor of 10. 下部应具有正常比例,而上部应具有10的比例。

I already found some examples on how to make plots with broken x or y axes, for example: http://matplotlib.org/examples/pylab_examples/broken_axis.html 我已经找到了一些有关如何使用x轴或y轴断开的图的示例,例如: http : //matplotlib.org/examples/pylab_examples/broken_axis.html

But I do not understand how to achieve this, when I want to apply this to one single subplot inside a 2x2 grid of plots. 但是,当我要将其应用于2x2的绘图网格中的一个子图时,我不知道如何实现。 If it is important, I set up the plots like this: 如果重要的话,我可以按照以下步骤进行设置:

fig = plt.figure()
fig.set_size_inches(8, 6)

fig.add_subplot(221)
[...]
fig.add_subplot(222)
[...]

Couldn't you set up a 4x4 grid of axes, and have 3 of the axes span 2x2 of that space? 您是否无法设置4x4的轴网格,并且其中3个轴跨越该空间的2x2? Then the plot you want to have broken axes on can just cover the remaining 2x2 space as parts ax4_upper and ax4_lower . 然后,您想要在轴上断开的图就可以覆盖剩下的2x2空间,分别是ax4_upperax4_lower部分。

ax1 = plt.subplot2grid((4, 4), (0, 0), colspan=2, rowspan=2)
ax2 = plt.subplot2grid((4, 4), (0, 2), colspan=2, rowspan=2)
ax3 = plt.subplot2grid((4, 4), (2, 0), colspan=2, rowspan=2)
ax4_upper = plt.subplot2grid((4, 4), (2, 2), colspan=2, rowspan=1)
ax4_lower = plt.subplot2grid((4, 4), (3, 2), colspan=2, rowspan=1)

You can then set the ylim values for ax4_upper and ax4_lower , and continue as your example showed: 然后,可以为ax4_upperax4_lower设置ylim值,并继续执行示例所示:

# hide the spines between ax4 upper and lower
ax4_upper.spines['bottom'].set_visible(False)
ax4_lower.spines['top'].set_visible(False)
ax4_upper.xaxis.tick_top()
ax4_upper.tick_params(labeltop='off') # don't put tick labels at the top
ax4_lower.xaxis.tick_bottom()

d = .015 # how big to make the diagonal lines in axes coordinates
# arguments to pass plot, just so we don't keep repeating them
kwargs = dict(transform=ax4_upper.transAxes, color='k', clip_on=False)
ax4_upper.plot((-d,+d),(-d,+d), **kwargs)      # top-left diagonal
ax4_upper.plot((1-d,1+d),(-d,+d), **kwargs)    # top-right diagonal

kwargs.update(transform=ax4_lower.transAxes)  # switch to the bottom axes
ax4_lower.plot((-d,+d),(1-d,1+d), **kwargs)   # bottom-left diagonal
ax4_lower.plot((1-d,1+d),(1-d,1+d), **kwargs) # bottom-right diagonal

plt.show()

2x2断轴

You could use gridspec to layout the shape and location of the axes: 您可以使用gridspec布局轴的形状和位置:

import numpy as np
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt

gs = gridspec.GridSpec(4, 2)
ax00 = plt.subplot(gs[:2, 0])
ax01 = plt.subplot(gs[:2, 1])
ax10a = plt.subplot(gs[2, 0])
ax10b = plt.subplot(gs[3, 0])
ax11 = plt.subplot(gs[2:, 1])

x = np.linspace(-1, 1, 500)
y = 100*np.cos(10*x)**2*np.exp(-x**2)
for ax in (ax00, ax01, ax10a, ax10b, ax11):
    ax.plot(x, y)

ax10a.set_ylim(60, 110)
ax10b.set_ylim(0, 10) 

ax10a.spines['bottom'].set_visible(False)
ax10b.spines['top'].set_visible(False)
ax10a.xaxis.tick_top()
ax10a.tick_params(labeltop='off') # don't put tick labels at the top
ax10b.xaxis.tick_bottom()

d = .015 # how big to make the diagonal lines in axes coordinates
# arguments to pass plot, just so we don't keep repeating them
kwargs = dict(transform=ax10a.transAxes, color='k', clip_on=False)
ax10a.plot((-d,+d),(-d,+d), **kwargs)      # top-left diagonal
ax10a.plot((1-d,1+d),(-d,+d), **kwargs)    # top-right diagonal

kwargs.update(transform=ax10b.transAxes)  # switch to the bottom axes
ax10b.plot((-d,+d),(1-d,1+d), **kwargs)   # bottom-left diagonal
ax10b.plot((1-d,1+d),(1-d,1+d), **kwargs) # bottom-right diagonal

plt.tight_layout()
plt.show()

在此处输入图片说明

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

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