Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scatter plot same point repeated several times python

I am trying to draw a scatter plot from a dictionary bellow:

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]}

I am using the following code to draw a scatter plot where dictionary keys are the x values are the values are the corresponding values.

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

And getting this plot:

enter image description here

I'de like to be able to distinguish between just having one point at a given x and y location vs having multiple point. For example for x = 12, y = 1 and 17 are repeated twice. I'm looking in a way of representing this repetition by either color or size of the data points.

I could not find any reference on how to do this. I would appreciate any help or guidance.

Thanks.

like image 922
PyPhys Avatar asked Oct 23 '25 03:10

PyPhys


1 Answers

You can get the .count() for each item and calculate the size based off that, then use the named parameter s to specify those sizes. Btw change .items() to .iteritems() if you are on 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()



Here's what it looks like with data with more reptititions, since your dataset doesn't have a lot of repeats it's hard to show the effect.

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

like image 84
Keatinge Avatar answered Oct 24 '25 16:10

Keatinge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!