FooBar
FooBar

Reputation: 16518

Matplotlib: Plot movements in 3D

I have a solver that solves a system of equations in 3 variables. Each iteration, it has a new guess on all three variables. The guesses over iterations look like this:

array([[ 0.86063431,  0.07119279,  1.70377142],
       [ 0.86391084,  0.07014899,  1.72184785],
       [ 0.86332177,  0.069444  ,  1.71182579],
       [ 0.86192988,  0.06913941,  1.69818289],
       [ 0.86166436,  0.06916367,  1.69527615]])

(Here for 5 iterations). I would like to plot these using matplotlib. I was thinking about having a dot for each of these coordinates, and have a line connecting them to show the order of coordinates.

Is this a good way of visualizing this? How would I do that using matplotlib?

Upvotes: 0

Views: 5113

Answers (1)

sulkeh
sulkeh

Reputation: 947

You can plot this as a 3D trajectory with matplotlib:

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

points = np.array([[ 0.86063431,  0.07119279,  1.70377142],
                       [ 0.86391084,  0.07014899,  1.72184785],
                       [ 0.86332177,  0.069444  ,  1.71182579],
                       [ 0.86192988,  0.06913941,  1.69818289],
                       [ 0.86166436,  0.06916367,  1.69527615]]).T
    
fig = plt.figure()
ax = fig.add_subplot(111, projection = '3d')
ax.plot(points[0], points[1], points[2], marker = 'x')
ax.scatter(*points.T[0], color = 'red')
plt.show()

Upvotes: 1

Related Questions