Catalog
A rotated flower plotted via python (animated)

All the python study code can be found in my own repository.

As a study of Python data visualization, I plot a 3D follower and let it rotated horizontally. The packages I used are

1
import subprocess
2
import numpy as np
3
from matplotlib import pyplot as plt
4
from matplotlib import animation
5
from mpl_toolkits.mplot3d import Axes3D
6
from matplotlib import cm
7
from matplotlib.ticker import LinearLocator

The follower is defined via the following data

1
[x, t] = np.meshgrid(np.array(range(25))/24.0,
2
                     np.arange(0, 575.5, 0.5)/575*17*np.pi-2*np.pi)
3
4
p = (np.pi/2)*np.exp(-t/(8*np.pi))
5
6
u = 1-(1-np.mod(3.6*t, 2*np.pi)/np.pi)**4/2
7
8
y = 2*(x**2-x)**2*np.sin(p)
9
10
r = u*(x*np.sin(p)+y*np.cos(p))

and the init function is as follows

1
def init():
2
3
    surf = ax.plot_surface(r*np.cos(t), r*np.sin(t), u*(x*np.cos(p)-y*np.sin(p)),
4
                           rstride=1, cstride=1, cmap=cm.gist_rainbow_r, linewidth=0, antialiased=True)
5
    plt.axis('off')
6
    return fig,

An animate function

1
def animate(i):
2
    # azimuth angle : 0 deg to 360 deg
3
    ax.view_init(elev=10, azim=i*4)
4
    return fig,

plot and save

1
n = 'rotate_azimuth_angle_3d_flower'
2
# ani.save(fn+'.mp4',writer='ffmpeg',fps=1000/50)
3
ani.save(fn+'.gif', writer='imagemagick', fps=1000/50)
4
5
cmd = 'magick convert %s.gif -fuzz 5%% -layers Optimize %s_r.gif' % (fn, fn)
6
subprocess.check_output(cmd)
7
8
9
plt.rcParams['animation.html'] = 'html5'
10
ani

The generated flowers are as follows
Flowers

Author: Knifelee
Link: https://knifelees3.github.io/2019/12/25/A_En_Python_PlotStudy3_RotatedFlower/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Donate
  • 微信
  • 支付寶

Comment