我有一个包含数据的文件,我将其分为三类。我想显示三个不同的“箱”,它们都只显示一个数字(该类别的平均值)。

import csv
import matplotlib.pyplot as plt
import seaborn as sns

c1 = [1, 1, 1, 3, 3, 3] # average 2
c2 = [8, 12] # average 10
c3 = [20, 30, 40] # average 30

sns.set(style='ticks')

plt.hist([(sum(c1)/len(c1)), (sum(c2)/len(c2)), (sum(c3)/len(c3))], bins=8)
plt.show()


现在我知道我的代码行不通了,我只是不知道如何设置类似这样的内容,而这会导致我想要的结果。

总结一下我想要的:在x轴上c1,c2,c3。在y轴上,我希望c1达到该列表的平均值(对于c1,在y轴上为2),对于c2到10,对于c3到30。

最佳答案

由于您只需要每个列表的平均值,因此您需要的是条形图而不是直方图,其中的刻度标签代表c1c2c3。直方图用于绘制分布,每个列表都需要离散的平均条形

import matplotlib.pyplot as plt
import seaborn as sns

c1 = [1, 1, 1, 3, 3, 3] # average 2
c2 = [8, 12] # average 10
c3 = [20, 30, 40] # average 30

sns.set(style='ticks')

plt.bar(range(3), [(sum(c1)/len(c1)), (sum(c2)/len(c2)), (sum(c3)/len(c3))])
plt.xticks(range(3), ['c1', 'c2', 'c3'])
plt.show()


python - Pyplot如何从多个列表的平均值创建直方图?-LMLPHP

关于python - Pyplot如何从多个列表的平均值创建直方图?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56502338/

10-10 13:11