Reputation: 1431
I have a csv file with a grid of z values corresponding to x
in [1,2,3,4]
and y
in [1,2,3]
like this:
1.1,1.2,1.3
2.1,2.2,2.3
3.1,3.2,3.3
4.1,4.2,4.3
I want to make a 3D scatterplot using mplot3d. What is the right way to read the file and make a 3D plot?
Upvotes: 0
Views: 7578
Reputation: 2660
I'm not sure I understood your question correctly, but if the CSV file's first column contains x
coordinates, the second contains y
coordinates, and the third z
coordinates, it's actually quite easy.
Assuming that your CSV file looks like this (the titles are important):
points.csv
x,y,z <- titles
1.1,1.2,1.3
2.1,2.2,2.3
3.1,3.2,3.3
4.1,4.2,4.3
Now, you can read the file in one line with pandas
:
points = pandas.read_csv('points.csv')
Accessing each column is easy too:
points['x'].values #get the x coordinates
So then you can use matplotlib to plot the points:
Full code:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import pandas
points = pandas.read_csv('points.csv')
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = points['x'].values
y = points['y'].values
z = points['z'].values
# x y z
# 1.1,1.2,1.3
# 2.1,2.2,2.3
# 3.1,3.2,3.3
# 4.1,4.2,4.3
ax.scatter(x, y, z, c='r', marker='o')
plt.show()
Notes:
Check this out for more info on 3D plotting with matplotlib
Upvotes: 4