6
votes

I'm trying to plot a panelplot with multiple boxplots from data in pandas dataframe. The columns of dataframe look like this:

 data.columns 
 Index([u'SiteId', u'obs1', u'obs2', u'obs3', u'obs4', u'obs5', u'obs6', u'date', u'area']

I want to create a panel of 9 different plots (since there are 9 distinct geographical areas) each of which has 12 boxplots for each month of the year. An example is shown below with the snippet used to create the plot:

df = data.ix[:, ['obs1','date', 'area']]
df = df.set_index('date')
colm = ['LOCATION 1']
for area in areas:
   df2 = df.loc[(df.area== area)]
   df2.boxplot(column=colm, by=df2.index.month, showmeans=True)

the above code results in the only one figure (with boxplots corresponding to each month in the figure), but I want to create 9 such plots each corresponding to a particular area as subplots in the same plot. In other words, I want to first group the data by area, then by month of the year and then plot the result as boxplot. Any thoughts how I can get the desired plots? Any help is appreciated.

Also, how can I get rid of the "Boxplot grouped by [1 1 1 ...12 12 12]" and "1,1,1,1,1,1,1,1,1,....." both at the top and bottom of the plot?

I can't post images since stackoverflow rules don't allow me to. Thanks.

1
if you could provide a simple dataframe with fake data to play around with, that would be useful. I suspect your 9 figures are indeed generate, but each one is overwritten by the next. You could try create a new figure in your for-loop, and you should get 9 different figures. If you want to plot all 9 areas in the same figure, you should create a 9x12 subplot and plot on the relevant axes in your for-loopDiziet Asahi
Thanks Diziet, I tried that but it doesn't work. Below is sample data (although it is just for a few days but the date covers entire year, and there are 9 such areas). : date obs1 area 2011-01-03 95.213458 4 2011-01-15 85.360990 5 2011-01-18 84.556772 5 2011-01-21 50.630212 6 2011-01-24 85.118985 3 2011-01-27 7.944901 1 2011-01-30 56.947048 3 2011-01-03 89.431410 6 2011-01-06 7.979408 6Vakratund

1 Answers

3
votes

Does this do what you want?

fig, axs = plt.subplots(len(areas), 1, figsize=(5,45))
for ax,area in zip(axs,areas):
    df2 = df.loc[(df.area==area)]
    df2.boxplot(column=['obs1'], by=df2.index.month, showmeans=True, ax=ax)