2017-01-26 7 views
1

異なる棒のサイズを持ち、グループサイズが異なる可能性があるデータのグループをプロットしようとしています。同じグループに属しているバー(同じ色で表示されているもの)を並べてグループ化するにはどうすればいいですか? (thisと同様に、同一の色がサイド・バイ・サイドであるべきである以外は)matplotlib不均一なグループサイズの横棒グラフ横並び

width = 0.50  
groupgap=2 
y1=[20,80] 
y2=[60,30,10] 
x1 = np.arange(len(y1)) 
x2 = np.arange(len(y2))+groupgap 
ind = np.concatenate((x1,x2)) 
fig, ax = plt.subplots() 
rects1 = ax.bar(x1, y1, width, color='r', ecolor= "black",label="Gender") 
rects2 = ax.bar(x2, y2, width, color='b', ecolor= "black",label="Type") 
ax.set_ylabel('Population',fontsize=14) 
ax.set_xticks(ind) 
ax.set_xticklabels(('Male', 'Female','Student', 'Faculty','Others'),fontsize=14) 
ax.legend() 

enter image description here

答えて

3

カテゴリ(groupgap)との間のギャップを使用するという考えは、移動するための方法は確かです。あなただけのようにも最初のグループの長さを追加する必要があります。ここでは

x2 = np.arange(len(y2))+groupgap+len(y1) 

は私がgroupgap=1を使用し、完全な例です:

import matplotlib.pyplot as plt 
import numpy as np 

width = 1  
groupgap=1 
y1=[20,80] 
y2=[60,30,10] 
x1 = np.arange(len(y1)) 
x2 = np.arange(len(y2))+groupgap+len(y1) 
ind = np.concatenate((x1,x2)) 
fig, ax = plt.subplots() 
rects1 = ax.bar(x1, y1, width, color='r', edgecolor= "black",label="Gender") 
rects2 = ax.bar(x2, y2, width, color='b', edgecolor= "black",label="Type") 
ax.set_ylabel('Population',fontsize=14) 
ax.set_xticks(ind) 
ax.set_xticklabels(('Male', 'Female','Student', 'Faculty','Others'),fontsize=14) 
plt.show() 

enter image description here

関連する問題