简体   繁体   English

地理坐标(纬度/经度)上的抖动 function

[英]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

  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.我正在尝试创建一个抖动效果,以便根据学校 ID 代表 map 中的所有学生。

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.我不想在 map 中将许多点表示为一个点,而是在学校周围创建点,以代表属于该学校的学生。

抖动效应

A function that can be applied using pandas groupby('School_ID') that creates this minimal modification on coordinates.可以使用 pandas groupby('School_ID')应用的 function 可在坐标上创建此最小修改。

Since you want to plot the results, you need graphic libraries.既然要 plot 的结果,就需要图形库。 Seaborn has a jitter function inside. Seaborn 内部有一个抖动 function。

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.如果您想知道,那就是蓝色点。

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 .但是,如果您想为 map 手动执行抖动效果,则不必按School_ID

sigma for normal distrubition need to be selected experimentally:需要通过实验选择正态分布的sigma

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM