-
Notifications
You must be signed in to change notification settings - Fork 1
/
aniplot.py
146 lines (102 loc) · 4.02 KB
/
aniplot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue May 16 15:19:56 2023
@author: wouter
"""
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import axes3d
import os, sys
import numpy as np
##### TO CREATE A SERIES OF PICTURES
def make_views(ax,angles,elevation=None, width=4, heigth = 3,
prefix='tmprot_',**kwargs):
"""
Makes jpeg pictures of the given 3d ax, with different angles.
Args:
ax (3D axis): te ax
angles (list): the list of angles (in degree) under which to
take the picture.
width,heigth (float): size, in inches, of the output images.
prefix (str): prefix for the files created.
Returns: the list of files created (for later removal)
"""
files = []
ax.figure.set_size_inches(width,heigth)
for i,angle in enumerate(angles):
ax.view_init(elev = elevation, azim=angle)
fname = '%s%03d.jpeg'%(prefix,i)
ax.figure.savefig(fname)
files.append(fname)
return files
##### TO TRANSFORM THE SERIES OF PICTURE INTO AN ANIMATION
def make_movie(files,output, fps=10,bitrate=1800,**kwargs):
"""
Uses mencoder, produces a .mp4/.ogv/... movie from a list of
picture files.
"""
output_name, output_ext = os.path.splitext(output)
command = { '.mp4' : 'mencoder "mf://%s" -mf fps=%d -o %s.mp4 -ovc lavc\
-lavcopts vcodec=msmpeg4v2:vbitrate=%d'
%(",".join(files),fps,output_name,bitrate)}
command['.ogv'] = command['.mp4'] + '; ffmpeg -i %s.mp4 -r %d %s'%(output_name,fps,output)
print (command[output_ext])
output_ext = os.path.splitext(output)[1]
os.system(command[output_ext])
def make_gif(files,output,delay=100, repeat=True,**kwargs):
"""
Uses imageMagick to produce an animated .gif from a list of
picture files.
"""
loop = -1 if repeat else 0
os.system('convert -delay %d -loop %d %s %s'
%(delay,loop," ".join(files),output))
def make_strip(files,output,**kwargs):
"""
Uses imageMagick to produce a .jpeg strip from a list of
picture files.
"""
os.system('montage -tile 1x -geometry +0+0 %s %s'%(" ".join(files),output))
##### MAIN FUNCTION
def rotanimate(ax, angles, output, **kwargs):
"""
Produces an animation (.mp4,.ogv,.gif,.jpeg,.png) from a 3D plot on
a 3D ax
Args:
ax (3D axis): the ax containing the plot of interest
angles (list): the list of angles (in degree) under which to
show the plot.
output : name of the output file. The extension determines the
kind of animation used.
**kwargs:
- width : in inches
- heigth: in inches
- framerate : frames per second
- delay : delay between frames in milliseconds
- repeat : True or False (.gif only)
"""
output_ext = os.path.splitext(output)[1]
files = make_views(ax,angles, **kwargs)
D = { '.mp4' : make_movie,
'.ogv' : make_movie,
'.gif': make_gif ,
'.jpeg': make_strip,
'.png':make_strip}
D[output_ext](files,output,**kwargs)
for f in files:
os.remove(f)
##### EXAMPLE
# if __name__ == '__main__':
# fig = plt.figure()
# ax = fig.add_subplot(111, projection='3d')
# X, Y, Z = axes3d.get_test_data(0.05)
# s = ax.plot_surface(X, Y, Z, cmap=cm.jet)
# plt.axis('off') # remove axes for visual appeal
# angles = np.linspace(0,360,21)[:-1] # Take 20 angles between 0 and 360
# # create an animated gif (20ms between frames)
# rotanimate(ax, angles,'movie.gif',delay=20)
# # create a movie with 10 frames per seconds and 'quality' 2000
# rotanimate(ax, angles,'movie.mp4',fps=10,bitrate=2000)
# # create an ogv movie
# rotanimate(ax, angles, 'movie.ogv',fps=10)