散点图同一点重复几次python

PyP*_*hys 2 python matplotlib scatter-plot plotly

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

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]}
Run Code Online (Sandbox Code Playgroud)

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

for xe, ye in data_dict.iteritems():
    plt.scatter([xe] * len(ye), ye)
Run Code Online (Sandbox Code Playgroud)

得到这个情节:

在此处输入图片说明

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

我找不到有关如何执行此操作的任何参考。我将不胜感激任何帮助或指导。

谢谢。

Kea*_*nge 6

您可以获取每个项目的.count()并据此计算尺寸,然后使用命名参数s指定这些尺寸。顺便说一句更改.items().iteritems()如果您在python 2上

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()
Run Code Online (Sandbox Code Playgroud)



这是具有更多复制的数据的外观,因为您的数据集没有很多重复,因此很难显示效果。

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]}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明