2017-04-10 52 views
4

我目前正在處理matplotlib一些奇怪的行爲時,繪製多個錯誤與不同的y軸成一個子圖。Matplotlib多個網格zorder不工作與錯誤欄

當這樣做時,對應於第二個誤差線圖的網格總是與第一個誤差線圖重疊。通過zorder選項,我只能將其他網格移動到錯誤欄下方。

我希望兩個網格都低於兩個錯誤條。 有沒有人知道這個解決方案,或者這是一個matplotlib中的錯誤?

我使用Python 2.7.12和matplotlib 1.5.1。

最小工作示例:

import numpy as np 
import matplotlib 
matplotlib.use('Agg') 
import matplotlib.pyplot as plt 
import matplotlib.colors as colors 

# numbers of epochs 
ttimes_means_list = [251.4, 153.3, 22.0, 202.1, 46.6] 
ttimes_stddevs_list = [32.1, 35.1, 12.0, 84.9, 14.7] 
# numbers of times 
times_means_list = [5231, 3167, 860, 3932, 1244] 
times_stddevs_list = [1381, 572, 253, 1445, 215] 

labels = ['x1', 'x2', 'x3', 'x4', 'x5'] 
linewidth=3 

xvalues = np.arange(0, len(times_means_list), 1) 
xvalues_s = xvalues + 0.1 

fig = plt.figure() 
ax1 = fig.add_subplot(111) 
ax2 = ax1.twinx() 
ax1.set_ylabel('Number') 
ax1.xaxis.grid(False) 
ax1.yaxis.grid(True, color='navy', linewidth=linewidth/2.0) 
ax2.xaxis.grid(False) 
ax1.set_zorder(0) 
ax2.set_zorder(0) 
ax2.yaxis.grid(True, color='darkorange', linewidth=linewidth/2.0) 
ax2.set_ylabel('Time') 
ax1.set_xticks(xvalues) 
ax2.set_xticks(xvalues) 
ax1.set_xticklabels(labels) 
ax2.set_xticklabels(labels) 
ax1.set_xlabel('x label') 
errplot1 = ax1.errorbar(xvalues, ttimes_means_list, yerr=ttimes_stddevs_list, 
     linestyle="None", elinewidth=linewidth, color='navy', label='Number', 
     capthick=linewidth, zorder=10) 
errplot2 = ax2.errorbar(xvalues_s, times_means_list, yerr=times_stddevs_list, 
     linestyle="None", elinewidth=linewidth, color='darkorange', 
     label='Time', capthick=linewidth, zorder=10) 
ax1.set_axisbelow(True) 
ax2.set_axisbelow(True) 
fig.legend((errplot1, errplot2), ('number', 'time'), 
     loc='upper right', numpoints=1) 
plt.xlim(-0.5, len(times_means_list)-0.5) 
plt.title('Some plot title') 
plt.savefig('mwe_plot.pdf') 
plt.clf() 

輸出曲線(橙色格子點重疊的藍色條): Multiple errorbars with multiple grids

回答

1

也許可以得出「海軍」線的兩倍。

import numpy as np 
import matplotlib 
matplotlib.use('Agg') 
import matplotlib.pyplot as plt 
import matplotlib.colors as colors 

# numbers of epochs 
ttimes_means_list = [251.4, 153.3, 22.0, 202.1, 46.6] 
ttimes_stddevs_list = [32.1, 35.1, 12.0, 84.9, 14.7] 
# numbers of times 
times_means_list = [5231, 3167, 860, 3932, 1244] 
times_stddevs_list = [1381, 572, 253, 1445, 215] 

labels = ['x1', 'x2', 'x3', 'x4', 'x5'] 
linewidth=3 

xvalues = np.arange(0, len(times_means_list), 1) 
xvalues_s = xvalues + 0.1 

fig = plt.figure() 
ax1 = fig.add_subplot(111) 
ax2 = ax1.twinx() 
ax1.set_ylabel('Number') 
ax1.xaxis.grid(False) 
g1 = ax1.yaxis.grid(True, color='navy', linewidth=linewidth/2.0) 
ax2.xaxis.grid(False) 
ax1.set_zorder(0) 
ax2.set_zorder(0) 
g2 = ax2.yaxis.grid(True, color='darkorange', linewidth=linewidth/2.0) 
ax2.set_ylabel('Time') 
ax1.set_xticks(xvalues) 
ax2.set_xticks(xvalues) 
ax1.set_xticklabels(labels) 
ax2.set_xticklabels(labels) 
ax1.set_xlabel('x label') 
ax1.errorbar(xvalues, ttimes_means_list, yerr=ttimes_stddevs_list, 
     linestyle="None", elinewidth=1, color='navy', label='Number', 
     capthick=1, zorder=10,) 
errplot2 = ax2.errorbar(xvalues_s, times_means_list, yerr=times_stddevs_list, 
     linestyle="None", elinewidth=linewidth, color='darkorange', 
     label='Time', capthick=linewidth, zorder=10) 
ax1.set_axisbelow(True) 
ax2.set_axisbelow(True) 

# draw the 'navy' line again 
ax3 = ax1.twinx() 
ax3.yaxis.grid(False) 
errplot1 = ax3.errorbar(xvalues, ttimes_means_list, yerr=ttimes_stddevs_list, 
     linestyle="None", elinewidth=linewidth, color='navy', label='Number', 
     capthick=linewidth, zorder=10) 

fig.legend((errplot1, errplot2), ('number', 'time'), 
     loc='upper right', numpoints=1) 
plt.xlim(-0.5, len(times_means_list)-0.5) 
plt.title('Some plot title') 
# plt.savefig('mwe_plot.pdf') 
# plt.clf() 

plt.show() 

,你會得到: enter image description here

+1

非常感謝!事實上,這解決了這個問題。我剛剛添加了'ax3.get_yaxis()。set_visible(False)'以擺脫右側的附加標籤。 – ml4294

+0

@ ml4294謝謝你的改進! – xiaoyi