Home > Blockchain >  Create automatic bar chart subplots in Python
Create automatic bar chart subplots in Python

Time:07-01

I have a dataframe like so:

Date        ID  Data
2022-05-31  21  1
2022-05-31  22  4
2022-05-31  28  3
2022-06-01  21  2
2022-06-01  22  9
2022-06-01  28  2
2022-06-02  22  12
2022-06-03  22  10
2022-06-03  23  1
2022-07-03  25  1
2022-07-04  22  12
2022-07-04  23  6
2022-07-04  25  7
2022-08-05  22  12
2022-08-05  23  12
2022-08-05  25  11
2022-08-06  22  8
2022-08-06  23  11
2022-08-06  25  11

Here the x-axis is Date and y-axis is Data. The data is grouped by ID which would be the legend. I am able to create a stacked bar chart with ID as the color. However I would like create separate bar charts for each ID. In the example there are 5 unique IDs. So there would be a matrix of 5 bar charts.

Is there a way to do this automatically without splitting the dataset into groups and then plotting each ID individually?

CodePudding user response:

You can use groupby and plot the groups individually:

for key, grp in df.groupby('ID'):
    grp.plot.bar('Date', 'Data', title=key, legend=False)

For a fancier look with shared axes you can use a seaborn enter image description here

CodePudding user response:

Since you had plotly in the tags, plotly.express has the ability to automatically create faceted graphs. faceted graphs by ID. If you prefer two columns, set face_col_wrap=2. Also, the color bar is displayed, so I hide it. I also exclude unnecessary dates. I create an index of minimum to maximum dates and then create an exclusion list by deleting dates where data exists.

df.sort_values(['ID','Date'], ascending=[True,True], inplace=True)

import plotly.express as px
import pandas as pd

fig = px.bar(df, x='Date', y='Data', color='ID', facet_col='ID', facet_col_wrap=1)

fig.update_layout(autosize=False,
                  height=600,
                  width=800,
                  coloraxis_showscale=False
                 )

fig.update_xaxes(
    tickvals=df['Date'].unique(),
    rangebreaks=[
        dict(values=pd.date_range(df['Date'].min(), df['Date'].max()).drop(df['Date'].unique()), dvalue=86400000)
    ])

fig.show()

enter image description here

  • Related