使用pymatplotlib库画函数图

matplotlib库提供许多函数画静态图以及动态图。

下面展示效果图和源码:

demo1:

test

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
import matplotlib.pyplot as plt
import numpy as np

#author:zzm
#time: 2019.4.25
s = np.arange(0, np.pi*2, 0.01)

def x(s,a,p):
return a*np.sin(p*s)

def y(s,b,q,t):
return b*np.sin(q*s+t)

def lisa(a,b,n,t,posit):
p = 1
q = n*p
plt.subplot(3,3,posit)
plt.grid()
plt.plot(x(s,a,p),y(s,b,q,t))

lisa(1,1,1,0,1)
lisa(1,1,1,np.pi/2,2)
lisa(1,1,1,1,3)
lisa(1,1,2,0,7)
lisa(1,1,1,np.pi/2,8)
lisa(1,1,1,1,9)

plt.savefig('test', dpi = 600)
plt.show()

demo2:

sinx_dot

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
import numpy as np 
import matplotlib.pyplot as plt
from matplotlib import animation


fig, ax = plt.subplots()
x = np.linspace(0, 2*np.pi, 200)
y = np.sin(x)
l = ax.plot(x, y)
dot, = ax.plot([], [], 'ro')

def init():
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1, 1)
return l

def gen_dot():
for i in np.linspace(0, 2*np.pi, 200):
newdot = [i, np.sin(i)]
yield newdot

def update_dot(newd):
dot.set_data(newd[0], newd[1])
return dot,

ani = animation.FuncAnimation(fig, update_dot, frames = gen_dot, interval = 100, init_func=init)
ani.save('sin_dot.gif', writer='pillow', fps=30)

plt.show()

demo3:

roll

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
import numpy as np 
import matplotlib.pyplot as plt
import matplotlib.animation as animation

fig = plt.figure(figsize=(6, 6))
ax = plt.gca()
ax.grid()
ln1, = ax.plot([], [], '-', lw=2)
ln2, = ax.plot([], [], '-', color='r', lw=2)
theta = np.linspace(0, 2*np.pi, 100)
r_out = 1
r_in = 0.5

def init():
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)
x_out = [r_out*np.cos(theta[i]) for i in range(len(theta))]
y_out = [r_out*np.sin(theta[i]) for i in range(len(theta))]
ln1.set_data(x_out, y_out)
return ln1,

def update(i):
x_in = [(r_out-r_in)*np.cos(theta[i])+r_in*np.cos(theta[j]) for j in range(len(theta))]
y_in = [(r_out-r_in)*np.sin(theta[i])+r_in*np.sin(theta[j]) for j in range(len(theta))]
ln2.set_data(x_in, y_in)
return ln2,

ani = animation.FuncAnimation(fig, update, range(len(theta)), init_func=init, interval=30)
ani.save('roll.gif', writer='pillow', fps=100)

plt.show()

demo4:

danbai

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
from math import sin, cos
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt
import matplotlib.animation as animation

g = 9.8
leng = 1.0

def pendulum_equations(w, t, l):
th, v = w
dth = v
dv = - g/l * sin(th)
return dth, dv

t = np.arange(0, 20, 0.1)
track = odeint(pendulum_equations, (1.0, 0), t, args=(leng,))
xdata = [leng*sin(track[i, 0]) for i in range(len(track))]
ydata = [-leng*cos(track[i, 0]) for i in range(len(track))]

figure, ax = plt.subplots()
ax.grid()
line, = ax.plot([], [], 'H-', color='darkred', lw=2)
time_template = 'time = %.1fs'
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)

def init():
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)
time_text.set_text('')
return line, time_text

def update(i):
newx = [0, xdata[i]]
newy = [0, ydata[i]]
line.set_data(newx, newy)
time_text.set_text(time_template %(0.1*i))
return line, time_text

ani = animation.FuncAnimation(figure, update, range(1, len(xdata)), init_func=init, interval=50)
ani.save('f2.gif', writer='pillow', fps=100)
plt.show()

demo5:

sin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = ax.plot([], [], 'r-', animated=False) #,表示创建tuple类

def init():
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1, 1)
return ln,

def update(frame):
xdata.append(frame)
ydata.append(np.sin(frame))
ln.set_data(xdata, ydata)
return ln,

ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
init_func=init, blit=True)
ani.save('f1.gif', writer='pillow', fps=30)

plt.show()

demo6:

wuli
wuli
wuli
wuli
wuli
wuli
wuli
wuli
wuli
wuli
wuli
wuli
wuli
wuli
wuli
wuli
wuli
wuli

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
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

#author: zzm
#time: 2019.4.25
#制作李萨如图形动态gif
#李萨如图形的轨迹方程:
# x = A1cos(wt+β1)
# y = A2cos(wt+β2)

figure, ax = plt.subplots()
xdata, ydata = [], []
dot, = ax.plot([], [], 'r-', animated=False)


#def x(s):
# return np.cos(3*s+np.pi)

def y(s):
return 4/np.pi/3*np.sin(3*s)+4/np.pi/3*np.sin(3*s)

def init():
ax.set_xlim(-np.pi, np.pi)
ax.set_ylim(-2, 2)
return dot,

def update(frame):
xdata.append(frame)
ydata.append(y(frame))
dot.set_data(xdata, ydata)
return dot,

photo = FuncAnimation(figure, update, frames=np.linspace(-np.pi,np.pi, 128),
init_func=init, blit=True)

photo.save('zz2.gif', writer='pillow', fps=30)
plt.show()

Donate? comment?