3D Line or Scatter plot using Matplotlib (Python) [3D Chart]

If you ever want to plot some trajectory of particles then a 3D plot can be particularly useful.

The following is a very simple example of code illustrating the procedure to plot a 3D line/scatter chart using Matplotlib and Python

CODE

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



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

t = np.arange(0,20,0.2)
x = np.cos(t)-1
y = 1/2*(np.cos(2*t)-1)


# For scatter plot
# ax.scatter(x, y, t, c='r', marker='o')
ax.plot(x, y, t, c='r', marker='o')
# For line plot
ax.plot(x, y, t, c='g')

ax.set_xlabel('X(t)')
ax.set_ylabel('Y(t)')
ax.set_zlabel('time')
ax.set_title('Trajectory of electron for E vector along [120]')

plt.show()

OUTPUT

[wpedon id="7041" align="center"]

Leave a Reply

Your email address will not be published. Required fields are marked *