gpt4 book ai didi

python - 来自不同长度字典的箱线图

转载 作者:行者123 更新时间:2023-11-28 21:38:50 26 4
gpt4 key购买 nike

我有一本字典,每个键的值长度不同。尝试绘制为 boxplot,我无法继续。还有其他方法吗?

我写了这个但是它不起作用:

import matplotlib.pyplot as plt 
import matplotlib as mpl

dict1 = {'Pb': [53.0, 56.0, 56.0, 57.0, 57.0, 57.0, 46.0], 'Pa': [56.0, 55.0], 'Pg': [57.0, 57.0, 58.0, 57.0, 57.0, 57.0, 57.0, 57.0,53.0, 57.0, 55.0, 58.0, 58.0, 58.0, 57.0, 57.0, 57.0, 57.0, 57.0, 55.0, 55.0, 57.0, 57.0, 55.0, 58.0, 58.0, 58.0, 55.0, 58.0, 54.0, 58.0, 57.0, 57.0, 58.0, 55.0, 56.0, 55.0, 55.0, 55.0, 58.0, 56.0, 57.0, 57.0, 57.0, 57.0, 56.0, 57.0, 56.0],'Pf': [54.0], 'Pn': [56.0, 56.0, 55.0, 56.0, 56.0, 56.0], 'Ps': [58.0, 56.0, 57.0, 56.0, 56.0, 55.0, 56.0, 56.0, 56.0, 55.0, 56.0, 56.0, 56.0, 58.0, 57.0, 58.0, 57.0, 57.0, 56.0, 58.0, 56.0, 53.0, 56.0, 56.0, 56.0, 56.0, 56.0, 56.0]}

# remove from here
for k, v in dict1.iteritems():
boxplot(k,v)
plt.show()

提出并测试的解决方案:只需将解释替换为下面的文本即可。它适用于 python 3.5

#And include this:
labels, data = [*zip(*dict1.items())] # 'transpose' items to parallel key, value lists
plt.boxplot(data)
plt.xticks(range(1, len(labels) + 1), labels)
plt.show()

最佳答案

你想把所有的值都放在一起吗?
[n for v in dict1.values() for n in v]“扁平化”值列表

那么箱线图就是 matplotlib 标准图 https://matplotlib.org/examples/pylab_examples/boxplot_demo.html

from matplotlib import pyplot as plt


plt.boxplot([n for v in dict1.values() for n in v])

enter image description here

或按键

# Python 3.5+
labels, data = [*zip(*dict1.items())] # 'transpose' items to parallel key, value lists

# or backwards compatable
labels, data = dict1.keys(), dict1.values()

plt.boxplot(data)
plt.xticks(range(1, len(labels) + 1), labels)
plt.show()

enter image description here

关于python - 来自不同长度字典的箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47657651/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com