小编典典

在matplotlib的x轴上//中断

python

描述我要实现的目标的最佳方法是使用自己的图像

现在我在光谱图中有很多死角,尤其是在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但至今仍未找到任何答案。

但是,我的折断线没有我想要的陡峭…如何更改它们?(我利用了下面的示例答案)


阅读 302

收藏
2020-12-20

共1个答案

小编典典

您可以直接将matplotlib示例改编为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()

matplotlib
x轴损坏示例

出于您的目的,只需两次绘制数据(在每个轴上绘制一次,ax然后适当地ax2设置xlims即可。“中断线”应移动以匹配新的中断,因为它们是在相对轴坐标而非数据坐标中绘制的。

折线只是在一对点之间绘制的未剪裁的绘图线。例如,在第一个轴上的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)

2020-12-20