2016-11-18 19 views
2

私はPythonでコーディングし、Matplotlibをテストしています。私は2つの円グラフを持っているので、複数の凡例を追加しようとしています。これを使ってhttp://matplotlib.org/users/legend_guide.html私は2つの凡例を持っていますが、同じ色です。すべての円グラフで、私は2種類の色を持っているので、私は彼らの尊敬の伝説のために同じ色をしたい。複数の凡例からmatplotlibの円グラフまで?

また、凡例をウィンドウの境界に近づけることは可能ですか?伝説は長いフレーズであり、私は画面の中央に2番目の伝説を望んでいないので。

import matplotlib.pyplot as plt 
import matplotlib.patches as mpatches 

#def pieChart() 
# Data to plot 
labels = 'Participaram', 'Não participaram' 
sizes = [215, 130] 
colors = ['gold', 'yellowgreen'] 
explode = (0.1, 0) # explode 1st slice 

labels2 = 'Só visualizam dúvidas alheias', 'Mandaram e visualizam' 
sizes2 = [215, 130] 
colors2 = ['lightskyblue', 'lightcoral'] 
explode2 = (0, 0.08) # explode 1st slice 

# Plot 
plt.pie(sizes, explode=explode, colors=colors2,autopct='%1.1f%%', shadow=True, startangle=90, center = (-2,0)) 

plt.pie(sizes2, explode=explode2, colors=colors,autopct='%1.1f%%', shadow=True, startangle=45, center = (2,0)) 

first_legend = plt.legend(labels,loc = 2) 
ax = plt.gca().add_artist(first_legend) 
second_legend = plt.legend(labels2,loc = 4,ncol=1) 

plt.axis('equal') 
plt.show() 

​​

答えて

2

別に、すべての円グラフを描画するために、図に二つの軸を定義し、自動を調整使用tight_layout:オプションから

import matplotlib.pyplot as plt 
import matplotlib.patches as mpatches 

#def pieChart() 
# Data to plot 
labels = u'Participaram', u'Não participaram' 
sizes = [215, 130] 
colors = ['gold', 'yellowgreen'] 
explode = (0.1, 0) # explode 1st slice 

labels2 = [u'Só visualizam dúvidas alheias', u'Mandaram e visualizam'] 
sizes2 = [215, 130] 
colors2 = ['lightskyblue', 'lightcoral'] 
explode2 = (0, 0.08) # explode 1st slice 

fig = plt.figure() 
ax1 = fig.add_subplot(121) 
ax2 = fig.add_subplot(122) 

# Plot 
ax1.pie(sizes, explode=explode, colors=colors2, autopct='%1.1f%%', 
     shadow=True, startangle=90, center = (-2,0)) 

ax2.pie(sizes2, explode=explode2, colors=colors,autopct='%1.1f%%', 
     shadow=True, startangle=45, center = (2,0)) 

first_legend = ax1.legend(labels, loc = 2) 
second_legend = ax2.legend(labels2, loc = 2) 

ax1.axis('equal') 
ax2.axis('equal') 
plt.tight_layout() 
plt.show() 

0

アパートには、いくつかの描きます1つのサブプロットで作業することも、この例のように2つの凡例を定義して提供することもできます各パイから凡例までの各パッチ。あなたが後でそれらを手動で設定するようlegendlabelsが、この場合には空である凡例ラベル、あるとpctlabelsはパイに示すパーセント値(ある

patches, legendlabels   = plt.pie(data, ...) 
patches, legendlabels, pctlabels = plt.pie(data, ... , autopct='%1.1f%%' ...) 

として呼び出しから plt.pie()にパッチを入手します存在する場合)。

全体のコードは次のようになります。

import matplotlib.pyplot as plt 

labels = u'Participaram', u'Não participaram' 
sizes = [215, 130] 
colors = ['gold', 'yellowgreen'] 
explode = (0.1, 0) # explode 1st slice 

labels2 = u'Só visualizam dúvidas alheias', u'Mandaram e visualizam' 
sizes2 = [215, 130] 
colors2 = ['lightskyblue', 'lightcoral'] 
explode2 = (0, 0.08) # explode 1st slice 

# Plot 
slices1, legendlabels1, pctlabels1 = plt.pie(sizes, explode=explode, 
     colors=colors2,autopct='%1.1f%%', shadow=True, startangle=90, center = (-2,0)) 

slices2, legendlabels2, pctlabels2 = plt.pie(sizes2, explode=explode2, 
     colors=colors,autopct='%1.1f%%', shadow=True, startangle=45, center = (2,0)) 

# slices are the patches of the pie (cake pieces) 
# legendlabels are the legend labels, which are empty in this case, 
#  as you want to set them manually afterwards 
# pctlabels are the percentage values shown on the pie 

# now we provide the patches to the legend, such that each legend knows which patches to draw 
first_legend = plt.legend(slices1, labels, loc = 2) 
ax = plt.gca().add_artist(first_legend) 
second_legend = plt.legend(slices2, labels2, loc = 4) 

plt.axis('equal') 
plt.show() 

は、次のプロットを提供します。

enter image description here

関連する問題