考虑一下我有一些数据。 让我们说这是每个月的降雨量和温度的天气数据。 对于这个例子,我将随机生成如下:
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"])
所以数据看起来像:
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
... ... ... ...
我可以像这样绘制格子图表:
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())
我可以像这样绘制每个'temp'
的方框图(对于'冷'):
rain_record[rain_record.temp=='cold'].boxplot(by='month')
我可以循环每个temp来生成它们的系列。 但是轴线本身并不会像在Trellis中一样排列。 我想这个选项存在于manaully设置matplotlibs轴, 但我不确定这样做的好方法。
答案 0 :(得分: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)