How to make a 3D scatter plot in Python?


I am currently have a nx3 matrix array. I want plot the three columns as three axis"s. How can I do that?

I have googled and people suggested using Matlab, but I am really having a hard time with understanding it. I also need it be a scatter plot.

Can someone teach me?

Answer rating: 192

You can use matplotlib for this. matplotlib has a mplot3d module that will do exactly what you want.

from matplotlib import pyplot
from mpl_toolkits.mplot3d import Axes3D
import random

fig = pyplot.figure()
ax = Axes3D(fig)

sequence_containing_x_vals = list(range(0, 100))
sequence_containing_y_vals = list(range(0, 100))
sequence_containing_z_vals = list(range(0, 100))


ax.scatter(sequence_containing_x_vals, sequence_containing_y_vals, sequence_containing_z_vals)

The code above generates a figure like:

matplotlib 3D image

Get Solution for free from DataCamp guru