3

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,

3 Answers 3

2

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()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @vestland. This was exactly what I was looking for. In my case because of the way I had my dataset I had to flatten the column names with this: df.reset_index(inplace=True)
@DanielBelémDuarte You are welcome! And thanks for the feedback!
1
import pandas as pd
import io
import plotly.express as px
import plotly.graph_objects as go

df = pd.read_csv(
    io.StringIO(
        """year,month,count
2020,1,50
2020,2,50
2020,3,50
2020,4,50
2020,11,100
2020,12,50
2021,1,80
2021,2,765
2021,3,100
2021,4,265
2021,5,500"""
    )
)

go.Figure(go.Bar(x=[df["month"].tolist(), df["year"].tolist()], y=df["count"]))

enter image description here

1 Comment

thank you @Rob Raymond. This is a great answer but I'n not marking as the correct answer as it is not creating different data series.
0

Using Plotly Express and updated with multi-categorical x-axis:

import pandas as pd
import io
import plotly.express as px

df = pd.read_csv(
    io.StringIO(
        """year,month,count
2020,1,50
2020,2,50
2020,3,50
2020,4,50
2020,11,100
2020,12,50
2021,1,80
2021,2,765
2021,3,100
2021,4,265
2021,5,500"""
    )
)

# convert year to string so you get a catergorical scale
df['year'] = df['year'].astype(str)

channel_top_Level = "year"
channel_2nd_Level = "month"

fig = px.bar(df, x = channel_2nd_Level, y = 'count', color = channel_top_Level)
for num,channel_top_Level_val in enumerate(df[channel_top_Level].unique()):
    temp_df = df.query(f"{channel_top_Level} == {channel_top_Level_val !r}")
    fig.data[num].x = [
            temp_df[channel_2nd_Level].tolist(), 
            temp_df[channel_top_Level].tolist() 
          ]
fig.layout.xaxis.title.text = f"{channel_top_Level} / { channel_2nd_Level}"
fig

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.