'Plot latitude, longitude, elevation and EMF data from CSV in Python
I'm trying to plot a large number of latitude, longitude, elevation and EMF values from a CSV file. The CSV file looks like this
dat,latitude,longitude,EMF,Elevation
1/20/2022 7:18:17,59.39556688,18.12773272,0,18.17260262
1/20/2022 7:18:18,59.39556685,18.12773267,0,18.17260262
1/20/2022 7:18:19,59.39556684,18.12773265,0,18.17260262
1/20/2022 7:18:20,59.39556693,18.1277326,4.1,18.17260262
1/20/2022 7:18:21,59.39556698,18.12773191,4,18.17260262
1/20/2022 7:18:22,59.39556714,18.1277315,4.1,18.17260262
1/20/2022 7:18:23,59.39556728,18.12773191,4.1,18.17260262
1/20/2022 7:18:24,59.39556718,18.12773088,4,18.17260262
1/20/2022 7:18:25,59.39556755,18.12773013,4.1,18.17260262
1/20/2022 7:18:26,59.39556755,18.1277296,131,18.17260262
1/20/2022 7:18:27,59.39556729,18.12772922,125.9,18.17260262
1/20/2022 7:18:28,59.39556682,18.1277278,9,18.17260262
1/20/2022 7:18:29,59.39556684,18.1277263,4.1,18.17260262
I want to represent them by diferent colors depending on the EMF value.
My code is this
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pandas
points = pandas.read_csv('data.csv')
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
latitude = points['latitude'].values
longitude = points['longitude'].values
EMF = points['EMF'].values
plt.ticklabel_format(useOffset=False)
ax.scatter(latitude, longitude, EMF, c='r', marker='o')
plt.show()
How can i do that?
Solution 1:[1]
Edit: per Mr. T's comment, you can just plug EMF into c
import pandas as pd
from io import StringIO
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
s = """1/20/2022 7:18:17,59.39556688,18.12773272,0,18.17260262
1/20/2022 7:18:18,59.39556685,18.12773267,0,18.17260262
1/20/2022 7:18:19,59.39556684,18.12773265,0,18.17260262
1/20/2022 7:18:20,59.39556693,18.1277326,4.1,18.17260262
1/20/2022 7:18:21,59.39556698,18.12773191,4,18.17260262
1/20/2022 7:18:22,59.39556714,18.1277315,4.1,18.17260262
1/20/2022 7:18:23,59.39556728,18.12773191,4.1,18.17260262
1/20/2022 7:18:24,59.39556718,18.12773088,4,18.17260262
1/20/2022 7:18:25,59.39556755,18.12773013,4.1,18.17260262
1/20/2022 7:18:26,59.39556755,18.1277296,131,18.17260262
1/20/2022 7:18:27,59.39556729,18.12772922,125.9,18.17260262
1/20/2022 7:18:28,59.39556682,18.1277278,9,18.17260262
1/20/2022 7:18:29,59.39556684,18.1277263,4.1,18.17260262"""
df = pd.read_csv(StringIO(s), header=None)
df.columns = pd.Index(['dat','latitude','longitude','EMF','Elevation'])
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.ticklabel_format(useOffset=False)
x, y, z = df['latitude'], df['longitude'], df['EMF']
ax.scatter(x, y, z, c=z)
plt.show()
Original answer Without resorting to fancier methods, you could pick a few thresholds and use pandas convenient indexing capabilities to plot different thresholds with different colors.
import pandas as pd
from io import StringIO
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
s = """1/20/2022 7:18:17,59.39556688,18.12773272,0,18.17260262
1/20/2022 7:18:18,59.39556685,18.12773267,0,18.17260262
1/20/2022 7:18:19,59.39556684,18.12773265,0,18.17260262
1/20/2022 7:18:20,59.39556693,18.1277326,4.1,18.17260262
1/20/2022 7:18:21,59.39556698,18.12773191,4,18.17260262
1/20/2022 7:18:22,59.39556714,18.1277315,4.1,18.17260262
1/20/2022 7:18:23,59.39556728,18.12773191,4.1,18.17260262
1/20/2022 7:18:24,59.39556718,18.12773088,4,18.17260262
1/20/2022 7:18:25,59.39556755,18.12773013,4.1,18.17260262
1/20/2022 7:18:26,59.39556755,18.1277296,131,18.17260262
1/20/2022 7:18:27,59.39556729,18.12772922,125.9,18.17260262
1/20/2022 7:18:28,59.39556682,18.1277278,9,18.17260262
1/20/2022 7:18:29,59.39556684,18.1277263,4.1,18.17260262"""
df = pd.read_csv(StringIO(s), header=None)
df.columns = pd.Index(['dat','latitude','longitude','EMF','Elevation'])
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.ticklabel_format(useOffset=False)
below = df[df['EMF'] < 2]
x, y, z = below['latitude'], below['longitude'], below['EMF']
ax.scatter(x, y, z, c='r')
above = df[df['EMF'] >= 2]
x, y, z = above['latitude'], above['longitude'], above['EMF']
ax.scatter(x, y, z, c='b')
plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


