我有一個很難下我的曲線圖繪製網格線而不與主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軸被圖形隱藏,特別是當蜱也被阻擋時。
爲什麼要使用'fig.gca()'而不是'ax'? –
出於好奇:你知道在'ax.lines'中創建行嗎? –
你是對的,我只是想弄明白如何去做,而不是試圖理解一切是如何工作的...... –