【问题标题】:How to plot variables separately with FacetGrid in Seaborn如何在 Seaborn 中使用 FacetGrid 分别绘制变量
【发布时间】:2018-11-28 07:01:22
【问题描述】:

我想分别绘制不同的变量(作为小倍数),看看它们中的任何一个是否有任何重要的趋势。假设我正在跟踪各种蜥蜴的健康统计数据,我想要一个单独的体重和长度图表,看看是否有任何突然变化。这是我的数据:

month   hand    color   weight  length
1       left    blue    123.16  13.9
1       left    red     125.62  12.84
1       right   blue    186.46  7.18
1       right   red     152.3   7.51
2       left    blue    4465    187.77
2       left    red     116.27  10.6
2       right   blue    189.13  14.67
2       right   red     82.78   14.18
3       left    blue    124.85  13.25
3       left    red     178.51  8.33
3       right   blue    98.88   10.68
3       right   red     142.87  5.91

我想要两张图,一张表示重量,一张表示长度,每张图有四条线(用于右撇子蓝蜥蜴、右撇子红蜥蜴等)。

我认为这算作微小数据,FacetGrid docs 说是必需的,因为每一行都是“观察”,因为同时测量了重量和长度。

FacetGrid 似乎设置为根据变量的制作不同的图表。从文档中的这个示例中可以清楚地看出这一点:

>>> import matplotlib.pyplot as plt
>>> g = sns.FacetGrid(tips, col="time",  row="smoker")
>>> g = g.map(plt.hist, "total_bill")

有没有什么方法可以指定一个变量/列的列表来代替它用于每个图表?还是我必须使用其他方法?

【问题讨论】:

    标签: python pandas matplotlib seaborn


    【解决方案1】:

    您可以添加一个新列,聚合手和颜色列

    df["handcolor"] = df["hand"] + df["color"]
    

    那看来你要画折线图了。

    例如

    fig, (ax1,ax2) = plt.subplots(ncols=2)
    ax1.set_title("weight")
    ax2.set_title("length")
    for n,grp in df.groupby("handcolor"):
        ax1.plot(grp.month, grp.weight)
        ax2.plot(grp.month, grp.length)
    

    【讨论】:

      猜你喜欢
      • 2018-08-26
      • 2016-11-09
      • 1970-01-01
      • 2018-12-09
      • 2014-10-31
      • 2015-06-17
      • 2020-09-26
      • 1970-01-01
      • 2014-09-12
      相关资源
      最近更新 更多