我正试图使用来自this site的学院记分卡数据在MatPlotLib中创建一个100%堆积条形图。
共有38列: [在此插入学习领域]授予学位的百分比这解释了为什么有38个领域!
我有一个学校的子集,我想做这个堆积图。
我试着按照指示行事。对。这是很长的代码,但我想按本子来演。(加上我在这个博客上一直很走运) 数据来自这些PCIP(按研究领域授予学位的百分比),以百分比的形式出现,所以我不必遵循克里斯的计算,因为他们已经完成了。
运行代码时出现错误:
bar_width = 1
bar_l = [i for i in range(len(df['PCIP01']))]
tick_pos = [i+(bar_width/2) for i in bar_l]
# Create a figure with a single subplot
f, ax = plt.subplots(1, figsize=(10,5))
ax.bar(bar_l,
degrees.PCIP01,
label='PCIP01',
alpha=0.9,
color='#2D014B',
width=bar_width
)
ax.bar(bar_l,
PCIP04,
label='PCIP04',
alpha=0.9,
color='#28024E',
width=bar_width
)
[在剩下的36块地里,以此类推
# Set the ticks to be School names
plt.xticks(tick_pos, degrees['INSTNM'])
ax.set_ylabel("Percentage")
ax.set_xlabel("")
# Let the borders of the graphic
plt.xlim([min(tick_pos)-bar_width, max(tick_pos)+bar_width])
plt.ylim(-10, 110)
# rotate axis labels
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
# shot plot
这就是我收到的错误:
ValueError Traceback (most recent call last)
<ipython-input-91-019d33be36c2> in <module>()
7 alpha=0.9,
8 color='#2D014B',
----> 9 width=bar_width
10 )
11 ax.bar(bar_l,
C:\Users\MYLOCATION\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
1889 warnings.warn(msg % (label_namer, func.__name__),
1890 RuntimeWarning, stacklevel=2)
-> 1891 return func(ax, *args, **kwargs)
1892 pre_doc = inner.__doc__
1893 if pre_doc is None:
C:\Users\MYLOCATION\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in bar(self, left, height, width, bottom, **kwargs)
2077 if len(height) != nbars:
2078 raise ValueError("incompatible sizes: argument 'height' "
-> 2079 "must be length %d or scalar" % nbars)
2080 if len(width) != nbars:
2081 raise ValueError("incompatible sizes: argument 'width' "
ValueError: incompatible sizes: argument 'height' must be length 38678 or scalar
有谁能帮助我简化这些代码,以便我可以创建这个堆积的100%条形图?
首先,在这个数据集中有很多大学,也许堆积条形图不是最好的方法?
无论如何,你可以循环浏览每种类型的学位并添加另一个栏。要创建堆积条形图,只需更改每个条形图的底部位置。
您可以修改此代码以获得所需的内容(例如,您似乎需要百分比而不是分数,因此只需将每个度数列乘以100即可)。为了测试,我选择了前10所大学,结果是:
有了10所大学,这已经是一个相当繁忙的情节——有了100所大学,这几乎是不可读的:
我可以保证,有了近8000所大学,这个堆积如山的条形图将完全不可读。或许可以考虑用另一种方式来表示数据?
相关问题 更多 >
编程相关推荐