我有一个计数表作为 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
最佳答案
你想在这里看什么?下面的箱线图将返回下图。
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)
关于python - 来自 Python 计数表的箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38761192/