【问题标题】:Plotting a line plot with error bars and datapoints from a pandas DataFrame绘制带有来自 pandas DataFrame 的误差线和数据点的线图
【发布时间】:2014-10-17 00:41:29
【问题描述】:

我一直在绞尽脑汁想弄清楚如何以我想要的方式绘制 pandas DataFrame,但无济于事。

DataFrame 有一个 MultiIndex,它看起来像这样:

+-----------+--------------+------------+--------------+-----------------+---------+---------+---------+---------+---------+
|           |              |            |              |                 | run_001 | run_002 | run_003 | run_004 | run_005 |
+-----------+--------------+------------+--------------+-----------------+---------+---------+---------+---------+---------+
| file_type | server_count | file_count | thread_count | cacheclear_type |         |         |         |         |         |
+-----------+--------------+------------+--------------+-----------------+---------+---------+---------+---------+---------+
| gor       | 01servers    | 05files    | 20threads    | ccALWAYS        | 15.918  | 16.275  | 15.807  | 17.781  | 16.233  |
| gor       | 01servers    | 10files    | 20threads    | ccALWAYS        | 17.322  | 17.636  | 16.096  | 16.484  | 16.715  |
| gor       | 01servers    | 15files    | 20threads    | ccALWAYS        | 19.265  | 17.128  | 17.630  | 18.739  | 16.833  |
| gor       | 01servers    | 20files    | 20threads    | ccALWAYS        | 23.744  | 20.539  | 21.416  | 22.921  | 22.794  |
+-----------+--------------+------------+--------------+-----------------+---------+---------+---------+---------+---------+

我想要做的是绘制一个折线图,其中 x 值是“file_count”值,每个 y 值是 DataFrame 中相应行的所有 run_xxx 值的平均值。

如果可能的话,我想添加误差线,甚至是数据点本身,这样我就可以看到平均值背后的数据分布。

这是一个(蹩脚的)大体上我在说什么的模型:

我已经能够使用内置在 pandas 的 DataFrame 中的 boxplot() 函数创建一个箱线图:

df.transpose().boxplot()

这看起来几乎没问题,但有点混乱,并且没有绘制实际的数据点。

【问题讨论】:

    标签: python matplotlib pandas plot


    【解决方案1】:

    Beeswarm plot 在这种情况下会非常好,特别是当你有很多点以及显示这些点的分布时。但是,您需要将 position 参数提供给 beeswarm,因为默认情况下它将从 0 开始。另一方面,pandas DataFrameboxplot 方法在 x = 1 处绘制框, 2 ...

    归结为以下几点:

    from beeswarm import *
    D1 = beeswarm(df.values, positions = np.arange(len(df.values))+1)
    D2 = df.transpose().boxplot(ax=D1[1])
    

    【讨论】:

    • 谢谢,这绝对有帮助,尽管我今天确实设法摆脱了这个问题。我会接受你的回答,因为它确实回答了我的问题,我很感激你的努力。
    【解决方案2】:

    为了完整起见,我将在此处包括我最终设法做到这一点的方式:

    import numpy as np
    import matplotlib.pyplot as plt
    import random
    
    dft = df.sortlevel(2).transpose()
    
    fig, ax = plt.subplots()
    
    x = []
    y = []
    y_err = []
    
    scatterx = []
    scattery = []
    
    for n, col in enumerate(dft.columns):
        x.append(n)
        y.append(np.mean(dft[col]))
        y_err.append(np.std(dft[col]))
    
        for v in dft[col]:
            scattery.append(v)
            scatterx.append(n + ((random.random()-0.5)*0.05))
    
    p = plt.plot(x, y, label=label)
    color=p[0].get_color()
    plt.errorbar(x, y, yerr=y_err, fmt=color)
    plt.scatter(scatterx, scattery, alpha=0.3, color=color)
    
    plt.legend(loc=2)
    ax.set_xticks(range(len(dft.columns)))
    ax.set_xticklabels([x[2] for x in dft.columns])
    plt.show()
    

    这将显示带有误差线和数据点的折线图。上面的代码可能有一些错误。在粘贴之前,我复制了它并简化了一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      • 2012-10-13
      相关资源
      最近更新 更多