Box Plot Trellis

Lyn*_*ite 5 python plot matplotlib boxplot pandas

考虑一下我有一些数据.让我们说这是每个月的降雨量和温度的天气数据.对于这个例子,我将随机生成如下:

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"])
Run Code Online (Sandbox Code Playgroud)

所以数据看起来像:

    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
...     ...    ...     ...
Run Code Online (Sandbox Code Playgroud)

我想画一个Box图Trellis图表.


我可以绘制这样的平均图表:

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())
Run Code Online (Sandbox Code Playgroud)

trellis_of意思是


我可以画一个每个'temp'像这样的盒子图(对于'冷'):

rain_record[rain_record.temp=='cold'].boxplot(by='month')
Run Code Online (Sandbox Code Playgroud)

箱形图

我可以循环每个临时生成它们的系列.但是轴线本身并不会像在Trellis中一样排列.我想这个选项存在于manaully设置matplotlibs轴,但我不确定一个很好的方法来做到这一点.

mwa*_*kom 9

你可以使用seaborn,特别是factorplot功能:

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)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述