0
votes

I am trying to create 12 boxplots of panda df's by the column = 'Var' and by = 'plant_name' from my dataframe (df3). I need to create a boxplot for of the column 'Var' and by = 'plant_name" for each of the 12 months of the year Jan - Dec. I have code below that plots 12 plots - one for each month from my list of months =['Jan','Feb','Mar',...'Dec']. However, my code is plotting all the months data in the same boxplot 12 times instead of 12 unique months data from the 'Var' column in df3.

My code:

months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nove','Dec']
for column in months:
    plt.figure()
    df3.boxplot(by='plant_name',column='Var')
    plt.xticks(rotation=90, ha='right')

df3.head(5)
Out[32]: 
  plant_name  month  year     power_kwh  power_kwh_rhs   Var
0  BII NEE STIPA      1  1991  11905.826075   14673.281223 -18.9
1  BII NEE STIPA      1  1992  14273.927688   14673.281223  -2.7
2  BII NEE STIPA      1  1993  12559.828360   14673.281223 -14.4
3  BII NEE STIPA      1  1994  14627.635081   14673.281223  -0.3
4  BII NEE STIPA      1  1995  13715.054435   14673.281223  -6.5

and the end of df3 -

Out[33]: 
plant_name  month  year     power_kwh  power_kwh_rhs   Var
2875    VENTOSA     12  2016  45840.068414   48876.289265  -6.2
2876    VENTOSA     12  2017  59288.158871   48876.289265  21.3
2877    VENTOSA     12  2018  44438.768683   48876.289265  -9.1
2878    VENTOSA     12  2019  47002.390188   48876.289265  -3.8
2879    VENTOSA     12  2020  52101.279839   48876.289265   6.6
2

2 Answers

1
votes

Try with:

months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nove','Dec']
for i, month in enumerate(months,1):
    plt.figure()
    df3[df3['month']==i].boxplot(by='plant_name',column='Var')
    plt.xticks(rotation=90, ha='right')
0
votes

Try this:

fig, ax = plt.subplots(len(months))
for i, month in enumerate(months):
    df3[df3['month']==i].boxplot(by='plant_name',column='Var', ax=ax[i])
    ax.xticks(rotation=90, ha='right')

If you want a "2-D" array of boxplots, then use something like fig, ax = plt.subplots(4,3) and then use a double loop