littlefirewooder
littlefirewooder

Reputation: 19

How to draw 3D dynamic curve with python

I continue to output three-dimensional coordinates, and use these coordinates to output three-dimensional dynamic curves. Here is my code, but there is nothing in the figure.

plt.ion()  
x = [0]
y = [0]
z = [0]
x_now = 0
fig = plt.figure()
ax1 = plt.axes(projection='3d')

for i in range(50):
    plt.clf() 
    x_now = i * 1
    x.append(x_now)
    y.append(x_now)
    z.append(x_now)
    ax1.scatter3D(x, y, z) 
    plt.show()
    plt.pause(0.1)

Upvotes: 1

Views: 284

Answers (2)

Michael
Michael

Reputation: 5335

Where do you launch this script? If it is jupyter-notebook, add this line on top:

%matplotlib inline

If you are using jupyter-console, it will be

%matplotlib <backend>

where <backend> is one of ('osx', 'qt4', 'qt5', 'gtk3', 'notebook', 'wx', 'qt', 'nbagg).

If it is a regular script, first you should do:

import matplotlib
matplotlib.use('<backend>') #in quotes

where <backend> is:

- interactive backends:
  GTK3Agg, GTK3Cairo, MacOSX, nbAgg,
  Qt4Agg, Qt4Cairo, Qt5Agg, Qt5Cairo,
  TkAgg, TkCairo, WebAgg, WX, WXAgg, WXCairo

- non-interactive backends:
  agg, cairo, pdf, pgf, ps, svg, template

Then you are importing:

import matplotlib.pyplot as plt

and go on with your script.

Upvotes: 1

Ricardo
Ricardo

Reputation: 691

plt.clf() will clear the figure.

import matplotlib.pyplot as plt
x = [0]
y = [0]
z = [0]
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
for i in range(5):
    x_now = i * 1
    x.append(x_now)
    y.append(x_now)
    z.append(x_now)    
    ax.scatter(x, y, z)
    plt.pause(0.1)
plt.show()

Upvotes: 1

Related Questions