I have table looking like this:
id | year | value |
---|---|---|
98 | 2001 | 4 |
252 | 2011 | 5 |
198 | 2021 | 6 |
232 | 2001 | 7 |
981 | 2011 | 4 |
244 | 2021 | 2 |
I would like to do a grouped bar chart, in which the bars are grouped according to the year and and each group shows the values of the id the respective year. So basically many bar charts plotted into one big plot.
I tried pandas.groupby, but it just gives me several subplots. And with pandas.pivot I get the error: Index contains duplicate entries, cannot reshape.
Further it is possible to show mean and variance over each group (year)? Is there a smart way of doing it?
CodePudding user response:
Grouped chart can be done via seaborn, plotly visualization packages. I prefer plotly cause its interactive plot and lot more customizable. Below is the code per your question.
Where data.csv contains the data you have mentioned in tabular format.
import pandas as pd
import plotly.express as px
df = pd.read_csv('data.csv')
df['id'] = df['id'].astype(object)
fig = px.bar(df, x="year",color='id',
y=['value'],
title="A Grouped Bar Chart With Plotly Express in Python",
barmode='group', text_auto=True,
height=400,
width=600,
)
fig.update_traces(textposition='outside')
fig.show()
[![Grouped Bar Chart Plot][1]][1]
[1]: https://i.stack.imgur.com/bHn4x.png