user8184942
user8184942

Reputation:

How to plot/scatter geo coordinates in python pandas

I'm having a hard time plotting something in pandas the way I want it.

I have movement traces in a csv file that look like that:

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:

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

Now I want to plot every node with a different color. 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:

Plot of coordinates

Can someone give me a hint how to plot it the way I want it?

Upvotes: 1

Views: 1389

Answers (2)

Wolke-Wanderer
Wolke-Wanderer

Reputation: 46

This can be done with the seaborn package, it can be quite helpful in plotting and works with pandas. Looking through its gallery as well as the python graph gallery the might also give some inspiration for your plots.

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

For your specific problem, with the dataframe set up, this hopefully helps:

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

Upvotes: 2

user8184942
user8184942

Reputation:

Found a solution thankts to some tipps in the comments.

Now i use matplotlip to plot and pandas to group:

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

Upvotes: 0

Related Questions