2016-04-13 26 views
0

我使用matploitlib箱線,以創建像下面這樣的數字:的Python:在箱線圖一箱的設置背景

enter image description here

使用此代碼:

# Create a figure instance 
fig = plt.figure(1, figsize=(4, 4)) 

# Create an axes instance 
ax = fig.add_subplot(111) 
ax.set_ylim([0, 1.2]) 

# Create the boxplot 
bp = ax.boxplot(data_to_plot,widths=(0.5, 0.5)) 
plt.setp(bp['boxes'], linewidth=1) 

# Save the figure 
plt.xticks([1, 2], ['A', 'B'],fontsize=16) 
plt.yticks(fontsize=16) 
#fig.savefig('fig1.png', bbox_inches='tight') 
plt.show() 

我試圖要着手爲每個盒子設置背景,以便它們具有不同的顏色。

感謝

+0

這裏看一個例子:http://matplotlib.org/examples /pylab_examples/boxplot_demo2.html – roadrunner66

回答

0

我最終加入以下參數箱線圖:

bp = ax.boxplot(data_to_plot,widths=(0.5, 0.5),patch_artist=True) 

,並指定每個箱體採用獨特的顏色:

bp['boxes'][0].set(facecolor = '#1b9e77') 
1

這只是部分做你想要什麼,但也許它可以幫助你找到答案,你的問題。你可以使用

bp = ax.boxplot(data_to_plot,widths=(0.5, 0.5),notch=True, patch_artist=True) 

來填補陰謀。爲了改變顏色請看這Example

+0

是的,添加patch_artist幫助我解決了我的問題。 – ahajib