I have a csv like this:
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
I would like to draw a bar chart, which shows versions on x axis and an amount (number) of different colors on y axis.
So I want to group DataFrame
by version, check which colors belong to a particular version, count colors and display the results on the pygal bar chart.
It should look similar to this:
What I tried so far:
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')
Unfortunately, it does not work and can not match group of colors to proper version.
I also tried using matplotlib.pyplot
and it works like a charm:
pd.crosstab(df['version'],df['color']).plot.bar(ax=ax)
plt.draw()
This works as well:
df.groupby(['version','color']).size().unstack(fill_value=0).plot.bar()
But the generated chart is not accurate enough for me. I would like to have pygal chart.
I also checked:
How to plot pandas groupby values in a graph?
How to plot a pandas dataframe?