【问题标题】:Break // in x axis of matplotlib [duplicate]Break //在matplotlib的x轴上[重复]
【发布时间】:2015-11-18 01:26:44
【问题描述】:

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

现在我在光谱图中有很多死区,特别是在 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 的正确程序,但目前还没有找到。

我已经做到了这一点,差不多到了:

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

【问题讨论】:

标签: python matplotlib plot


【解决方案1】:

您可以直接调整 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 并适当设置您的 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)

【讨论】:

  • 我将添加到目前为止的内容,但是我的折线不如您的示例那么好,如何修改它们的“渐变”?
  • 查看我更新的问题。
  • 折线只是未剪裁的情节线:请参阅我的编辑。
  • 尝试了你的建议,但这并没有让它们变得更陡峭,只是更短......
  • 太棒了 - 很高兴它有帮助!
【解决方案2】:

xnx 提供的解决方案是一个良好的开端,但还有一个问题是 x 轴的比例在绘图之间是不同的。如果左图中的范围和右图中的范围相同,这不是问题,但如果它们不相等,子图仍然会给两个图相同的宽度,因此 x 轴刻度在两个图(与 xnx 的示例一样)。我做了一个包,brokenaxes 来处理这个问题。

【讨论】:

  • 谢谢!完美运行。
  • 这太棒了!
  • 有没有办法将断轴作为插图添加到图中?
  • @user668074 好问题。我认为作为插图的断轴是一个不寻常的用例,但它应该是可能的。我没有尝试这样做,但我认为你最好的选择是使用 GridSpec 放置你的轴,然后将 subplot_spec 传递给 brokenaxes
  • 在创建损坏的轴时用 despine=False 解决 :)(是的,我应该在询问之前阅读文档!)
猜你喜欢
  • 2021-12-30
  • 2020-10-14
  • 1970-01-01
  • 2021-11-20
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 2020-03-29
  • 2019-08-06
相关资源
最近更新 更多