2015-08-24 27 views
8

最佳方式x軸來形容我要的是用我自己的形象,實現什麼:歇//在matplotlib

enter image description here

現在我在光譜的情節有很多死角,尤其之間5200和6300我的問題很簡單,我怎麼會在一個可愛的小破//看起來與此類似(圖片來自網絡解除)地址:

enter image description here

我使用這個設置對我的情節:

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正確的程序,但沒有發現任何東西。

我設法儘可能此去,幾乎有:

enter image description here

然而,我的斷裂線並不陡峭,因爲我想他們......我該如何改變呢? (我已經使用的如回答以下)

+1

這是[示例](http://matplotlib.org/examples/pylab_examples/broken_axis.html)有幫助嗎? –

回答

10

您可以直接在x軸的突破適應the matplotlib example

""" 
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 broken x-axis example

你的目的,只是展現您的數據的兩倍(在每個軸上一次,axax2並適當設置您的xlim s「斷線」應該移動以匹配新斷點,因爲它們繪製在相對座標軸座標而不是數據座標上

破折線只是在一對點之間繪製的未折線圖線。例如。 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)

+0

我會添加到目前爲止,但我的休息線不如你的例子,我怎麼修改他們的「梯度」? –

+0

查看我更新的問題。 –

+1

突破線只是未剪切的情節線:請參閱我的編輯。 – xnx

5

由xnx提供的解決方案是一個好的開端,但是剩下的問題是x圖的比例在圖之間不同。如果左側圖中的範圍與右側圖中的範圍相同,則這不是問題,但如果它們不相等,則子圖仍然會給出兩個圖相等的寬度,因此x軸的比例將會不同兩塊地塊(就像xnx的例子一樣)。我做了一個包,brokenaxes來處理這個。

+1

謝謝!完美工作。 – arie64