2016-06-11 95 views
1

我试图借鉴字典波纹管的散点图:散点图同一个点反复多次蟒蛇

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) 

并获得该地块:

enter image description here

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

我找不到任何有关如何做到这一点的参考。我将不胜感激任何帮助或指导。

感谢。

回答

2

您可以获取每个项目的.count()并基于该项目计算大小,然后使用指定参数s来指定这些大小。顺便说一句更改.items().iteritems()如果你是蟒蛇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() 



这里是什么样子与更多的reptititions数据,因为数据集中不具有很多重复的很难以显示效果。

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