【问题标题】:Box Plot Trellis箱形图格子
【发布时间】:2014-09-24 04:11:49
【问题描述】:

假设我有一些数据。 可以说这是天气数据,每个月的降雨量和温度。 对于这个例子,我会随机生成是这样的:

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
...     ...    ...     ...

我要绘制Box plotsTrellis 图表。


我可以像这样绘制一个网格图:

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 以生成一系列它们。 但是轴本质上不会像格子一样排列。 我想存在手动设置 matplotlibs 轴的选项, 但我不确定这样做的好方法。

【问题讨论】:

    标签: python matplotlib plot pandas boxplot


    【解决方案1】:

    您可以使用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)
    

    【讨论】:

    • 确实,扩展的科学 python 生态系统是巨大的,并且是相互关联的。
    • 有没有办法使绘图属性的 X 位置与值相关?假设我只在第 3、6 和 12 个月进行读数,我希望 6 和 16 之间的差距大于 3 和 6 之间的差距。
    • 不,factorplot 始终假定 x 轴上的值是分类的(即使它们看起来是定量的)。
    猜你喜欢
    • 1970-01-01
    • 2010-12-03
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    相关资源
    最近更新 更多