2016-08-11 8 views
1

私はmatplotlibの棒グラフに対して頭を叩いています。 男性の前に女性が表示されるように、ここでバーの順序を変更するにはどうすればよいですか?matplotlibチャートの系列順序を決定する

#!/usr/bin/env python 
# a bar plot with errorbars 
import numpy as np 
import matplotlib.pyplot as plt 

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) 

womenMeans = (25, 32, 34, 20, 25) 
womenStd = (3, 5, 2, 3, 3) 
rects2 = ax.bar(ind + width, womenMeans, width, color='y', yerr=womenStd) 

# add some text for labels, title and axes ticks 
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')) 


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() 

chart

matplotlibのは、アルファベット順にデータを並べ替えを主張するように私には思えます。これを避ける方法はありますか?

答えて

0

ソリューションは非常に簡単です、順序はxlocationです:

rects2 = ax.bar(ind - width, womenMeans, width, color='y', yerr=womenStd) 

これは最初の前に、第2シリーズのバーをプロットします。

関連する問題