在matplotlib的x轴上断开//[重复]

jvlzgdj9  于 2023-04-21  发布在  其他
关注(0)|答案(2)|浏览(118)

此问题已在此处有答案

Is there a way to make a discontinuous axis in Matplotlib?(7个回答)
5年前关闭。
最好的方式来描述我想要实现的是用我自己的形象:

现在我在光谱图中有很多死空间,特别是在5200和6300之间。我的问题很简单,我如何添加一个看起来类似于此的漂亮的小//中断(从网上提取的图像):

我使用这个设置为我的情节:

nullfmt = pyplot.NullFormatter()

fig = pyplot.figure(figsize=(16,6))

gridspec_layout1= gridspec.GridSpec(2,1)
gridspec_layout1.update(left=0.05, right=0.97, hspace=0, wspace=0.018)
pyplot_top      = fig.add_subplot(gridspec_layout1[0])
pyplot_bottom   = fig.add_subplot(gridspec_layout1[1])

pyplot_top.xaxis.set_major_formatter(nullfmt)

我很确定这是可以实现的gridpsec,但一个先进的教程封面究竟是如何实现的,将不胜感激。
如果这个问题以前在stackoverflow上已经处理过了,我也很抱歉,但是我已经广泛地寻找了gridSpec的正确过程,但是还没有找到任何东西。
我已经做到了这一步,差不多就是这样:

然而,我的中断线并不像我希望的那样陡峭…我该如何改变它们?(我已经使用了下面的示例答案)

kulphzqa

kulphzqa1#

您可以直接将the matplotlib example调整为x轴上的中断:

"""
Broken axis example, where the x-axis will have a portion cut out.
"""
import matplotlib.pylab as plt
import numpy as np

x = np.linspace(0,10,100)
x[75:] = np.linspace(40,42.5,25)

y = np.sin(x)

f, (ax, ax2) = plt.subplots(1, 2, sharey=True, facecolor='w')

# plot the same data on both axes
ax.plot(x, y)
ax2.plot(x, y)

ax.set_xlim(0, 7.5)
ax2.set_xlim(40, 42.5)

# hide the spines between ax and ax2
ax.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)
ax.yaxis.tick_left()
ax.tick_params(labelright='off')
ax2.yaxis.tick_right()

# This looks pretty good, and was fairly painless, but you can get that
# cut-out diagonal lines look with just a bit more work. The important
# thing to know here is that in axes coordinates, which are always
# between 0-1, spine endpoints are at these locations (0, 0), (0, 1),
# (1, 0), and (1, 1).  Thus, we just need to put the diagonals in the
# appropriate corners of each of our axes, and so long as we use the
# right transform and disable clipping.

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=ax.transAxes, color='k', clip_on=False)
ax.plot((1-d, 1+d), (-d, +d), **kwargs)
ax.plot((1-d, 1+d), (1-d, 1+d), **kwargs)

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

# What's cool about this is that now if we vary the distance between
# ax and ax2 via f.subplots_adjust(hspace=...) or plt.subplot_tool(),
# the diagonal lines will move accordingly, and stay right at the tips
# of the spines they are 'breaking'

plt.show()

出于您的目的,只需将数据绘制两次(每个轴上一次,axax2,并适当地设置xlim。“断开线”应该移动以匹配新的断开,因为它们是在相对轴坐标而不是数据坐标中绘制的。
断开线只是在一对点之间绘制的未剪裁的绘图线。例如,ax.plot((1-d, 1+d), (-d, +d), **kwargs)在第一个轴上绘制点(1-d, -d)(1+d, +d)之间的断开线:这是右下角的一个。如果你想改变梯度,适当地改变这些值。例如,要使这个更陡,尝试ax.plot((1-d/2, 1+d/2), (-d, +d), **kwargs)

t3psigkw

t3psigkw2#

xnx提供的解决方案是一个很好的开始,但还有一个遗留的问题,即x轴的尺度在图之间是不同的。如果左侧图中的范围和右侧图中的范围相同,这不是问题,但如果它们不相等,子图仍然会给予两个图相等的宽度,所以两个图的x轴比例是不同的(就像xnx的例子一样)。我做了一个包brokenaxes来处理这个问题。

相关问题