Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Box Plot Trellis

Consider I have some data. Lets say it is weather data, of rainfall and temperature for each month. For this example, I will randomly generate is like so:

def rand_weather(n):
    month = n%12+1
    temp_ind = np.random.randint(0,4)
    temp = ["freezing", "cold", "moderate", "hot", "extreme"][temp_ind]
    rain = np.random.normal(50 - 4*temp_ind, 25) + np.random.randint(0,20)
    return month,rain, temp

data = [rand_weather(n) for n in range(3000)]
rain_record = pd.DataFrame(data, columns=["month", "rainfall", "temp"])

So the data looks something like:

    month   rainfall      temp
0       1  78.364133      cold
1       2  54.290201  freezing
2       3  81.341265      cold
3       4  98.980334       hot
...     ...    ...     ...
12      1  66.378066  moderate
13      2  44.264323  moderate
...     ...    ...     ...

I want to draw a Trellis chart of Box plots.


I can draw a Trellis Chart of the means like so:

avgs = rain_record.groupby(['temp','month']).mean()
avgs.reset_index(inplace=True) #Make the 'temp' and 'month' columns again

import pandas.tools.rplot as rplot
plt.figure(figsize=(12,6), dpi=20)
plt.title=pattern

plot = rplot.RPlot(avgs, y='rainfall', x='month')
plot.add(rplot.TrellisGrid(['temp', '.']))
plot.add(rplot.GeomScatter())
#plot.add(rplot.GeomPoint(size=80.0, alpha=0.5))
t=plot.render(plt.gcf())

trellis_of means


And I can draw a box plot of each 'temp' like so (for 'cold'):

rain_record[rain_record.temp=='cold'].boxplot(by='month')

box_plot

I could loop though each temp to generate as series of them. But the axis would not intrinsically line up, like they would in a Trellis. I guess the option exists to manaully setup matplotlibs axis, but I'm not sure of a nice way to do that.

like image 605
Lyndon White Avatar asked Aug 01 '14 02:08

Lyndon White


1 Answers

You could use seaborn, specifically the factorplot function:

import seaborn as sns
sns.set_style("whitegrid")

sns.factorplot("month", "rainfall", row="temp", data=rain_record,
               size=2, aspect=5, kind="box", palette="PuBuGn_d")
sns.despine(left=True)

enter image description here

like image 120
mwaskom Avatar answered Sep 18 '22 05:09

mwaskom