2013-10-30 30 views
10

我有一個很難下我的曲線圖繪製網格線而不與主x和y軸ZORDER搞亂後面網格線:Matplotlib:保持圖形,但Y和X軸之上

import matplotlib.pyplot as plt 
import numpy as np 


N = 5 
menMeans = (20, 35, 30, 35, 27) 
menStd = (2, 3, 4, 1, 2) 

ind = np.arange(N) # the x locations for the groups 
width = 0.35  # the width of the bars 

fig, ax = plt.subplots() 
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd, alpha=0.9, linewidth = 0,zorder=3) 

womenMeans = (25, 32, 34, 20, 25) 
womenStd = (3, 5, 2, 3, 3) 
rects2 = ax.bar(ind+width, womenMeans, width, color='y', yerr=womenStd, alpha=0.9, linewidth = 0,zorder=3) 

# add some 
ax.set_ylabel('Scores') 
ax.set_title('Scores by group and gender') 
ax.set_xticks(ind+width) 
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5')) 

ax.legend((rects1[0], rects2[0]), ('Men', 'Women')) 

fig.gca().yaxis.grid(True, which='major', linestyle='-', color='#D9D9D9',zorder=2, alpha = .9) 
[line.set_zorder(4) for line in ax.lines] 

def autolabel(rects): 
    # attach some text labels 
    for rect in rects: 
     height = rect.get_height() 
     ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height), 
       ha='center', va='bottom') 

autolabel(rects1) 
autolabel(rects2) 

plt.show() 

示例是從matplotlib自己的,我已經調整了一點,以顯示如何使問題出現。我無法發佈圖片,但是如果您運行代碼,您會看到這些條被繪製在水平網格線上方以及x和y軸上方。我不希望x和y軸被圖形隱藏,特別是當蜱也被阻擋時。

+0

爲什麼要使用'fig.gca()'而不是'ax'? –

+0

出於好奇:你知道在'ax.lines'中創建行嗎? –

+0

你是對的,我只是想弄明白如何去做,而不是試圖理解一切是如何工作的...... –

回答

2

我已經試過matplotlib 1.2.1 1.3.1rc2和碩士(提交06d014469fc5c79504a1b40e7d45bc33acc00773)

要獲得橫槓頂部的軸刺,你可以做到以下幾點:

for k, spine in ax.spines.items(): #ax.spines is a dictionary 
    spine.set_zorder(10) 

編輯

看來,我不能讓剔線走在酒吧的頂部。我試過

1. ax.tick_params(direction='in', length=10, color='k', zorder=10) 
    #This increases the size of the lines to 10 points, 
    #but the lines stays hidden behind the bars 
2. for l in ax.yaxis.get_ticklines(): 
     l.set_zorder(10) 

和其他一些沒有結果的方式。看來,繪製橫線時,他們都穿上頂部和ZORDER被忽略

一種解決方法可能是繪製刻度線向外

ax.tick_params(direction='out', length=4, color='k', zorder=10) 

或兩者並向外,用direction='inout'

EDIT2

我在@tcaswell評論後做了一些測試。

如果ax.bar功能zorder設定爲< = 2時,軸,ticklines 網格線上面的條繪製。如果valus> 2.01(軸的默認值),則會在軸,ticklines和網格上繪製條形圖。然後可以設置更大的值(如上所述),但任何試圖改變ticklines的嘗試都會被忽略(儘管值會在相應的藝術家中更新)。

我試着用zorder=1barzorder=0網格與網格繪製在酒吧的頂部。所以zorder被忽略。

回顧

在我看來,那ticklines和電網zorder只是忽略並保持爲默認值。對我來說,這是一個與barpatches有關的bug。

BTW,我記得在ticklines成功改變ZORDER使用imshow

+0

這聽起來像一個bug,你能檢查它是否也發生在主人身上嗎? – tacaswell

+0

我試一試,讓你知道 –

+0

@tcaswell我試過1.3.1rc2和master(剛剛提取)並且不起作用。那是一個錯誤。誰提交文件? [我可以試着在今天晚些時候看看源代碼,如果我發現有關該問題的任何暗示] –

1

當我有軸爲@ luke_16上面繪製的同樣的問題。在我的情況下,它使用ax.set_axisbelow(True)選項來設置劇情背後的情節。

我對這個錯誤的解決方法是,不使用板載格,而是要模擬它:

def grid_selfmade(ax,minor=False): 
    y_axis=ax.yaxis.get_view_interval() 
    for xx in ax.xaxis.get_ticklocs(): 
     ax.plot([xx,xx],y_axis,linestyle=':',linewidth=0.5,zorder=0) 
    if minor==True: 
     for xx in ax.xaxis.get_ticklocs(minor=True): 
      ax.plot([xx,xx],y_axis,linestyle=':',linewidth=0.5,zorder=0) 
    x_axis=ax.xaxis.get_view_interval() 
    for yy in ax.yaxis.get_ticklocs(): 
     ax.plot(x_axis,[yy,yy],linestyle=':',linewidth=0.5,zorder=0,) 
    if minor==True: 
     for yy in ax.yaxis.get_ticklocs(minor=True): 
      ax.plot(x_axis,[yy,yy],linestyle=':',linewidth=0.5,zorder=0) 

此功能只需要當前軸實例,然後繪製板載狀網格背後其他一切在主要蜱(可選也爲次要蜱)。

對於圖形頂部的座標軸和座標軸刻度,必須將ax.set_axisbelow(False)設置爲False,並且不要在圖中使用zorder> 2。我通過更改代碼中的繪圖命令的順序來管理沒有任何zorder選項的繪圖zorder。

5

我有軸同樣的問題越來越繪製的情節線之下時,我有背景網格線:

ax.yaxis.grid() # grid lines 
ax.set_axisbelow(True) # grid lines are behind the rest 

爲我工作的解決方案是設置zorder參數的plot()函數來一個介於1和2之間的值。它不是立即清楚,但zorder值可以是任何數字。從已matplotlib.artist.Artist類的文檔:

set_zorder(水平)

設置藝術家的ZORDER。首先繪製具有較低zorder值的藝術家。

承接:任何數量的

因此:

for i in range(5): 
    ax.plot(range(10), np.random.randint(10, size=10), zorder=i/100.0 + 1) 

我沒有檢查的值在此範圍之外,也許他們也將工作。

0

一個更簡單,更好的解決方案是將線從電網命令複製和禁用網格,再重新添加行,但用正確ZORDER:

ax.grid(True) 
lines = ax1.xaxis.get_gridlines().copy() + ax1.yaxis.get_gridlines().copy() 
ax.grid(False) 
for l in lines: 
    ax1.add_line(l) 
    l.set_zorder(0) 
相關問題