Jitter function on Geographic coordinates (Latitude/longitudes)

Viewed 1024

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

  12344      14a      -22.7324    -47.6533

You can notice that if the student belongs to the same school, the school's geocod will be the same.

I am trying to create a jitter effect in order to represent all the student in a map based on school id.

Example:

Instead of represent many dots into one single point in a map, I would like to create points around the school, to represent the students that belong to that school.

Jitter Effect

A function that can be applied using pandas groupby('School_ID') that creates this minimal modification on coordinates.

2 Answers

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 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))
Related