【问题标题】:matplotlib: box plot for each categorymatplotlib:每个类别的箱线图
【发布时间】:2018-07-20 14:30:05
【问题描述】:

我的 pandas 数据框有两列:categoryduration。和 我使用以下代码制作所有数据点的箱线图。

import matplotlib.pyplot as plt
plt.boxplot(df.duration)
plt.show()

但是,如果我希望每个category 前一个框,我该如何修改上面的代码?谢谢!

【问题讨论】:

    标签: pandas matplotlib boxplot


    【解决方案1】:

    除了Wen's answer,您可能还想查看the seaborn library。就是为了这样的情节而生的。

    Seaborn 是一个基于 matplotlib 的 Python 可视化库。它 提供用于绘制有吸引力的统计数据的高级界面 图形。

    查看documentation for boxplots

    绘制箱线图以显示关于类别的分布。

    sns.boxplot(data=df, x='category', y='duration')
    

    【讨论】:

      【解决方案2】:

      我们可以用熊猫来做到这一点

      #df=pd.DataFrame({'category':list('aacde'),'duration':[1,3,2,3,4]}) sample data
      df.assign(index=df.groupby('category').cumcount()).pivot('index','category','duration').plot(kind='box')
      

      【讨论】:

      • 如何在这个 pandas 版本中添加异常值?如果没有,我们如何在 matplotlib 中做到这一点?
      猜你喜欢
      • 2020-03-02
      • 2021-07-31
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 2016-06-22
      • 2010-12-03
      • 2018-07-12
      • 2013-05-11
      相关资源
      最近更新 更多