在matplotlib的x轴上中断//

GCi*_*ien 18 python plot matplotlib

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

在此输入图像描述

现在我在光谱图中有很多死区,特别是在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)
Run Code Online (Sandbox Code Playgroud)

我非常肯定它可以通过gridpsec实现,但是一个高级教程确切地说明了如何实现这一点将非常感激.

如果这个问题之前已经在stackoverflow上处理过,但我已经广泛地查看了正确的程序,gridSpec但尚未找到任何内容.

我已经设法走了这么远,几乎就在那里:

在此输入图像描述

但是,我的断线并不像我想的那样陡峭...我该怎么改变它们?(我已经使用了下面的示例答案)

xnx*_*xnx 18

您可以直接调整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()
Run Code Online (Sandbox Code Playgroud)

matplotlib破x轴示例

为了您的目的,只需绘制两次数据(在每个轴上绘制一次,ax然后适当地ax2设置)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)


ben*_*ter 13

xnx提供的解决方案是一个良好的开端,但仍有一个问题是x轴的比例在图之间是不同的.如果左图中的范围和右图中的范围相同,则这不是问题,但如果它们不相等,则子图仍将使两个图的宽度相等,因此x轴刻度将在两个图(就像xnx的例子一样).我做了一个包,破解了这个.

  • 谢谢你!效果很好。 (3认同)
  • 这太棒了! (3认同)
  • 创建断轴时使用 despine=False 解决了:)(是的,我应该在询问之前阅读文档!) (2认同)