2016-06-11 58 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