Cover Image for 3D Scatter Plotting in Python using Matplotlib
82 views

3D Scatter Plotting in Python using Matplotlib

Creating a 3D scatter plot in Python using Matplotlib is a great way to visualize three-dimensional data. Here’s an example of how to create a basic 3D scatter plot:

Python
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# Generate some random 3D data
np.random.seed(42)
num_points = 100
x = np.random.rand(num_points)
y = np.random.rand(num_points)
z = np.random.rand(num_points)

# Create a 3D figure
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Create the 3D scatter plot
ax.scatter(x, y, z, c='b', marker='o')

# Set labels for the axes
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

# Set a title for the plot
plt.title('3D Scatter Plot Example')

# Show the plot
plt.show()

In this example:

  1. We import Matplotlib’s pyplot module and the Axes3D module from mpl_toolkits.mplot3d.
  2. We generate some random 3D data for the x, y, and z coordinates. You can replace this with your own data.
  3. We create a 3D figure using plt.figure() and add a subplot with 3D projection using fig.add_subplot(111, projection='3d').
  4. We create the 3D scatter plot using ax.scatter(), specifying the x, y, and z coordinates. The c argument sets the color, and the marker argument sets the marker style.
  5. We set labels for the x, y, and z axes using ax.set_xlabel(), ax.set_ylabel(), and ax.set_zlabel().
  6. We set a title for the plot using plt.title().
  7. Finally, we display the 3D scatter plot using plt.show().

This code will create a 3D scatter plot with random data. You can customize the data and appearance of the plot to suit your specific requirements.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS