0

Below are the pivot dataframes df1 and df2. Now I am trying to make subplots in plotly by using below dataframes. But I am getting key error while executing my code. My code as under:

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
pio.renderers.default='browser'

df1 = Student   July    August
      Bobby     824     516

df2 = Country   July    August
       EUR      274     150 
       USA      212     128 
       China    113     170 
       Port     44      10


# plotly setup for fig
fig = make_subplots(2,1)
fig.add_trace(go.Bar(x=df1.Student, y=df1.loc['July','August']),row=1, col=1)

fig.add_trace(go.Bar(x=df2.Country, y=df2.loc['July','August']),row=2, col=1)
fig.show()
John83
  • 97
  • 1
  • 8
  • @John82 Please share an ***executable*** code snippet. Because of the way you've tried to set up your dataframes your current snippet will raise a `SyntaxError`. Have a look [here](https://stackoverflow.com/questions/63163251/pandas-how-to-easily-share-a-sample-dataframe-using-df-to-dict/63163254#63163254) on how to share a sample of your dataframe successfully. – vestland Jun 29 '21 at 19:46

1 Answers1

1
  • you are using plotly express concepts to create the traces. You need to create a trace for each bar column
  • simple case of then adding that to the subplots figure
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
import numpy as np

s = 200
# generate pivot table dataframes similar to question
df = pd.DataFrame({"Student": np.random.choice(["Bob", "John", "Fred", "Jill", "Anne"], s),
              "Month": np.random.choice(
                  pd.Series(pd.date_range("31-Jan-2020", freq="M", periods=6)).dt.strftime("%B"),s),
              "score": np.random.randint(5, 75, s),
    })

df1 = df.groupby(["Student","Month"], as_index=False).agg({"score":"mean"}).pivot(index="Student", columns="Month", values="score")
df = pd.DataFrame({"Country": np.random.choice(["UK", "USA", "France", "Germany", "Mexico"], s),
              "Month": np.random.choice(
                  pd.Series(pd.date_range("31-Jan-2020", freq="M", periods=6)).dt.strftime("%B"),s),
              "score": np.random.randint(5, 75, s),
    })
df2 = df.groupby(["Country","Month"], as_index=False).agg({"score":"mean"}).pivot(index="Country", columns="Month", values="score")


# create figure
fig = make_subplots(rows=2, cols=1, specs=[[{"type":"bar"}],[{"type":"bar"}]])
# add traces
for col in ["April","May"]:
    fig.add_trace(go.Bar(x=df1.index, y=df1[col], name=col), row=1, col=1)
for col in ["April","May"]:
    fig.add_trace(go.Bar(x=df2.index, y=df2[col], name=col), row=2, col=1)

    
fig

enter image description here

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30