Python 中断 // 在 matplotlib 的 x 轴上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32185411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Break // in x axis of matplotlib
提问by GCien
Best way to describe what I want to achieve is using my own image:
描述我想要实现的目标的最佳方式是使用我自己的图像:
Now I have a lot of dead space in the spectra plot, especially between 5200 and 6300. My question is quite simple, how would I add in a nice little // break that looks something similar to this (image lifted from the net):
现在我在光谱图中有很多死空间,尤其是在 5200 和 6300 之间。我的问题很简单,我将如何添加一个漂亮的小 // 中断,看起来与此类似(图片来自网络):
I'm using this setup for my plots:
我正在为我的情节使用这个设置:
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)
I'm quite certain it is achievable with gridpsec but an advanced tutorial cover exactly how this is achieved would be greatly appreciated.
我很确定使用 gridpsec 可以实现它,但是将非常感谢详细介绍如何实现的高级教程。
Apologies also if this question has been dealt with previously on stackoverflow but I have looked extensively for the correct procedure for gridSpec
but found nothing as yet.
如果之前在 stackoverflow 上已经处理过这个问题,我也很抱歉,但我已经广泛寻找正确的程序,gridSpec
但还没有找到。
I have managed to go as far as this, pretty much there:
我已经设法做到了这一点,几乎在那里:
However, my break lines are not as steep as I would like them...how do I change them? (I have made use of the example answer below)
然而,我的断裂线并不像我想要的那么陡峭……我该如何改变它们?(我已经使用了下面的示例答案)
采纳答案by xnx
You could adapt the matplotlib examplefor a break in the x-axis directly:
您可以直接调整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()
For your purposes, just plot your data twice (once on each axis, ax
and ax2
and set your xlim
s appropriately. The "break lines" should move to match the new break because they are plotted in relative axis coordinates rather than data coordinates.
出于您的目的,只需将数据绘制两次(在每个轴上绘制一次,ax
并适当ax2
设置xlim
s。“中断线”应移动以匹配新的中断,因为它们是在相对轴坐标而不是数据坐标中绘制的。
The break lines are just unclipped plot lines drawn between a pair of points. E.g. ax.plot((1-d,1+d), (-d,+d), **kwargs)
plots the break line between point (1-d,-d)
and (1+d,+d)
on the first axis: this is the bottom righthand one. If you want to change the graident, change these values appropriately. For example, to make this one steeper, try ax.plot((1-d/2,1+d/2), (-d,+d), **kwargs)
中断线只是在一对点之间绘制的未剪裁的绘图线。例如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)
回答by ben.dichter
The solution provided by xnx is a good start, but there is a remaining issue that the scales of the x-axes are different between the plots. This is not a problem if the range in the left plot and the range in the right plot are the same, but if they are unequal, subplot will still give the two plots equal width, so the x-axis scale will be different between the two plots (as is the case with xnx's example). I made a package, brokenaxesto deal with this.
xnx 提供的解决方案是一个良好的开端,但仍然存在一个问题,即图之间 x 轴的比例不同。如果左图中的范围和右图中的范围相同,这不是问题,但如果它们不相等,子图仍会使两个图具有相等的宽度,因此 x 轴比例在两个图之间会有所不同两个图(就像 xnx 的例子一样)。我做了一个包,断轴来处理这个。