2015-06-11 210 views
1

我想用兩個y座標軸繪製一個圖形,並且我想要左y軸顯示一條線,根據該座標軸對x軸進行排序,右邊y軸顯示條。我的問題是第二個y軸隱藏了第一個y軸,如附圖所示。我使用的代碼如下:在Matplotlib中雙y座標軸上繪製條紋前面的線

import matplotlib 
import matplotlib.pyplot as plt 
from matplotlib.ticker import ScalarFormatter 

fig, ax1 = plt.subplots()  
ax2 = ax1.twinx() 
p1 = ax1.plot(ind, total_facilities, '--bo') 
width = 1 
p2 = ax2.bar(ind, pdb_facilities, width, color='gray',edgecolor = "none") 
plt.xlim([-1,len(total_facilities)]) 
ax1.set_yscale('symlog') 

當我重新安排軸線所以我繪製在二級軸行雲的灰色條前面的藍色線,但隨後這是令人困惑,因爲我想在x軸將根據主要y軸的值進行排序。有什麼方法可以將藍線保持在主y軸?

enter image description here

+1

嘗試ax1.plot (ind,total_facilities,'--bo',zorder = 10) – Sait

+0

謝謝,但它沒有工作 – Vasilis

+0

@Sait但是感謝您的評論,我在這裏找到了解決方案: http://matplotlib.1069221.n5.nabble .COM /控制-twinx系列 - ZORDER-AX2系列隱藏-AX1系列,或就地-AX2上左-AX1上 - 右 - td12994.html – Vasilis

回答

1

我發現我可以切換輔助y軸的地方,所以輔助y軸繪製在左側和右側的初級:

ax2 = ax1.twinx() 
p1 = ax2.plot(ind, total_facilities, '--bo') 
p2 = ax1.bar(ind, pdb_facilities, width, color='gray',edgecolor = "none") 
plt.xlim([-1,len(total_facilities)]) 
ax2.set_yscale('symlog') 
ax1.yaxis.tick_right() 
ax2.yaxis.tick_left() 
plt.show()