I'm trying to build a pygal bar chart from a pandas DataFrame series onto which .value_counts()
is applied e.g. without having to split the series into multiple columns and add those columns individually, as suggested in the documentation and this question.
import pandas as pd
import pygal
df = pd.DataFrame.from_dict({'categorical': ['foo','foo','bar','foo','bar','too']})
series = df.categorical.value_counts()
series
> foo 3
bar 2
too 1
Ideally the result looks like the plot in the solution to this question. Can this be done?
Thanks in advance!
I think that this is what you were aiming for:
Pygal doesn't isn't able to plot Pandas objects directly, but you can take advantage of the attributes of the Series
object returned by value_counts
to set the x labels of a chart and add the counts as a series of data.
The chart above was created with this code:
import pandas as pd
import pygal
df = pd.DataFrame.from_dict({'categorical': ['foo','foo','bar','foo','bar','too']})
series = df.categorical.value_counts()
chart = pygal.Bar(width=400, height=300)
chart.x_labels = series.index
chart.add("Count", series.values)
chart.render_to_png("bar.png") # Or any other output option.