gpt4 book ai didi

python - 散点图同一点重复多次python

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

我正在尝试从下面的字典中绘制散点图:

data_dict = {12: [1, 17, 11, 17, 1, 14, 38], 13: [13, 6, 4, 6], 14: [15, 8, 20, 8, 7], 15: [2, 3, 3, 1], 16: [62, 13, 36, 3, 8, 99, 54], 17: [1], 18: [44, 30, 36, 14, 21, 13, 44, 1, 62, 36], 19: [5, 5], 20: [27, 42, 42, 18, 31, 55, 31, 55], 21: [59, 1, 42, 17, 66, 26, 18, 4, 36, 42, 20, 54, 44, 35]}

我正在使用以下代码绘制散点图,其中字典键是 x 值是值是对应的值。

for xe, ye in data_dict.iteritems():
plt.scatter([xe] * len(ye), ye)

得到这个 plotly :

enter image description here

我希望能够区分在给定的 x 和 y 位置只有一个点还是有多个点。例如,对于 x = 12,y = 1 和 17 重复两次。我正在寻找一种通过数据点的颜色或大小来表示这种重复的方式。

我找不到任何关于如何执行此操作的引用。如果有任何帮助或指导,我将不胜感激。

谢谢。

最佳答案

您可以获得每个项目的 .count() 并根据它计算大小,然后使用命名参数 s 指定这些大小。顺便说一句,如果你使用的是 python 2,请将 .items() 更改为 .iteritems()

http://i.imgur.com/b8rO75l.png

import matplotlib.pyplot as plt

data_dict = {12: [1, 17, 11, 17, 1, 14, 38], 13: [13, 6, 4, 6], 14: [15, 8, 20, 8, 7], 15: [2, 3, 3, 1], 16: [62, 13, 36, 3, 8, 99, 54], 17: [1], 18: [44, 30, 36, 14, 21, 13, 44, 1, 62, 36], 19: [5, 5], 20: [27, 42, 42, 18, 31, 55, 31, 55], 21: [59, 1, 42, 17, 66, 26, 18, 4, 36, 42, 20, 54, 44, 35]}
size_constant = 20

for xe, ye in data_dict.items():
xAxis = [xe] * len(ye)

#square it to amplify the effect, if you do ye.count(num)*size_constant the effect is barely noticeable
sizes = [ye.count(num)**2.5 * size_constant for num in ye]
plt.scatter(xAxis, ye, s=sizes)

plt.show()



这是具有更多重复次数的数据的样子,因为您的数据集没有很多重复,所以很难显示效果。

data_dict = {5 : [1], 8 : [5,5,5], 11 : [3,3,3],  15 : [8,8,8,8,7,7], 19 : [12, 12, 12, 12, 12, 12]}

enter image description here

关于python - 散点图同一点重复多次python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37759475/

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