You can use the scatter3D()
method of the Axes3DSubplot
object:
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter3D(data[:,1], data[:,2], data[:,7], c='r', marker='0')
Question
I'm stumped as to why this is not working. I am pulling a bunch of floating point data in to a numpy array from a csv file, and I just want to create a 3d scatter plot based from 3 of the columns in the array.
#import data from the csv file
data = np.genfromtxt('data.csv', delimiter=',', dtype=float, skiprows=1)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(data[:,1], data[:,2], data[:,7], c='r', marker='0')
plt.show()
every time i get an assertion error:
/usr/lib/pymodules/python2.7/matplotlib/path.pyc in __init__(self, vertices, codes, _interpolation_steps, closed)
127 codes[-1] = self.CLOSEPOLY
128
--> 129 assert vertices.ndim == 2
130 assert vertices.shape[1] == 2
131
AssertionError:
I have... just figured it out, but i'll post this any way because that is the single most useless error message i have ever encountered. the problem was here:
ax.scatter(data[:,1], data[:,2], data[:,7], c='r', marker='0')
marker='0' is invalid, i meant to hit marker='o', once fixed it works just fine.
Solution
You can use the scatter3D()
method of the Axes3DSubplot
object:
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter3D(data[:,1], data[:,2], data[:,7], c='r', marker='0')