How to make a 3D scatter plot in Python? How to make a 3D scatter plot in Python? python python

How to make a 3D scatter plot in Python?


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

from matplotlib import pyplotfrom mpl_toolkits.mplot3d import Axes3Dimport randomfig = 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))random.shuffle(sequence_containing_x_vals)random.shuffle(sequence_containing_y_vals)random.shuffle(sequence_containing_z_vals)ax.scatter(sequence_containing_x_vals, sequence_containing_y_vals, sequence_containing_z_vals)pyplot.show()

The code above generates a figure like:

matplotlib 3D image


Use the following code it worked for me:

# Create the figurefig = plt.figure()ax = fig.add_subplot(111, projection='3d')# Generate the valuesx_vals = X_iso[:, 0:1]y_vals = X_iso[:, 1:2]z_vals = X_iso[:, 2:3]# Plot the valuesax.scatter(x_vals, y_vals, z_vals, c = 'b', marker='o')ax.set_xlabel('X-axis')ax.set_ylabel('Y-axis')ax.set_zlabel('Z-axis')plt.show()

while X_iso is my 3-D array and for X_vals, Y_vals, Z_vals I copied/used 1 column/axis from that array and assigned to those variables/arrays respectively.


from mpl_toolkits import mplot3dimport numpy as npimport matplotlib.pyplot as pltfig = plt.figure()ax = plt.axes(projection='3d')ax = plt.axes(projection='3d')

scatter plot

zdata = 15 * np.random.random(100)xdata = np.sin(zdata) + 0.1 * np.random.randn(100)ydata = np.cos(zdata) + 0.1 * np.random.randn(100)ax.scatter3D(xdata, ydata, zdata);

enter image description here

Colab notebook