简体   繁体   English

如何在 python pandas 中绘制/分散地理坐标

[英]How to plot/scatter geo coordinates in python pandas

I'm having a hard time plotting something in pandas the way I want it.我很难按照我想要的方式在 pandas 中绘制一些东西。

I have movement traces in a csv file that look like that:我在 csv 文件中有运动痕迹,如下所示:

NodeID | Time   | Lon              | Lat             # Line not in the CSV
0      | 38.665 |29564.86465677584 |37278.27065254189
0      | 64.29  |29529.86419382295 |37300.74058444612
0      | 80.74  |29511.18207467592 |37317.11012177728
1      | 166.3  |29593.54098394629 |37403.83872184437
1      | 188.98 |29622.25878085964 |37441.86538931914
1      | 219.33 |29658.04716892622 |37491.65280349273
.
.
.
20     | 566.3  |29593.54098394629 |37403.83872184437
20     | 888.98 |29622.25878085964 |37441.86538931914
30     | 919.33 |29658.04716892622 |37491.65280349273


I read the csv with:我阅读了 csv :

df = pd.read_csv(path, delimiter=' ', 
                 names=["node", "time", "x","y"], header=None)

Now I want to plot every node with a different color.现在我想 plot 每个节点用不同的颜色。 Ideally, every point should be connected to the next, but this is not necessary due to the close distance of the locations.理想情况下,每个点都应该连接到下一个点,但由于位置距离很近,这不是必需的。

I tried it with:我试过了:

df.plot(x='x', y='y', kind='scatter')
# or
df[NodeID:].plot(x='x', y='y', kind='scatter')
# or with matplotlib
ax = fig.subplots()
ax.plot(pddata['x'], pddata['y'], label='node ' + str(pddata['node'])) 

But it always looks like this:但它总是看起来像这样:

坐标图

Can someone give me a hint how to plot it the way I want it?有人可以给我一个提示如何 plot 它按照我想要的方式吗?

This can be done with the seaborn package, it can be quite helpful in plotting and works with pandas .这可以通过seaborn package 来完成,它对绘图和使用pandas非常有帮助。 Looking through its gallery as well as the python graph gallery the might also give some inspiration for your plots.浏览它的图库以及 python 图库,也可能会为您的绘图提供一些灵感。

https://seaborn.pydata.org/ https://python-graph-gallery.com/ https://seaborn.pydata.org/ https://python-graph-gallery.com/

For your specific problem, with the dataframe set up, this hopefully helps:对于您的具体问题,使用 dataframe 设置,希望这会有所帮助:

import seaborn as sns
sns.lineplot(x='x', y='y', hue='node', marker='o', data=df)

Found a solution thankts to some tipps in the comments.感谢评论中的一些提示,找到了解决方案。

Now i use matplotlip to plot and pandas to group:现在我使用 matplotlip 对 plot 和 pandas 进行分组:

fig = plt.figure(num=None, figsize=(15, 15), dpi=80, facecolor='w', edgecolor='k')
ax = fig.subplots()

df = pd.read_csv('Filename', delimiter=' ', names=["node", "time", "x","y"], header=None)

# Scatter for every node
for node, node_data in df.groupby('node'):
    ax.scatter(node_data['x'], node_data['y'], label='node ' + str(node), s=2,
    c=np.random.rand(3,))
    #or use ax.plot to show lines

plt.show()

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

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