How to make a 4D plot with Matplotlib using arbitrary data?

To make a 4D plot, we can create x, y, z and c standard data points. Create a new figure or activate an existing figure.


  • Use figure() method to create a figure or activate an existing figure.

  • Add a figure as part of a subplot arrangement.

  • Create x, y, z and c data points using numpy.

  • Create a scatter plot using scatter method.

  • To display the figure, use show() method.


from matplotlib import pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.random.standard_normal(100)
y = np.random.standard_normal(100)
z = np.random.standard_normal(100)
c = np.random.standard_normal(100)
img = ax.scatter(x, y, z, c=c, cmap='YlOrRd', alpha=1)


Updated on: 11-May-2021

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started