[英]How to plot/scatter geo coordinates in python pandas
我很难按照我想要的方式在 pandas 中绘制一些东西。
我在 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
我阅读了 csv :
df = pd.read_csv(path, delimiter=' ',
names=["node", "time", "x","y"], header=None)
现在我想 plot 每个节点用不同的颜色。 理想情况下,每个点都应该连接到下一个点,但由于位置距离很近,这不是必需的。
我试过了:
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']))
但它总是看起来像这样:
有人可以给我一个提示如何 plot 它按照我想要的方式吗?
这可以通过seaborn
package 来完成,它对绘图和使用pandas
非常有帮助。 浏览它的图库以及 python 图库,也可能会为您的绘图提供一些灵感。
https://seaborn.pydata.org/ https://python-graph-gallery.com/
对于您的具体问题,使用 dataframe 设置,希望这会有所帮助:
import seaborn as sns
sns.lineplot(x='x', y='y', hue='node', marker='o', data=df)
感谢评论中的一些提示,找到了解决方案。
现在我使用 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.