Jitter function on Geographic coordinates (Latitude/longitudes)

…衆ロ難τιáo~ 提交于 2020-01-16 08:36:34

问题


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.

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


回答1:


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



回答2:


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

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.



来源:https://stackoverflow.com/questions/58473623/jitter-function-on-geographic-coordinates-latitude-longitudes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!