Matplotlib 直方图不等于数据集
Matplotlib Histogram not equal data sets
我想创建一个将使用以下内容的直方图。我知道这是因为我的 menMeans 和 womenMeans 的长度不相等。如果我没有对列表进行硬编码,并且可能想稍后添加更多列表以提供更多条形图,我该怎么做?了解条形图不会总是具有相似值集的情况下缩放图形的最佳方法是什么。
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
## the data
N = 5
menMeans = [18, 35, 30, 35, 27] ### len=5
womenMeans = [25, 32, 34, 20, 25,42] ### len =6
## necessary variables
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars
## the bars
rects1 = ax.bar(ind, menMeans, width,
color='black')
rects2 = ax.bar(ind+width, womenMeans, width,
color='red')
# axes and labels
ax.set_xlim(-width,len(ind)+width)
ax.set_ylim(0,45)
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
xTickMarks = ['Group'+str(i) for i in range(1,7)]
ax.set_xticks(ind+width)
xtickNames = ax.set_xticklabels(xTickMarks)
plt.setp(xtickNames, rotation=45, fontsize=10)
## add a legend
ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )
plt.show()
我得到的错误是:
Traceback (most recent call last):
File "C:\Python27\test_3.py", line 22, in <module>
color='red')
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 4999, in bar
nbars)
AssertionError: incompatible sizes: argument 'height' must be length 5 or scalar
我认为解决这个问题的最简单方法是向男性列表添加一个或多个零均值,直到它与另一个的长度相同。额外的零意味着不改变图表的外观 - 它看起来就像是不存在的栏:
这是一个简单的通用函数:
def equalizeLists(*lists):
maxLen = max([len(list) for list in lists])
for list in lists:
list = list.extend([0]*(maxLen - len(list)))
return maxLen
这将通过在较短列表的末尾添加零来自动均衡两个或多个列表的长度。您可以像这样将它插入到您的代码中:
## the data
menMeans = [18, 35, 30, 35, 27]
womenMeans = [25, 32, 34, 20, 25,42]
N = equalizeLists(menMeans, womenMeans)
我想创建一个将使用以下内容的直方图。我知道这是因为我的 menMeans 和 womenMeans 的长度不相等。如果我没有对列表进行硬编码,并且可能想稍后添加更多列表以提供更多条形图,我该怎么做?了解条形图不会总是具有相似值集的情况下缩放图形的最佳方法是什么。
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
## the data
N = 5
menMeans = [18, 35, 30, 35, 27] ### len=5
womenMeans = [25, 32, 34, 20, 25,42] ### len =6
## necessary variables
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars
## the bars
rects1 = ax.bar(ind, menMeans, width,
color='black')
rects2 = ax.bar(ind+width, womenMeans, width,
color='red')
# axes and labels
ax.set_xlim(-width,len(ind)+width)
ax.set_ylim(0,45)
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
xTickMarks = ['Group'+str(i) for i in range(1,7)]
ax.set_xticks(ind+width)
xtickNames = ax.set_xticklabels(xTickMarks)
plt.setp(xtickNames, rotation=45, fontsize=10)
## add a legend
ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )
plt.show()
我得到的错误是:
Traceback (most recent call last):
File "C:\Python27\test_3.py", line 22, in <module>
color='red')
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 4999, in bar
nbars)
AssertionError: incompatible sizes: argument 'height' must be length 5 or scalar
我认为解决这个问题的最简单方法是向男性列表添加一个或多个零均值,直到它与另一个的长度相同。额外的零意味着不改变图表的外观 - 它看起来就像是不存在的栏:
这是一个简单的通用函数:
def equalizeLists(*lists):
maxLen = max([len(list) for list in lists])
for list in lists:
list = list.extend([0]*(maxLen - len(list)))
return maxLen
这将通过在较短列表的末尾添加零来自动均衡两个或多个列表的长度。您可以像这样将它插入到您的代码中:
## the data
menMeans = [18, 35, 30, 35, 27]
womenMeans = [25, 32, 34, 20, 25,42]
N = equalizeLists(menMeans, womenMeans)