2017-10-08 9 views
0

グループ化した後など、以下の例のようなシリーズが得られます。各バーの平均数値を表示したいと思います。下のコードは1つのエントリだけを示しています(もちろん、私には凡例が1つしかありません)。誰もがこれらの数字を表示するスマートな方法を提案できますか?パンダシリーズを反復して新しいチャートの凡例を作成する

%matplotlib inline 
import matplotlib.pyplot as plt 
import matplotlib 
matplotlib.style.use('ggplot') 
import pandas 

# create Series 
dict_ = {"Business" : 104.04,"Economy":67.04, "Markets":58.56, "Companies":38.48} 
s = pandas.Series(data=dict_) 

# plot it 
ax = s.plot(kind='bar', color='#43C6DB', stacked=True, figsize=(20, 10), legend=False) 
plt.tick_params(axis='both', which='major', labelsize=14) 
plt.xticks(rotation=30) #rotate labels 
# Shrink current axis by 20% 
box = ax.get_position() 
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height]) 
#create new legend 
legend = ['%s (%.1f a day)' %(i, row/7) for i, row in s.iteritems()] 
# Put the legend to the right of the current axis 
L = ax.legend(legend, loc='center left', bbox_to_anchor=(1, 0.5), fontsize=18) 
plt.show() 

enter image description here

答えて

1

伝説は、単一のエントリがあります。これは青い棒のハンドルです。したがって、ラベルを長いリストに設定しても、そのリストの最初の要素だけが既存のハンドルのラベルとして使用されます。

アイデアは、ラベル

legend = ['%s (%.1f a day)' %(i, row/7) for i, row in s.iteritems()] 
h,l = ax.get_legend_handles_labels() 
L = ax.legend(handles = h*len(legend), labels=legend, loc='center left', 
       bbox_to_anchor=(1, 0.5), fontsize=18) 

enter image description here

+0

美しいと同じ大きさに凡例のハンドルを複製することができます!ありがとうございました。 – aviss

関連する問題