我有一个这样的csv:
name,version,color
AA,"version 1",yellow
BB,"version 2",black
CC,"version 3",yellow
DD,"version 1",black
AA,"version 1",green
BB,"version 2",green
FF,"version 3",green
GG,"version 3",red
BB,"version 3",yellow
BB,"version 2",red
BB,"version 1",black
我想画一个条形图,在x轴上显示版本,在y轴上显示不同颜色的数量。
DataFrame
按版本,检查属于特定版本的颜色,计数颜色并在上显示结果
pygal bar chart
.
它应该与此类似:
df = pd.read_csv(results)
new_df = df.groupby('version')['color'].value_counts()
bar_chart = pygal.Bar(width=1000, height=600,
legend_at_bottom=True, human_readable=True,
title='versions vs colors',
x_title='Version',
y_title='Number')
versions = []
for index, row in new_df.iteritems():
versions.append(index[0])
bar_chart.add(index[1], row)
bar_chart.x_labels = map(str, versions)
bar_chart.render_to_file('bar-chart.svg')
不幸的是,它不能工作,不能匹配的颜色组正确的版本。
matplotlib.pyplot
它就像一种魅力:
pd.crosstab(df['version'],df['color']).plot.bar(ax=ax)
plt.draw()
这同样有效:
df.groupby(['version','color']).size().unstack(fill_value=0).plot.bar()
但是生成的图表对我来说不够精确。我想要pygal图表。
我还检查了:
How to plot pandas groupby values in a graph?
How to plot a pandas dataframe?