Matplotlib 3D scatter plot animation is not moving - python

I am a python beginner. This is my first time building a 3D scatterplot animation. In my code, I only want 6 points to display in a frame and remove points before the next frame comes. After writing the code, the problem came. The video is like a photo (see below) and shows every point.
Please help me, I don't know how to change my code.
CSV
import os
import csv
from csv import reader
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
fig = plt.figure()
ax = plt.subplot(projection='3d')
ax.set_xlim(0,100)
ax.set_ylim(0,100)
ax.set_zlim(0,100)
dataname = open('/content/id3.csv', 'r')
data = csv.DictReader(dataname)
xr = []
yr = []
zr = []
xr1 = []
yr1 = []
zr1 = []
for col in data:
xr.append(col['x'])
yr.append(col['y'])
zr.append(col['z'])
xr1.append(col['x1'])
yr1.append(col['y1'])
zr1.append(col['z1'])
x=[int(x) for x in xr]
y=[int(x) for x in yr]
z=[int(x) for x in zr]
x1=[int(x) for x in xr1]
y1=[int(x) for x in yr1]
z1=[int(x) for x in zr1]
def init():
ax.scatter(x, y, z)
ax.scatter(x1, y1, z1)
def run(data):
ax.clear()
ax.set_xlim(0,100)
ax.set_ylim(0,100)
ax.set_zlim(0,100)
ax.scatter(x,y,z)
ax.scatter(x1, y1, z1)
ani = animation.FuncAnimation(fig, run, frames=50, interval=30, init_func=init)
ani.save('DREAMLINERs.mp4', fps=10)

You have to edit run function ad use i parameter as a counter: in each frame i increases by 1, so you can use it in order to select which point to plot in each frame. If you want to plot 6 points, whose coordinates come from x, y, z arrays, you can use:
ax.scatter(x[i:i + 6], y[i:i + 6], z[i:i + 6])
Complete Code
import csv
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ax = plt.subplot(projection='3d')
ax.set_xlim(0,100)
ax.set_ylim(0,100)
ax.set_zlim(0,100)
dataname = open('/content/id3.csv', 'r')
data = csv.DictReader(dataname)
xr = []
yr = []
zr = []
xr1 = []
yr1 = []
zr1 = []
for col in data:
xr.append(col['x'])
yr.append(col['y'])
zr.append(col['z'])
xr1.append(col['x1'])
yr1.append(col['y1'])
zr1.append(col['z1'])
x=[int(x) for x in xr]
y=[int(x) for x in yr]
z=[int(x) for x in zr]
x1=[int(x) for x in xr1]
y1=[int(x) for x in yr1]
z1=[int(x) for x in zr1]
def init():
ax.scatter(x, y, z)
ax.scatter(x1, y1, z1)
def run(i):
ax.clear()
ax.set_xlim(0,100)
ax.set_ylim(0,100)
ax.set_zlim(0,100)
ax.scatter(x[i:i + 6], y[i:i + 6], z[i:i + 6])
ax.scatter(x1[i:i + 6], y1[i:i + 6], z1[i:i + 6])
ani = animation.FuncAnimation(fig, run, frames=50, interval=30, init_func=init)
ani.save('DREAMLINERs.mp4', fps=10)
plt.show()
Animation
Notes
You can simplify the code by using pandas to read data from csv file:
import pandas as pd
data = pd.read_csv('data/id3.csv')
x = data['x']
y = data['y']
z = data['z']
x1 = data['x1']
y1 = data['y1']
z1 = data['z1']

Related

Edit the plot to make it smooth

I seek help in editing my plot. Here in the image you can see I am getting vertical lines in all the six plots.
But I want a smooth curve.
Here is my code what I have tried so far
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
from math import *
from scipy.stats import skew
from scipy.interpolate import make_interp_spline
import scipy
import seaborn as sns
plt.rcParams["figure.figsize"] = (12,3)
df=pd.read_csv("plot_data00.txt")
df=df[df['DOY'].between(184,187)]
df["HR"] = df["HR"]/24
df["DOY"] = df["DOY"]+ df["HR"]
df.to_csv("final_plot_data.txt")
#define variables to plot
import matplotlib.pyplot as plt
fig,axes= plt.subplots(nrows=6, ncols=1, squeeze=False)
x = df["DOY"]
y = df["By"]
z = df["Bz"]
a = df["Vsw"]
b = df["Nsw"]
c = df["reconnection_rate"]
d = df["magnetopause_distance"]
#create a figure and edit size
fig=plt.figure(figsize=(20,17))
#define subplots and define their position
plt1=fig.add_subplot(611)
plt2=fig.add_subplot(612)
plt3=fig.add_subplot(613)
plt4=fig.add_subplot(614)
plt5=fig.add_subplot(615)
plt6=fig.add_subplot(616)
plt1.plot("DOY", "By", data=df)
plt1.set_ylabel("By",size=16)
plt1.set_title("3-6 July 2003",size=20)
plt1.get_yaxis().set_label_coords(-0.05,0.5)
plt2.plot("DOY", "Bz", data=df)
plt2.set_ylabel("Bz",size=16)
plt2.get_yaxis().set_label_coords(-0.05,0.5)
plt3.plot("DOY", "Vsw", data=df)
plt3.set_ylabel("Vsw",size=16)
plt3.get_yaxis().set_label_coords(-0.05,0.5)
plt4.plot("DOY", "Nsw", data=df)
plt4.set_ylabel("Nsw",size=16)
plt4.get_yaxis().set_label_coords(-0.05,0.5)
plt5.plot("DOY", "reconnection_rate", data=df)
plt5.set_ylabel("MRR",size=16)
plt5.get_yaxis().set_label_coords(-0.05,0.5)
plt6.plot("DOY", "magnetopause_distance", data=df)
plt6.set_ylabel("MD",size=16)
plt6.set_xlabel("Day of Year",size=16)
plt6.get_yaxis().set_label_coords(-0.05,0.5)
#plt.subplots_adjust(hspace = ,wspace = 5)
#saving plot in .jpg format
plt.savefig('myplot03.jpg', format='jpeg',dpi=None, edgecolor='g', transparent=True, bbox_inches='tight')
I could have add my data, but no option seems better to represent it, a it is large(Suggestions are invited).
Thankyou for your time.
UPDATE:
The sample data for use is:
,Unnamed: 0,Unnamed: 0.1,index,YYYY,DOY,HR,MN,By,Bz,Vsw,Nsw,reconnection_rate,magnetopause_distance
0,225369,225369,263522,2003,184.0,0.0,2,4990000000.0,670000000.0,0.5928000000000002,1.49e-06,12573.256929898798,10.858269653698725
1,225370,225370,263523,2003,184.0,0.0,3,4080000000.0,390000000.0000001,0.5825,1.47e-06,11009.972803627901,10.946523155327649
2,225371,225371,263524,2003,184.0,0.0,4,4300000000.000002,-110000000.0,0.5675,1.4499999999999999e-06,13184.030740514894,11.067370358771116
1420,226789,226789,265056,2003,185.04340277777774,0.0017361111111111108,36,1100000000.0,4550000000.0,0.6971,4.589999999999999e-06,6.417619779966155,8.528263110991979
1421,226790,226790,265057,2003,185.04340277777774,0.0017361111111111108,37,530000000.0,6020000000.0,0.7007000000000001,4.17e-06,0.09620386453014222,8.650894534287474
1422,226791,226791,265058,2003,185.04340277777774,0.0017361111111111108,38,-3110000000.0,6230000000.000001,0.6958000000000002,4.779999999999998e-06,187.4614444320996,8.476077939976314
2765,228134,228134,266471,2003,186.04340277777774,0.0017361111111111108,11,3510000000.0000005,-2500000000.0,0.7323999999999999,1.7399999999999999e-06,2791.603594666996,9.860980027267642
2766,228135,228135,266472,2003,186.04340277777774,0.0017361111111111108,12,2000000000.0,-4340000000.000002,0.7396,1.42e-06,102.51414877271608,10.167496717459473
4048,229417,229417,267898,2003,187.0,0.0,58,5280000000.000001,1240000000.0,0.6935,1.22e-06,12837.913793204994,10.654153236043083
4049,229418,229418,267899,2003,187.0,0.0,59,3470000000.0,3690000000.0,0.6901,1.24e-06,1136.6725720724482,10.642739751911767
Sorry it looks clumsy, but it will help you to understand what i want to do actually.
Use scipy.interpolate module:
Plot raw data
sr = df.set_index('DOY')['Bz']
x = sr.index.values
y = sr.values
tck, u = interpolate.splprep([x, y])
u = np.linspace(0, 1, num=100, endpoint=True)
out = interpolate.splev(u, tck)
plt.plot(x, y, 'b', out[0], out[1], 'r')
plt.show()
Plot using the mean for same DOY
sr = df.set_index('DOY')['Bz'].groupby('DOY').mean()
x = sr.index.values
y = sr.values
tck, u = interpolate.splprep([x, y])
u = np.linspace(0, 1, num=100, endpoint=True)
out = interpolate.splev(u, tck)
plt.plot(x, y, 'b', out[0], out[1], 'r')
plt.show()
Combined
sr = df.set_index('DOY')['Bz']
x = sr.index.values
y = sr.values
sr2 = sr.groupby(level=0).mean()
x2 = sr2.index.values
y2 = sr2.values
tck, u = interpolate.splprep([x2, y2])
u = np.linspace(0, 1, num=100, endpoint=True)
out = interpolate.splev(u, tck)
plt.plot(x, y, 'b', out[0], out[1], 'r')
plt.show()

Real time live graphs in Jupyter Notebook

I have just started learning python to plot realtime gragh. I have tried solutions provided on stackoverflow but none of them are working. Below is my code and it isn't woorking. Please help
import numpy as np
import matplotlib.pyplot as plt
import pyautogui as pg
from matplotlib.animation import FuncAnimation
%matplotlib notebook
binSize = 512
# fig(ax1,ax2) = plt.subplots(2,figsize=(12,6))
f = []
def animate(i):
try:
while True:
x, y = pg.position()
f.append(x)
except KeyboardInterrupt:
print('')
# f.append(15)
if len(f)<binSize :
plt.cla()
plt.plot(f, color='c',LineWidth=1.5,label="Noisy")
else:
plt.cla()
plt.plot(f[-binSize:],color='c',LineWidth=1.5,label="Noisy")
ani = FuncAnimation(plt.gcf(),animate,interval=1);
So I have updated the code and trying to draw two subplots but after sometime
Upper graph stopped clearing the canvas (Mouse X coordinates)
Lower graph stopped updating the plot (FFT)
When data grows beyond the binSize, notebook freezes and plots update really slowly
%matplotlib notebook
binSize = 256
# fig(ax1,ax2) = plt.subplots(2,figsize=(12,6))
f = []
t = 0
dt = 1
fig,axs = plt.subplots(2,1)
def animate(i):
x, y = pg.position()
f.append(x)
n = len(f)
if n<binSize :
plt.sca(axs[0])
plt.cla()
plt.plot(f, color='c',LineWidth=1.5,label="MOUSE")
else:
fhat = np.fft.fft(f,binSize)
PSD = fhat*np.conj(fhat)/binSize
freq = (1/(dt*binSize))*np.arange(binSize)
L = np.arange(1,np.floor(binSize/2),dtype='int')
# update the code third time
axs[0].clear()
axs[0].plot(f[-binSize:], color='c',LineWidth=1.5,label="MOUSE")
# axs[0].xlim(0,binSize) # this stopped the FFT graph to be plotted
# plt.cla()
axs[1].clear()
axs[1].plot(freq[L],PSD[L],color='r',LineWidth=2,label="FFT")
# plt.xlim(t[0],t[-1])
# plt.legend()
# plt.sca(axs[1])
# plt.plot(freq[L],PSD[L],color='c',LineWidth=2,label="Mouse FFT")
# plt.xlim(0,300)
# plt.legend()
# plt.cla()
# plt.plot(f[-binSize:],color='c',LineWidth=1.5,label="Mouse")
ani = FuncAnimation(plt.gcf(),animate,interval=dt)
To make it faster you may reduce data like in other answer
f.pop(0)
I use also different method to update plot which works much faster on my computer.
I create empty plots at start
# needs `,` to get first element from list
p1, = axs[0].plot([], [], color='c', LineWidth=1.5, label="MOUSE")
p2, = axs[1].plot([], [], color='r', LineWidth=2, label="FFT")
and later only update data in plots without clear() and plot() again
xdata = range(len(f))
ydata = f
p1.set_data(xdata, ydata)
and
# replace data in plot
xdata = range(binSize)
ydata = f[-binSize:]
p1.set_data(xdata, ydata)
#p1.set_xdata(xdata)
#p1.set_ydata(ydata)
# replace data in plot
xdata = freq[:(binSize//2)]
ydata = PSD[:(binSize//2)]
p2.set_data(xdata, ydata)
It needs only to run code which rescale plot
# rescale view
axs[0].relim()
axs[0].autoscale_view(True,True,True)
axs[1].relim()
axs[1].autoscale_view(True,True,True)
animate() has to also return new plots
# return plots
return p1, p2
And FuncAnimation() has to blit them
ani = FuncAnimation(..., blit=True)
EDIT:
Animation works much, much faster also because I run it normally python script.py, not in Jupuyter Notebook
EDIT:
when I run normally I found one problem which I could find solution: it doesn't update values/ticks on axes. Jupyter Notebook doesn't have this problem.
import numpy as np
import matplotlib.pyplot as plt
import pyautogui as pg
from matplotlib.animation import FuncAnimation
%matplotlib notebook
binSize = 256
f = []
t = 0
dt = 1
fig, axs = plt.subplots(2, 1)
# needs `,` to get first element from list
p1, = axs[0].plot([], [], color='c', LineWidth=1.5, label="MOUSE")
p2, = axs[1].plot([], [], color='r', LineWidth=2, label="FFT")
freq = np.arange(binSize)/(dt*binSize)
def animate(i):
x, y = pg.position()
n = len(f)
if n < binSize :
f.append(x)
# replace data in plot
xdata = range(len(f))
ydata = f
p1.set_data(xdata, ydata)
#p1.set_xdata(xdata)
#p1.set_ydata(ydata)
else:
f.pop(0)
f.append(x)
fhat = np.fft.fft(f, binSize)
PSD = fhat * np.conj(fhat) / binSize
# replace data in plot
#xdata = range(binSize)
ydata = f[-binSize:]
#p1.set_data(xdata, ydata)
#p1.set_xdata(xdata)
p1.set_ydata(ydata)
# replace data in plot
xdata = freq[:(binSize//2)]
ydata = PSD[:(binSize//2)]
p2.set_data(xdata, ydata)
# rescale view
axs[0].relim()
axs[0].autoscale_view(True,True,True)
axs[1].relim()
axs[1].autoscale_view(True,True,True)
# return plots
return p1, p2
ani = FuncAnimation(plt.gcf(), animate, interval=dt, blit=True)
plt.show()
You should try this. Instead of clearing the plt clear axs[0] and so on. Also, instead of plotting on plt.plot, plot on axs[0].plot
%matplotlib notebook
binSize = 256
# fig(ax1,ax2) = plt.subplots(2,figsize=(12,6))
f = []
t = 0
dt = 1
fig,axs = plt.subplots(2,1)
plt.sca(axs[0])
plt.sca(axs[1])
def animate(i):
x, y = pg.position()
n = len(f)
if n<binSize :
f.append(x*100)
axs[0].clear()
axs[0].plot(f, color='c',LineWidth=1.5,label="MOUSE")
else:
f.pop(0)
f.append(x)
fhat = np.fft.fft(f,binSize)
PSD = fhat*np.conj(fhat)/binSize
freq = (1/(dt*binSize))*np.arange(binSize)
L = np.arange(1,np.floor(binSize/2),dtype='int') # index array of [1,2,3..... binsize/2] type int
axs[0].clear()
axs[0].plot(f[-binSize:], color='c',LineWidth=1.5,label="MOUSE")
axs[1].clear()
axs[1].plot(freq[L],PSD[L],color='r',LineWidth=2,label="FFT")
ani = FuncAnimation(plt.gcf(),animate,interval=dt)
plt.show()

Draw a circle on the plot that follows the mouse [duplicate]

I tried to write a simple script which updates a scatter plot for every timestep t. I wanted to do it as simple as possible. But all it does is to open a window where I can see nothing. The window just freezes. It is maybe just an small error, but I can not find it.
The the data.dat has the format
x y
Timestep 1 1 2
3 1
Timestep 2 6 3
2 1
(the file contains just the numbers)
import numpy as np
import matplotlib.pyplot as plt
import time
# Load particle positioins
with open('//home//user//data.dat', 'r') as fp:
particles = []
for line in fp:
line = line.split()
if line:
line = [float(i) for i in line]
particles.append(line)
T = 100
numbParticles = 2
x, y = np.array([]), np.array([])
plt.ion()
plt.figure()
plt.scatter(x,y)
for t in range(T):
plt.clf()
for k in range(numbP):
x = np.append(x, particles[numbParticles*t+k][0])
y = np.append(y, particles[numbParticles*t+k][1])
plt.scatter(x,y)
plt.draw()
time.sleep(1)
x, y = np.array([]), np.array([])
The simplest, cleanest way to make an animation is to use the matplotlib.animation module.
Since a scatter plot returns a matplotlib.collections.PathCollection, the way to update it is to call its set_offsets method. You can pass it an array of shape (N, 2) or a list of N 2-tuples -- each 2-tuple being an (x,y) coordinate.
For example,
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
T = 100
numbParticles = 2
particles = np.random.random((T,numbParticles)).tolist()
x, y = np.array([]), np.array([])
def init():
pathcol.set_offsets([[], []])
return [pathcol]
def update(i, pathcol, particles):
pathcol.set_offsets(particles[i])
return [pathcol]
fig = plt.figure()
xs, ys = zip(*particles)
xmin, xmax = min(xs), max(xs)
ymin, ymax = min(ys), max(ys)
ax = plt.axes(xlim=(xmin, xmax), ylim=(ymin, ymax))
pathcol = plt.scatter([], [], s=100)
anim = animation.FuncAnimation(
fig, update, init_func=init, fargs=(pathcol, particles), interval=1000, frames=T,
blit=True, repeat=True)
plt.show()
I finally found a solution. You can do it simply by using this script. I tried to keep it simple:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# Helps me to get the data from the file I want to plot
N = 0
# Load particle positioins
with open('//home//user//data.dat', 'r') as fp:
particles = []
for line in fp:
line = line.split()
particles.append(line)
# Create new Figure and an Axes which fills it.
fig = plt.figure(figsize=(7, 7))
ax = fig.add_axes([0, 0, 1, 1], frameon=True)
border = 100
ax.set_xlim(-border, border), ax.set_xticks([])
ax.set_ylim(-border, border), ax.set_yticks([])
# particle data
p = 18 # number of particles
myPa = np.zeros(p, dtype=[('position', float, 2)])
# Construct the scatter which we will update during animation
scat = ax.scatter(myPa['position'][:, 0], myPa['position'][:, 1])
def update(frame_number):
# New positions
myPa['position'][:] = particles[N*p:N*p+p]
# Update the scatter collection, with the new colors, sizes and positions.
scat.set_offsets(myPa['position'])
increment()
def increment():
global N
N = N+1
# Construct the animation, using the update function as the animation director.
animation = FuncAnimation(fig, update, interval=20)
plt.show()

Make a point move on the plot without clearing earlier plots in matplotlib [duplicate]

I tried to write a simple script which updates a scatter plot for every timestep t. I wanted to do it as simple as possible. But all it does is to open a window where I can see nothing. The window just freezes. It is maybe just an small error, but I can not find it.
The the data.dat has the format
x y
Timestep 1 1 2
3 1
Timestep 2 6 3
2 1
(the file contains just the numbers)
import numpy as np
import matplotlib.pyplot as plt
import time
# Load particle positioins
with open('//home//user//data.dat', 'r') as fp:
particles = []
for line in fp:
line = line.split()
if line:
line = [float(i) for i in line]
particles.append(line)
T = 100
numbParticles = 2
x, y = np.array([]), np.array([])
plt.ion()
plt.figure()
plt.scatter(x,y)
for t in range(T):
plt.clf()
for k in range(numbP):
x = np.append(x, particles[numbParticles*t+k][0])
y = np.append(y, particles[numbParticles*t+k][1])
plt.scatter(x,y)
plt.draw()
time.sleep(1)
x, y = np.array([]), np.array([])
The simplest, cleanest way to make an animation is to use the matplotlib.animation module.
Since a scatter plot returns a matplotlib.collections.PathCollection, the way to update it is to call its set_offsets method. You can pass it an array of shape (N, 2) or a list of N 2-tuples -- each 2-tuple being an (x,y) coordinate.
For example,
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
T = 100
numbParticles = 2
particles = np.random.random((T,numbParticles)).tolist()
x, y = np.array([]), np.array([])
def init():
pathcol.set_offsets([[], []])
return [pathcol]
def update(i, pathcol, particles):
pathcol.set_offsets(particles[i])
return [pathcol]
fig = plt.figure()
xs, ys = zip(*particles)
xmin, xmax = min(xs), max(xs)
ymin, ymax = min(ys), max(ys)
ax = plt.axes(xlim=(xmin, xmax), ylim=(ymin, ymax))
pathcol = plt.scatter([], [], s=100)
anim = animation.FuncAnimation(
fig, update, init_func=init, fargs=(pathcol, particles), interval=1000, frames=T,
blit=True, repeat=True)
plt.show()
I finally found a solution. You can do it simply by using this script. I tried to keep it simple:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# Helps me to get the data from the file I want to plot
N = 0
# Load particle positioins
with open('//home//user//data.dat', 'r') as fp:
particles = []
for line in fp:
line = line.split()
particles.append(line)
# Create new Figure and an Axes which fills it.
fig = plt.figure(figsize=(7, 7))
ax = fig.add_axes([0, 0, 1, 1], frameon=True)
border = 100
ax.set_xlim(-border, border), ax.set_xticks([])
ax.set_ylim(-border, border), ax.set_yticks([])
# particle data
p = 18 # number of particles
myPa = np.zeros(p, dtype=[('position', float, 2)])
# Construct the scatter which we will update during animation
scat = ax.scatter(myPa['position'][:, 0], myPa['position'][:, 1])
def update(frame_number):
# New positions
myPa['position'][:] = particles[N*p:N*p+p]
# Update the scatter collection, with the new colors, sizes and positions.
scat.set_offsets(myPa['position'])
increment()
def increment():
global N
N = N+1
# Construct the animation, using the update function as the animation director.
animation = FuncAnimation(fig, update, interval=20)
plt.show()

Animate a segment in a 3d space

I got a .dat file which contains the coordinates of a segment in 3d space.
The file has several lines, each single line characterizes the position at a particular time.
I tried this code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D
dati = np.loadtxt('dati.dat')
t=0
p1=[dati[t,1],dati[t,2],dati[t,3]]
p2=[dati[t,4],dati[t,5],dati[t,6]]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
seg,=ax.plot(p1,p2)
def updateFigure(t,dati,seg):
p1=[dati[t,1],dati[t,2],dati[t,3]]
p2=[dati[t,4],dati[t,5],dati[t,6]]
seg.set_data(p1,p2)
return seg,
ani=animation.FuncAnimation(fig, updateFigure,iMax, fargs=(dati,seg), interval=100, blit=True)
plt.show()
The program doesn't report errors but the figure doesn't move.
The same code, a bit modified, in the 2d space works..
Instead of calling set_data, you could set seg._verts3d directly, though note that manipulating the private variable _verts3d is relying on an implementation detail, not part of the Line3D public interface:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D
iMax = N = 500
theta = np.linspace(0, 6*np.pi, N)
x = np.cos(theta)
y = np.sin(theta)
z = np.linspace(0, 1, N)
step = 10
dati = np.column_stack(
[theta, x, np.roll(x, -step), np.roll(x, -2*step)
, y, np.roll(y, -step), np.roll(y, -2*step)
, z, np.roll(z, -step), np.roll(z, -2*step)])
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
seg, = plt.plot([], [])
ax.set_xlim3d(-1, 1)
ax.set_ylim3d(-1, 1)
ax.set_zlim3d(0, 1)
def init():
return seg,
def updateFigure(t):
p1 = dati[t, 1:4]
p2 = dati[t, 4:7]
p3 = dati[t, 7:10]
seg._verts3d = (p1, p2, p3)
return seg,
ani = animation.FuncAnimation(
fig, updateFigure
, init_func=init
, frames=iMax
, interval=5, blit=True)
plt.show()

Categories

Resources