Skip to content Skip to sidebar Skip to footer

Jitter Function On Geographic Coordinates (latitude/longitudes)

I have the following situation: I have a dataset where each row represent a student. Student_ID School_ID School_Lat School_Long 12221 14a -22.7324 -47.6533 123

Solution 1:

Since you want to plot the results, you need graphic libraries. Seaborn has a jitter function inside.

I don't have your full dataset to try, but I think you achieve what you want to with this code:

import seaborn as sns
   import matplotlib.pyplot as plt

   sns.stripplot(x=df['School_Lat'], y=df['School_Long'], data=df, jitter=True)
   sns.despine()

enter image description here

I added one more record to your sample data, just to see how it would behave. That's the blue spot, if you are wondering.

Solution 2:

Solution on plotting library level is perfect.

But if you want to do manually jitter effect for map you don't have to group by School_ID.

sigma for normal distrubition need to be selected experimentally:

sigma = 0.1
df['School_Lat'] = df['School_Lat'].apply(lambda x: x + np.random.normal(x, sigma, 1))
df['School_Long'] = df['School_Long'].apply(lambda x: x + np.random.normal(x, sigma, 1))

Post a Comment for "Jitter Function On Geographic Coordinates (latitude/longitudes)"