簡體   English   中英

matplotlib不均勻的組大小條形圖並排

[英]matplotlib uneven group size bar charts side-by-side

我試圖繪制具有不同條形大小的數據組,並且可能具有不同的組大小。 如何將屬於同一組的條形圖(顯示為相同的顏色)分組,以便它們並排? (與類似,除了相同的顏色應該是並排的)

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

在此輸入圖像描述

使用類別之間的差距( 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()

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM