Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3D animation using matplotlib

I want to make 3D animation with matplotlib, but I don't know how to. Here is my non-working code.

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

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

#setting
ax.set_xlim(-5,5)
ax.set_ylim(-5,5)
#ax.set_zlim(-5,5)
ax.set_xlabel('x')
ax.set_ylabel('y')
#ax.set_zlabel('z')
ax.grid()

f1, = ax.plot([], [], "r-", lw=1) #plot1

def gen():
    for phi in np.linspace(0,2*pi,100):
        yield np.cos(phi), np.sin(phi), phi

def update(data):
    p1, q1, psi = data
    f1.set_data(p1,q1)
    #f1.set_3d_properties(psi)

ani = animation.FuncAnimation(fig, update, gen, blit=False, interval=100, repeat=True)
#ani.save('matplot003.gif', writer='imagemagick')
plt.show()
like image 926
Yuto Avatar asked Jun 30 '16 09:06

Yuto


People also ask

Can Matplotlib do 3D?

In order to plot 3D figures use matplotlib, we need to import the mplot3d toolkit, which adds the simple 3D plotting capabilities to matplotlib. Once we imported the mplot3d toolkit, we could create 3D axes and add data to the axes. Let's first create a 3D axes.

How do you animate a 3D graph in Python?

To pass a function into animation class, make a user-defined function that removes the previous plot and plot a surface using x, y and z-array. Create a new figure or activate an existing figure. Add a subplot arrangement using subplots() method. Set the Z-axis limit using set_zlim() method.

Can Matplotlib create animated graphs?

Matplotlib library of Python is a plotting tool used to plot graphs of functions or figures. It can also be used as an animation tool too. The plotted graphs when added with animations gives a more powerful visualization and helps the presenter to catch a larger number of audience.

Is Matplotlib 2D or 3D?

Matplotlib is an excellent 2D and 3D graphics library for generating scientific figures.


1 Answers

I used this example http://matplotlib.org/1.4.1/examples/animation/simple_3danim.html and modified your code:

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

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

def gen(n):
    phi = 0
    while phi < 2*np.pi:
        yield np.array([np.cos(phi), np.sin(phi), phi])
        phi += 2*np.pi/n

def update(num, data, line):
    line.set_data(data[:2, :num])
    line.set_3d_properties(data[2, :num])

N = 100
data = np.array(list(gen(N))).T
line, = ax.plot(data[0, 0:1], data[1, 0:1], data[2, 0:1])

# Setting the axes properties
ax.set_xlim3d([-1.0, 1.0])
ax.set_xlabel('X')

ax.set_ylim3d([-1.0, 1.0])
ax.set_ylabel('Y')

ax.set_zlim3d([0.0, 10.0])
ax.set_zlabel('Z')

ani = animation.FuncAnimation(fig, update, N, fargs=(data, line), interval=10000/N, blit=False)
#ani.save('matplot003.gif', writer='imagemagick')
plt.show()

enter image description here

like image 123
Ophir Carmi Avatar answered Oct 03 '22 13:10

Ophir Carmi