python - 来自 Python 计数表的箱线图

标签 python python-3.x pandas matplotlib ggplot2

我有一个计数表作为 Python 中的数据框,我想将我的分布绘制为箱线图。例如:

df=pandas.DataFrame.from_items([('Quality',[29,30,31,32,33,34,35,36,37,38,39,40]), ('Count', [3,38,512,2646,9523,23151,43140,69250,107597,179374,840596,38243])])

我通过重复我的质量值来“解决”它。但我认为这不是一个好方法,而且我的数据框变得非常非常大。

在 R 中有一个单行:

ggplot(df, aes(x=1,y=Quality,weight=Count)) + geom_boxplot()

这将输出:!Boxplot from R 1

我的目标是比较不同组的分布,它应该看起来像this Python也能这样解决吗?

最佳答案

你想在这里看什么?下面的箱线图将返回下图。

enter image description here

import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline
df=pd.DataFrame.from_items([('Quality',[29,30,31,32,33,34,35,36,37,38,39,40]), ('Count', [3,38,512,2646,9523,23151,43140,69250,107597,179374,840596,38243])])
plt.figure()
df_box = df.boxplot(column='Quality', by='Count',return_type='axes')

如果您想查看按计数加权的质量分布,您可以尝试绘制直方图:

plt.figure()
df_hist = plt.hist(df.Quality, bins=10, range=None, normed=False, weights=df.Count)

Histogramme

关于python - 来自 Python 计数表的箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38761192/

相关文章:

python - 选择数据框中的公共(public)元素 Python

python - 如何在 Python 中使用嵌套的 if 和循环对代码进行矢量化?

python - Python 中 Azure Functions 的 Blob 名称模式

python - 为 ModelChoiceField 指定 CSS 类不起作用

python - 使用 SymPy 求解方程组

Python 文件上传到 Google 云存储桶返回 Broken Pipe 错误

python - 根据列表长度合并2个数据帧(使用Pandas)

python Pandas : Keeping earliest data in new column when grouping data

python - 试用不使用字典的动态Python变量创建

python - sre_constants.error : multiple repeat at position 2