Search code examples
pythonpandasplotlyplotly-python

Bar chart with 2 data series with Pandas Dataframe and Plotly


I have a dataframe with the following data (example):

year month count
2020 11 100
12 50
2021 01 80
02 765
03 100
04 265
05 500

I would like to plot this with plotly on a bar chart where I would have 2 vertical bars for each month, one for 2020 and another for 2021. I would like the axis to be defined automatically based on the existing values on the dataset which could change. today is only for year 2020 and 2021 but it could be different.

I have searched for information but is always mentioning hardcoded dataset series names and data and I'm not understanding how I could dynamically input these in ploty.

I was expecting something like this but it is not working:

import plotly.express as px

...

px.bar(df, x=['year','month'], y='count')
fig.show()

Thank you,


Solution

  • To get two vertical bar for each month, I'm guessing the traces should represent each individual year. In that case you can use:

    for y in df.year.unique():
        dfy = df[df.year == y]
        fig.add_bar(x = dfy.month, y = dfy.value, name = str(y))
    

    Plot 1

    enter image description here

    That's the result for your limited dataset, though. If you expand the dataset a bit you'll get a better impression of how it will look:

    Plot 2

    enter image description here

    Complete code:

    import plotly.graph_objects as go
    import pandas as pd
    
    df = pd.DataFrame({'year': {0: 2020, 1: 2020, 2: 2021, 3: 2021, 4: 2021, 5: 2021, 6: 2021},
                         'month': {0: 11, 1: 12, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5},
                         'value': {0: 100, 1: 50, 2: 80, 3: 765, 4: 100, 5: 265, 6: 500}})
    
    df = pd.DataFrame({'year': {0: 2020,
                      1: 2020,
                      2: 2020,
                      3: 2020,
                      4: 2020,
                      5: 2020,
                      6: 2020,
                      7: 2020,
                      8: 2020,
                      9: 2020,
                      10: 2020,
                      11: 2020,
                      12: 2021,
                      13: 2021,
                      14: 2021,
                      15: 2021,
                      16: 2021,
                      17: 2021,
                      18: 2021,
                      19: 2021,
                      20: 2021,
                      21: 2021,
                      22: 2021,
                      23: 2021},
                     'month': {0: 1,
                      1: 2,
                      2: 3,
                      3: 4,
                      4: 5,
                      5: 6,
                      6: 7,
                      7: 8,
                      8: 9,
                      9: 10,
                      10: 11,
                      11: 12,
                      12: 1,
                      13: 2,
                      14: 3,
                      15: 4,
                      16: 5,
                      17: 6,
                      18: 7,
                      19: 8,
                      20: 9,
                      21: 10,
                      22: 11,
                      23: 12},
                     'value': {0: 100,
                      1: 50,
                      2: 265,
                      3: 500,
                      4: 80,
                      5: 765,
                      6: 100,
                      7: 265,
                      8: 500,
                      9: 80,
                      10: 765,
                      11: 100,
                      12: 80,
                      13: 765,
                      14: 100,
                      15: 265,
                      16: 500,
                      17: 80,
                      18: 765,
                      19: 100,
                      20: 265,
                      21: 500,
                      22: 80,
                      23: 765}})
    
    fig = go.Figure()
    for y in df.year.unique():
        dfy = df[df.year == y]
        fig.add_bar(x = dfy.month, y = dfy.value, name = str(y))
    
    fig.show()