python中正弦函数模块_python3 的matplotlib的4种办法制作动态sin函数程序详述

本文主要是介绍python中正弦函数模块_python3 的matplotlib的4种办法制作动态sin函数程序详述,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.说明:

1.1 推荐指数:★★★

1.2 python的基础知识复习,通过生动的sin函数制作来复习return和yield,列表、函数定义等知识。

1.3 熟悉matplotlib作图相关知识。

1.4 加深理解sin函数,为以后圆的理解打下坚实基础,cos重复不解释了,将sin适当修改即可。

f5cab809d1d475de28b932ee3d311766.png

2.return法,基本方法,代码:

#---导出模块---

import numpy as np

from matplotlib import pyplot as plt

from matplotlib import animation

#定义画布,默认值,这个fig需要,虽然默认大小设置,fig需要挂在动画上

fig = plt.figure()

#坐标轴刻度

ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#color='blue'=蓝色,否则默认为清淡蓝色

line, = ax.plot([], [], lw=2,color='blue')

# 因为动画,所以初始化列表线条

def init():

line.set_data([], [])

return line, #注意逗号

#定义动画

def animate(i):

#x取值范围从0~2,等差数列,分成1000,越大线条越平滑

x = np.linspace(0, 2, 1000)

#动画x和y的值与i的从0~i的取值有关,才动起来

y = np.sin(2 * np.pi * (x - 0.01 * i))

line.set_data(x, y)

return line, #注意逗号

#将fig挂在动画上面

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

#如果需要保存动画,就这样

#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

#标题名称

plt.title('Sin-a-subplot')

plt.show()

图1

99c99cad98eda9f8f85600e6d779746f.gif

3.np.nan法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

#---定义画布---重点讲到区别和含义---

fig, ax = plt.subplots()

#---函数定义法---讲的很清楚了,很多遍---

#复习一下

#x的坐标取值范围,arange法一般是-2π到2π,这里是从0取,0.01,数值越小曲线越平滑

#注意与linspace取等差数列的区别

x = np.arange(0, 2*np.pi, 0.01)

#这是一步并2步了,相当于y=np.sin(x)

line, = ax.plot(x, np.sin(x))

#---初始化---注意np.nan(NaN)知识复习---

def init():

line.set_ydata([np.nan] * len(x))

#等同于下面

#line.set_ydata([] * len(x))

return line,

'''

有两种丢失数据:

None

np.nan(NaN)

None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。

np.nan(NaN)

np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。

但可以使用np.nan*()函数来计算nan,此时视nan为0。

'''

#---定义动画---

def animate(i):

#line.set_ydata(np.sin(x + i / 100))

#与上面一样效果

line.set_ydata(np.sin(x + 0.01 * i))

return line,

#fig的挂在动画上面

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2, blit=True, save_count=50)

# ani.save("movie.mp4")

plt.show()

图2

e03049dda42efdade55961dfececacb8.gif

4.带红色小圆点的yield法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

#---定义画布和ax轴---

fig, ax = plt.subplots()

'''

等价于:fig, ax = plt.subplots(11)=fig, ax = plt.subplots(1,1)

=fig, ax1 = plt.subplot()

或者:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

'''

#---x和y的函数关系---

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

#画正弦函数线

l = ax.plot(x, y)

#运动的圆球,ro=就是red的o=红色的圆球,如果是o,就是默认颜色的圆球

#挂在正弦函数线上的球,初始化坐标为空

dot, = ax.plot([], [], 'ro')

#---初始化定义红色圆球的ax坐标取值范围---

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

#---产生圆球的坐标取值范围,符合正弦函数---

def gen_dot():

#i类似x坐标,np.sin(i)类似y坐标

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

#通过yield函数产生

yield newdot

'''

首先比较下return 与 yield的区别:

return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。

yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束

带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。

'''

#---更新小圆球的位置---

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='imagemagick', fps=30)

plt.show()

图3

e3488a4b39eac61356f1d74c1a7c4667.gif

5 timer法:最新matplotlib好像淘汰了,可以运行,但是报错,可以不用管它,学习技术而已。代码如下:

#---导出模块---

import matplotlib.pyplot as plt

import numpy as np

#---fig和ax放在一起

fig, ax = plt.subplots()

#---初始化定义---

points_dot = 100

#复习一下列表知识,一个列表里有100个相同的0的列表

sin_list = [0] * points_dot

indx = 0

#---画正弦函数线---初始化---

line_sin, = ax.plot(range(points_dot), sin_list, label='sin-d', color='blue')

#---定义sin输出函数---

def sin_output(ax):

global indx, sin_list, line_sin

if indx == 20:

indx = 0

indx += 1

#更新sin列表,初始化全是100个0,更新后就是正弦函数的y坐标

sin_list = sin_list[1:] + [np.sin((indx / 10) * np.pi)]

#看看ydata就是y坐标的意思

line_sin.set_ydata(sin_list)

#从新画正弦函数动态曲线

ax.draw_artist(line_sin)

ax.figure.canvas.draw()

#计时器在新版的matplotlib中已经删除,目前能显示,但是报错,可以不管,暂时学学技术,了解一下

timer = fig.canvas.new_timer(interval=100)

timer.add_callback(sin_output, ax)

timer.start()

#x和y轴的刻度定义

ax.set_xlim([0, points_dot])

ax.set_ylim([-2, 2])

#ax.set_autoscale_on(False) #默认False

#0~100,每隔10取刻度值

ax.set_xticks(range(0, points_dot, 10))

ax.set_yticks(range(-2, 3, 1))

#显示网格

ax.grid(True)

#显示图例,固定位置=中心上面

ax.legend(loc='upper center', ncol=4)

plt.show()

'''

报错:

RuntimeError: wrapped C/C++ object of type QTimer has been deleted

提示新版的matplotlib已经删除timer了

'''

图4

1ec094bb91f0dbd79ff67bc62c5d449d.gif

希望喜欢,收藏之后好好复习,生动的图像,加深对python的基础知识的理解,熟悉matplotlib作图,以后拿来就用,通俗易懂。感谢作者分享-http://bjbsair.com/2020-04-07/tech-info/30776.html

1.说明:

1.1 推荐指数:★★★

1.2 python的基础知识复习,通过生动的sin函数制作来复习return和yield,列表、函数定义等知识。

1.3 熟悉matplotlib作图相关知识。

1.4 加深理解sin函数,为以后圆的理解打下坚实基础,cos重复不解释了,将sin适当修改即可。

f5cab809d1d475de28b932ee3d311766.png

2.return法,基本方法,代码:

#---导出模块---

import numpy as np

from matplotlib import pyplot as plt

from matplotlib import animation

#定义画布,默认值,这个fig需要,虽然默认大小设置,fig需要挂在动画上

fig = plt.figure()

#坐标轴刻度

ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#color='blue'=蓝色,否则默认为清淡蓝色

line, = ax.plot([], [], lw=2,color='blue')

# 因为动画,所以初始化列表线条

def init():

line.set_data([], [])

return line, #注意逗号

#定义动画

def animate(i):

#x取值范围从0~2,等差数列,分成1000,越大线条越平滑

x = np.linspace(0, 2, 1000)

#动画x和y的值与i的从0~i的取值有关,才动起来

y = np.sin(2 * np.pi * (x - 0.01 * i))

line.set_data(x, y)

return line, #注意逗号

#将fig挂在动画上面

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

#如果需要保存动画,就这样

#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

#标题名称

plt.title('Sin-a-subplot')

plt.show()

图1

99c99cad98eda9f8f85600e6d779746f.gif

3.np.nan法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

#---定义画布---重点讲到区别和含义---

fig, ax = plt.subplots()

#---函数定义法---讲的很清楚了,很多遍---

#复习一下

#x的坐标取值范围,arange法一般是-2π到2π,这里是从0取,0.01,数值越小曲线越平滑

#注意与linspace取等差数列的区别

x = np.arange(0, 2*np.pi, 0.01)

#这是一步并2步了,相当于y=np.sin(x)

line, = ax.plot(x, np.sin(x))

#---初始化---注意np.nan(NaN)知识复习---

def init():

line.set_ydata([np.nan] * len(x))

#等同于下面

#line.set_ydata([] * len(x))

return line,

'''

有两种丢失数据:

None

np.nan(NaN)

None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。

np.nan(NaN)

np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。

但可以使用np.nan*()函数来计算nan,此时视nan为0。

'''

#---定义动画---

def animate(i):

#line.set_ydata(np.sin(x + i / 100))

#与上面一样效果

line.set_ydata(np.sin(x + 0.01 * i))

return line,

#fig的挂在动画上面

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2, blit=True, save_count=50)

# ani.save("movie.mp4")

plt.show()

图2

e03049dda42efdade55961dfececacb8.gif

4.带红色小圆点的yield法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

#---定义画布和ax轴---

fig, ax = plt.subplots()

'''

等价于:fig, ax = plt.subplots(11)=fig, ax = plt.subplots(1,1)

=fig, ax1 = plt.subplot()

或者:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

'''

#---x和y的函数关系---

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

#画正弦函数线

l = ax.plot(x, y)

#运动的圆球,ro=就是red的o=红色的圆球,如果是o,就是默认颜色的圆球

#挂在正弦函数线上的球,初始化坐标为空

dot, = ax.plot([], [], 'ro')

#---初始化定义红色圆球的ax坐标取值范围---

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

#---产生圆球的坐标取值范围,符合正弦函数---

def gen_dot():

#i类似x坐标,np.sin(i)类似y坐标

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

#通过yield函数产生

yield newdot

'''

首先比较下return 与 yield的区别:

return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。

yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束

带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。

'''

#---更新小圆球的位置---

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='imagemagick', fps=30)

plt.show()

图3

e3488a4b39eac61356f1d74c1a7c4667.gif

5 timer法:最新matplotlib好像淘汰了,可以运行,但是报错,可以不用管它,学习技术而已。代码如下:

#---导出模块---

import matplotlib.pyplot as plt

import numpy as np

#---fig和ax放在一起

fig, ax = plt.subplots()

#---初始化定义---

points_dot = 100

#复习一下列表知识,一个列表里有100个相同的0的列表

sin_list = [0] * points_dot

indx = 0

#---画正弦函数线---初始化---

line_sin, = ax.plot(range(points_dot), sin_list, label='sin-d', color='blue')

#---定义sin输出函数---

def sin_output(ax):

global indx, sin_list, line_sin

if indx == 20:

indx = 0

indx += 1

#更新sin列表,初始化全是100个0,更新后就是正弦函数的y坐标

sin_list = sin_list[1:] + [np.sin((indx / 10) * np.pi)]

#看看ydata就是y坐标的意思

line_sin.set_ydata(sin_list)

#从新画正弦函数动态曲线

ax.draw_artist(line_sin)

ax.figure.canvas.draw()

#计时器在新版的matplotlib中已经删除,目前能显示,但是报错,可以不管,暂时学学技术,了解一下

timer = fig.canvas.new_timer(interval=100)

timer.add_callback(sin_output, ax)

timer.start()

#x和y轴的刻度定义

ax.set_xlim([0, points_dot])

ax.set_ylim([-2, 2])

#ax.set_autoscale_on(False) #默认False

#0~100,每隔10取刻度值

ax.set_xticks(range(0, points_dot, 10))

ax.set_yticks(range(-2, 3, 1))

#显示网格

ax.grid(True)

#显示图例,固定位置=中心上面

ax.legend(loc='upper center', ncol=4)

plt.show()

'''

报错:

RuntimeError: wrapped C/C++ object of type QTimer has been deleted

提示新版的matplotlib已经删除timer了

'''

图4

1ec094bb91f0dbd79ff67bc62c5d449d.gif

希望喜欢,收藏之后好好复习,生动的图像,加深对python的基础知识的理解,熟悉matplotlib作图,以后拿来就用,通俗易懂。感谢作者分享-http://bjbsair.com/2020-04-07/tech-info/30776.html

1.说明:

1.1 推荐指数:★★★

1.2 python的基础知识复习,通过生动的sin函数制作来复习return和yield,列表、函数定义等知识。

1.3 熟悉matplotlib作图相关知识。

1.4 加深理解sin函数,为以后圆的理解打下坚实基础,cos重复不解释了,将sin适当修改即可。

f5cab809d1d475de28b932ee3d311766.png

2.return法,基本方法,代码:

#---导出模块---

import numpy as np

from matplotlib import pyplot as plt

from matplotlib import animation

#定义画布,默认值,这个fig需要,虽然默认大小设置,fig需要挂在动画上

fig = plt.figure()

#坐标轴刻度

ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#color='blue'=蓝色,否则默认为清淡蓝色

line, = ax.plot([], [], lw=2,color='blue')

# 因为动画,所以初始化列表线条

def init():

line.set_data([], [])

return line, #注意逗号

#定义动画

def animate(i):

#x取值范围从0~2,等差数列,分成1000,越大线条越平滑

x = np.linspace(0, 2, 1000)

#动画x和y的值与i的从0~i的取值有关,才动起来

y = np.sin(2 * np.pi * (x - 0.01 * i))

line.set_data(x, y)

return line, #注意逗号

#将fig挂在动画上面

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

#如果需要保存动画,就这样

#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

#标题名称

plt.title('Sin-a-subplot')

plt.show()

图1

99c99cad98eda9f8f85600e6d779746f.gif

3.np.nan法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

#---定义画布---重点讲到区别和含义---

fig, ax = plt.subplots()

#---函数定义法---讲的很清楚了,很多遍---

#复习一下

#x的坐标取值范围,arange法一般是-2π到2π,这里是从0取,0.01,数值越小曲线越平滑

#注意与linspace取等差数列的区别

x = np.arange(0, 2*np.pi, 0.01)

#这是一步并2步了,相当于y=np.sin(x)

line, = ax.plot(x, np.sin(x))

#---初始化---注意np.nan(NaN)知识复习---

def init():

line.set_ydata([np.nan] * len(x))

#等同于下面

#line.set_ydata([] * len(x))

return line,

'''

有两种丢失数据:

None

np.nan(NaN)

None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。

np.nan(NaN)

np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。

但可以使用np.nan*()函数来计算nan,此时视nan为0。

'''

#---定义动画---

def animate(i):

#line.set_ydata(np.sin(x + i / 100))

#与上面一样效果

line.set_ydata(np.sin(x + 0.01 * i))

return line,

#fig的挂在动画上面

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2, blit=True, save_count=50)

# ani.save("movie.mp4")

plt.show()

图2

e03049dda42efdade55961dfececacb8.gif

4.带红色小圆点的yield法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

#---定义画布和ax轴---

fig, ax = plt.subplots()

'''

等价于:fig, ax = plt.subplots(11)=fig, ax = plt.subplots(1,1)

=fig, ax1 = plt.subplot()

或者:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

'''

#---x和y的函数关系---

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

#画正弦函数线

l = ax.plot(x, y)

#运动的圆球,ro=就是red的o=红色的圆球,如果是o,就是默认颜色的圆球

#挂在正弦函数线上的球,初始化坐标为空

dot, = ax.plot([], [], 'ro')

#---初始化定义红色圆球的ax坐标取值范围---

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

#---产生圆球的坐标取值范围,符合正弦函数---

def gen_dot():

#i类似x坐标,np.sin(i)类似y坐标

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

#通过yield函数产生

yield newdot

'''

首先比较下return 与 yield的区别:

return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。

yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束

带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。

'''

#---更新小圆球的位置---

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='imagemagick', fps=30)

plt.show()

图3

e3488a4b39eac61356f1d74c1a7c4667.gif

5 timer法:最新matplotlib好像淘汰了,可以运行,但是报错,可以不用管它,学习技术而已。代码如下:

#---导出模块---

import matplotlib.pyplot as plt

import numpy as np

#---fig和ax放在一起

fig, ax = plt.subplots()

#---初始化定义---

points_dot = 100

#复习一下列表知识,一个列表里有100个相同的0的列表

sin_list = [0] * points_dot

indx = 0

#---画正弦函数线---初始化---

line_sin, = ax.plot(range(points_dot), sin_list, label='sin-d', color='blue')

#---定义sin输出函数---

def sin_output(ax):

global indx, sin_list, line_sin

if indx == 20:

indx = 0

indx += 1

#更新sin列表,初始化全是100个0,更新后就是正弦函数的y坐标

sin_list = sin_list[1:] + [np.sin((indx / 10) * np.pi)]

#看看ydata就是y坐标的意思

line_sin.set_ydata(sin_list)

#从新画正弦函数动态曲线

ax.draw_artist(line_sin)

ax.figure.canvas.draw()

#计时器在新版的matplotlib中已经删除,目前能显示,但是报错,可以不管,暂时学学技术,了解一下

timer = fig.canvas.new_timer(interval=100)

timer.add_callback(sin_output, ax)

timer.start()

#x和y轴的刻度定义

ax.set_xlim([0, points_dot])

ax.set_ylim([-2, 2])

#ax.set_autoscale_on(False) #默认False

#0~100,每隔10取刻度值

ax.set_xticks(range(0, points_dot, 10))

ax.set_yticks(range(-2, 3, 1))

#显示网格

ax.grid(True)

#显示图例,固定位置=中心上面

ax.legend(loc='upper center', ncol=4)

plt.show()

'''

报错:

RuntimeError: wrapped C/C++ object of type QTimer has been deleted

提示新版的matplotlib已经删除timer了

'''

图4

1ec094bb91f0dbd79ff67bc62c5d449d.gif

希望喜欢,收藏之后好好复习,生动的图像,加深对python的基础知识的理解,熟悉matplotlib作图,以后拿来就用,通俗易懂。感谢作者分享-http://bjbsair.com/2020-04-07/tech-info/30776.html

1.说明:

1.1 推荐指数:★★★

1.2 python的基础知识复习,通过生动的sin函数制作来复习return和yield,列表、函数定义等知识。

1.3 熟悉matplotlib作图相关知识。

1.4 加深理解sin函数,为以后圆的理解打下坚实基础,cos重复不解释了,将sin适当修改即可。

f5cab809d1d475de28b932ee3d311766.png

2.return法,基本方法,代码:

#---导出模块---

import numpy as np

from matplotlib import pyplot as plt

from matplotlib import animation

#定义画布,默认值,这个fig需要,虽然默认大小设置,fig需要挂在动画上

fig = plt.figure()

#坐标轴刻度

ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#color='blue'=蓝色,否则默认为清淡蓝色

line, = ax.plot([], [], lw=2,color='blue')

# 因为动画,所以初始化列表线条

def init():

line.set_data([], [])

return line, #注意逗号

#定义动画

def animate(i):

#x取值范围从0~2,等差数列,分成1000,越大线条越平滑

x = np.linspace(0, 2, 1000)

#动画x和y的值与i的从0~i的取值有关,才动起来

y = np.sin(2 * np.pi * (x - 0.01 * i))

line.set_data(x, y)

return line, #注意逗号

#将fig挂在动画上面

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

#如果需要保存动画,就这样

#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

#标题名称

plt.title('Sin-a-subplot')

plt.show()

图1

99c99cad98eda9f8f85600e6d779746f.gif

3.np.nan法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

#---定义画布---重点讲到区别和含义---

fig, ax = plt.subplots()

#---函数定义法---讲的很清楚了,很多遍---

#复习一下

#x的坐标取值范围,arange法一般是-2π到2π,这里是从0取,0.01,数值越小曲线越平滑

#注意与linspace取等差数列的区别

x = np.arange(0, 2*np.pi, 0.01)

#这是一步并2步了,相当于y=np.sin(x)

line, = ax.plot(x, np.sin(x))

#---初始化---注意np.nan(NaN)知识复习---

def init():

line.set_ydata([np.nan] * len(x))

#等同于下面

#line.set_ydata([] * len(x))

return line,

'''

有两种丢失数据:

None

np.nan(NaN)

None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。

np.nan(NaN)

np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。

但可以使用np.nan*()函数来计算nan,此时视nan为0。

'''

#---定义动画---

def animate(i):

#line.set_ydata(np.sin(x + i / 100))

#与上面一样效果

line.set_ydata(np.sin(x + 0.01 * i))

return line,

#fig的挂在动画上面

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2, blit=True, save_count=50)

# ani.save("movie.mp4")

plt.show()

图2

e03049dda42efdade55961dfececacb8.gif

4.带红色小圆点的yield法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

#---定义画布和ax轴---

fig, ax = plt.subplots()

'''

等价于:fig, ax = plt.subplots(11)=fig, ax = plt.subplots(1,1)

=fig, ax1 = plt.subplot()

或者:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

'''

#---x和y的函数关系---

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

#画正弦函数线

l = ax.plot(x, y)

#运动的圆球,ro=就是red的o=红色的圆球,如果是o,就是默认颜色的圆球

#挂在正弦函数线上的球,初始化坐标为空

dot, = ax.plot([], [], 'ro')

#---初始化定义红色圆球的ax坐标取值范围---

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

#---产生圆球的坐标取值范围,符合正弦函数---

def gen_dot():

#i类似x坐标,np.sin(i)类似y坐标

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

#通过yield函数产生

yield newdot

'''

首先比较下return 与 yield的区别:

return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。

yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束

带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。

'''

#---更新小圆球的位置---

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='imagemagick', fps=30)

plt.show()

图3

e3488a4b39eac61356f1d74c1a7c4667.gif

5 timer法:最新matplotlib好像淘汰了,可以运行,但是报错,可以不用管它,学习技术而已。代码如下:

#---导出模块---

import matplotlib.pyplot as plt

import numpy as np

#---fig和ax放在一起

fig, ax = plt.subplots()

#---初始化定义---

points_dot = 100

#复习一下列表知识,一个列表里有100个相同的0的列表

sin_list = [0] * points_dot

indx = 0

#---画正弦函数线---初始化---

line_sin, = ax.plot(range(points_dot), sin_list, label='sin-d', color='blue')

#---定义sin输出函数---

def sin_output(ax):

global indx, sin_list, line_sin

if indx == 20:

indx = 0

indx += 1

#更新sin列表,初始化全是100个0,更新后就是正弦函数的y坐标

sin_list = sin_list[1:] + [np.sin((indx / 10) * np.pi)]

#看看ydata就是y坐标的意思

line_sin.set_ydata(sin_list)

#从新画正弦函数动态曲线

ax.draw_artist(line_sin)

ax.figure.canvas.draw()

#计时器在新版的matplotlib中已经删除,目前能显示,但是报错,可以不管,暂时学学技术,了解一下

timer = fig.canvas.new_timer(interval=100)

timer.add_callback(sin_output, ax)

timer.start()

#x和y轴的刻度定义

ax.set_xlim([0, points_dot])

ax.set_ylim([-2, 2])

#ax.set_autoscale_on(False) #默认False

#0~100,每隔10取刻度值

ax.set_xticks(range(0, points_dot, 10))

ax.set_yticks(range(-2, 3, 1))

#显示网格

ax.grid(True)

#显示图例,固定位置=中心上面

ax.legend(loc='upper center', ncol=4)

plt.show()

'''

报错:

RuntimeError: wrapped C/C++ object of type QTimer has been deleted

提示新版的matplotlib已经删除timer了

'''

图4

1ec094bb91f0dbd79ff67bc62c5d449d.gif

希望喜欢,收藏之后好好复习,生动的图像,加深对python的基础知识的理解,熟悉matplotlib作图,以后拿来就用,通俗易懂。感谢作者分享-http://bjbsair.com/2020-04-07/tech-info/30776.html

1.说明:

1.1 推荐指数:★★★

1.2 python的基础知识复习,通过生动的sin函数制作来复习return和yield,列表、函数定义等知识。

1.3 熟悉matplotlib作图相关知识。

1.4 加深理解sin函数,为以后圆的理解打下坚实基础,cos重复不解释了,将sin适当修改即可。

f5cab809d1d475de28b932ee3d311766.png

2.return法,基本方法,代码:

#---导出模块---

import numpy as np

from matplotlib import pyplot as plt

from matplotlib import animation

#定义画布,默认值,这个fig需要,虽然默认大小设置,fig需要挂在动画上

fig = plt.figure()

#坐标轴刻度

ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#color='blue'=蓝色,否则默认为清淡蓝色

line, = ax.plot([], [], lw=2,color='blue')

# 因为动画,所以初始化列表线条

def init():

line.set_data([], [])

return line, #注意逗号

#定义动画

def animate(i):

#x取值范围从0~2,等差数列,分成1000,越大线条越平滑

x = np.linspace(0, 2, 1000)

#动画x和y的值与i的从0~i的取值有关,才动起来

y = np.sin(2 * np.pi * (x - 0.01 * i))

line.set_data(x, y)

return line, #注意逗号

#将fig挂在动画上面

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

#如果需要保存动画,就这样

#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

#标题名称

plt.title('Sin-a-subplot')

plt.show()

图1

99c99cad98eda9f8f85600e6d779746f.gif

3.np.nan法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

#---定义画布---重点讲到区别和含义---

fig, ax = plt.subplots()

#---函数定义法---讲的很清楚了,很多遍---

#复习一下

#x的坐标取值范围,arange法一般是-2π到2π,这里是从0取,0.01,数值越小曲线越平滑

#注意与linspace取等差数列的区别

x = np.arange(0, 2*np.pi, 0.01)

#这是一步并2步了,相当于y=np.sin(x)

line, = ax.plot(x, np.sin(x))

#---初始化---注意np.nan(NaN)知识复习---

def init():

line.set_ydata([np.nan] * len(x))

#等同于下面

#line.set_ydata([] * len(x))

return line,

'''

有两种丢失数据:

None

np.nan(NaN)

None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。

np.nan(NaN)

np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。

但可以使用np.nan*()函数来计算nan,此时视nan为0。

'''

#---定义动画---

def animate(i):

#line.set_ydata(np.sin(x + i / 100))

#与上面一样效果

line.set_ydata(np.sin(x + 0.01 * i))

return line,

#fig的挂在动画上面

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2, blit=True, save_count=50)

# ani.save("movie.mp4")

plt.show()

图2

e03049dda42efdade55961dfececacb8.gif

4.带红色小圆点的yield法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

#---定义画布和ax轴---

fig, ax = plt.subplots()

'''

等价于:fig, ax = plt.subplots(11)=fig, ax = plt.subplots(1,1)

=fig, ax1 = plt.subplot()

或者:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

'''

#---x和y的函数关系---

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

#画正弦函数线

l = ax.plot(x, y)

#运动的圆球,ro=就是red的o=红色的圆球,如果是o,就是默认颜色的圆球

#挂在正弦函数线上的球,初始化坐标为空

dot, = ax.plot([], [], 'ro')

#---初始化定义红色圆球的ax坐标取值范围---

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

#---产生圆球的坐标取值范围,符合正弦函数---

def gen_dot():

#i类似x坐标,np.sin(i)类似y坐标

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

#通过yield函数产生

yield newdot

'''

首先比较下return 与 yield的区别:

return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。

yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束

带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。

'''

#---更新小圆球的位置---

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='imagemagick', fps=30)

plt.show()

图3

e3488a4b39eac61356f1d74c1a7c4667.gif

5 timer法:最新matplotlib好像淘汰了,可以运行,但是报错,可以不用管它,学习技术而已。代码如下:

#---导出模块---

import matplotlib.pyplot as plt

import numpy as np

#---fig和ax放在一起

fig, ax = plt.subplots()

#---初始化定义---

points_dot = 100

#复习一下列表知识,一个列表里有100个相同的0的列表

sin_list = [0] * points_dot

indx = 0

#---画正弦函数线---初始化---

line_sin, = ax.plot(range(points_dot), sin_list, label='sin-d', color='blue')

#---定义sin输出函数---

def sin_output(ax):

global indx, sin_list, line_sin

if indx == 20:

indx = 0

indx += 1

#更新sin列表,初始化全是100个0,更新后就是正弦函数的y坐标

sin_list = sin_list[1:] + [np.sin((indx / 10) * np.pi)]

#看看ydata就是y坐标的意思

line_sin.set_ydata(sin_list)

#从新画正弦函数动态曲线

ax.draw_artist(line_sin)

ax.figure.canvas.draw()

#计时器在新版的matplotlib中已经删除,目前能显示,但是报错,可以不管,暂时学学技术,了解一下

timer = fig.canvas.new_timer(interval=100)

timer.add_callback(sin_output, ax)

timer.start()

#x和y轴的刻度定义

ax.set_xlim([0, points_dot])

ax.set_ylim([-2, 2])

#ax.set_autoscale_on(False) #默认False

#0~100,每隔10取刻度值

ax.set_xticks(range(0, points_dot, 10))

ax.set_yticks(range(-2, 3, 1))

#显示网格

ax.grid(True)

#显示图例,固定位置=中心上面

ax.legend(loc='upper center', ncol=4)

plt.show()

'''

报错:

RuntimeError: wrapped C/C++ object of type QTimer has been deleted

提示新版的matplotlib已经删除timer了

'''

图4

1ec094bb91f0dbd79ff67bc62c5d449d.gif

希望喜欢,收藏之后好好复习,生动的图像,加深对python的基础知识的理解,熟悉matplotlib作图,以后拿来就用,通俗易懂。感谢作者分享-http://bjbsair.com/2020-04-07/tech-info/30776.html

1.说明:

1.1 推荐指数:★★★

1.2 python的基础知识复习,通过生动的sin函数制作来复习return和yield,列表、函数定义等知识。

1.3 熟悉matplotlib作图相关知识。

1.4 加深理解sin函数,为以后圆的理解打下坚实基础,cos重复不解释了,将sin适当修改即可。

f5cab809d1d475de28b932ee3d311766.png

2.return法,基本方法,代码:

#---导出模块---

import numpy as np

from matplotlib import pyplot as plt

from matplotlib import animation

#定义画布,默认值,这个fig需要,虽然默认大小设置,fig需要挂在动画上

fig = plt.figure()

#坐标轴刻度

ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#color='blue'=蓝色,否则默认为清淡蓝色

line, = ax.plot([], [], lw=2,color='blue')

# 因为动画,所以初始化列表线条

def init():

line.set_data([], [])

return line, #注意逗号

#定义动画

def animate(i):

#x取值范围从0~2,等差数列,分成1000,越大线条越平滑

x = np.linspace(0, 2, 1000)

#动画x和y的值与i的从0~i的取值有关,才动起来

y = np.sin(2 * np.pi * (x - 0.01 * i))

line.set_data(x, y)

return line, #注意逗号

#将fig挂在动画上面

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

#如果需要保存动画,就这样

#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

#标题名称

plt.title('Sin-a-subplot')

plt.show()

图1

99c99cad98eda9f8f85600e6d779746f.gif

3.np.nan法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

#---定义画布---重点讲到区别和含义---

fig, ax = plt.subplots()

#---函数定义法---讲的很清楚了,很多遍---

#复习一下

#x的坐标取值范围,arange法一般是-2π到2π,这里是从0取,0.01,数值越小曲线越平滑

#注意与linspace取等差数列的区别

x = np.arange(0, 2*np.pi, 0.01)

#这是一步并2步了,相当于y=np.sin(x)

line, = ax.plot(x, np.sin(x))

#---初始化---注意np.nan(NaN)知识复习---

def init():

line.set_ydata([np.nan] * len(x))

#等同于下面

#line.set_ydata([] * len(x))

return line,

'''

有两种丢失数据:

None

np.nan(NaN)

None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。

np.nan(NaN)

np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。

但可以使用np.nan*()函数来计算nan,此时视nan为0。

'''

#---定义动画---

def animate(i):

#line.set_ydata(np.sin(x + i / 100))

#与上面一样效果

line.set_ydata(np.sin(x + 0.01 * i))

return line,

#fig的挂在动画上面

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2, blit=True, save_count=50)

# ani.save("movie.mp4")

plt.show()

图2

e03049dda42efdade55961dfececacb8.gif

4.带红色小圆点的yield法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

#---定义画布和ax轴---

fig, ax = plt.subplots()

'''

等价于:fig, ax = plt.subplots(11)=fig, ax = plt.subplots(1,1)

=fig, ax1 = plt.subplot()

或者:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

'''

#---x和y的函数关系---

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

#画正弦函数线

l = ax.plot(x, y)

#运动的圆球,ro=就是red的o=红色的圆球,如果是o,就是默认颜色的圆球

#挂在正弦函数线上的球,初始化坐标为空

dot, = ax.plot([], [], 'ro')

#---初始化定义红色圆球的ax坐标取值范围---

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

#---产生圆球的坐标取值范围,符合正弦函数---

def gen_dot():

#i类似x坐标,np.sin(i)类似y坐标

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

#通过yield函数产生

yield newdot

'''

首先比较下return 与 yield的区别:

return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。

yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束

带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。

'''

#---更新小圆球的位置---

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='imagemagick', fps=30)

plt.show()

图3

e3488a4b39eac61356f1d74c1a7c4667.gif

5 timer法:最新matplotlib好像淘汰了,可以运行,但是报错,可以不用管它,学习技术而已。代码如下:

#---导出模块---

import matplotlib.pyplot as plt

import numpy as np

#---fig和ax放在一起

fig, ax = plt.subplots()

#---初始化定义---

points_dot = 100

#复习一下列表知识,一个列表里有100个相同的0的列表

sin_list = [0] * points_dot

indx = 0

#---画正弦函数线---初始化---

line_sin, = ax.plot(range(points_dot), sin_list, label='sin-d', color='blue')

#---定义sin输出函数---

def sin_output(ax):

global indx, sin_list, line_sin

if indx == 20:

indx = 0

indx += 1

#更新sin列表,初始化全是100个0,更新后就是正弦函数的y坐标

sin_list = sin_list[1:] + [np.sin((indx / 10) * np.pi)]

#看看ydata就是y坐标的意思

line_sin.set_ydata(sin_list)

#从新画正弦函数动态曲线

ax.draw_artist(line_sin)

ax.figure.canvas.draw()

#计时器在新版的matplotlib中已经删除,目前能显示,但是报错,可以不管,暂时学学技术,了解一下

timer = fig.canvas.new_timer(interval=100)

timer.add_callback(sin_output, ax)

timer.start()

#x和y轴的刻度定义

ax.set_xlim([0, points_dot])

ax.set_ylim([-2, 2])

#ax.set_autoscale_on(False) #默认False

#0~100,每隔10取刻度值

ax.set_xticks(range(0, points_dot, 10))

ax.set_yticks(range(-2, 3, 1))

#显示网格

ax.grid(True)

#显示图例,固定位置=中心上面

ax.legend(loc='upper center', ncol=4)

plt.show()

'''

报错:

RuntimeError: wrapped C/C++ object of type QTimer has been deleted

提示新版的matplotlib已经删除timer了

'''

图4

1ec094bb91f0dbd79ff67bc62c5d449d.gif

希望喜欢,收藏之后好好复习,生动的图像,加深对python的基础知识的理解,熟悉matplotlib作图,以后拿来就用,通俗易懂。感谢作者分享-http://bjbsair.com/2020-04-07/tech-info/30776.html

1.说明:

1.1 推荐指数:★★★

1.2 python的基础知识复习,通过生动的sin函数制作来复习return和yield,列表、函数定义等知识。

1.3 熟悉matplotlib作图相关知识。

1.4 加深理解sin函数,为以后圆的理解打下坚实基础,cos重复不解释了,将sin适当修改即可。

f5cab809d1d475de28b932ee3d311766.png

2.return法,基本方法,代码:

#---导出模块---

import numpy as np

from matplotlib import pyplot as plt

from matplotlib import animation

#定义画布,默认值,这个fig需要,虽然默认大小设置,fig需要挂在动画上

fig = plt.figure()

#坐标轴刻度

ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#color='blue'=蓝色,否则默认为清淡蓝色

line, = ax.plot([], [], lw=2,color='blue')

# 因为动画,所以初始化列表线条

def init():

line.set_data([], [])

return line, #注意逗号

#定义动画

def animate(i):

#x取值范围从0~2,等差数列,分成1000,越大线条越平滑

x = np.linspace(0, 2, 1000)

#动画x和y的值与i的从0~i的取值有关,才动起来

y = np.sin(2 * np.pi * (x - 0.01 * i))

line.set_data(x, y)

return line, #注意逗号

#将fig挂在动画上面

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

#如果需要保存动画,就这样

#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

#标题名称

plt.title('Sin-a-subplot')

plt.show()

图1

99c99cad98eda9f8f85600e6d779746f.gif

3.np.nan法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

#---定义画布---重点讲到区别和含义---

fig, ax = plt.subplots()

#---函数定义法---讲的很清楚了,很多遍---

#复习一下

#x的坐标取值范围,arange法一般是-2π到2π,这里是从0取,0.01,数值越小曲线越平滑

#注意与linspace取等差数列的区别

x = np.arange(0, 2*np.pi, 0.01)

#这是一步并2步了,相当于y=np.sin(x)

line, = ax.plot(x, np.sin(x))

#---初始化---注意np.nan(NaN)知识复习---

def init():

line.set_ydata([np.nan] * len(x))

#等同于下面

#line.set_ydata([] * len(x))

return line,

'''

有两种丢失数据:

None

np.nan(NaN)

None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。

np.nan(NaN)

np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。

但可以使用np.nan*()函数来计算nan,此时视nan为0。

'''

#---定义动画---

def animate(i):

#line.set_ydata(np.sin(x + i / 100))

#与上面一样效果

line.set_ydata(np.sin(x + 0.01 * i))

return line,

#fig的挂在动画上面

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2, blit=True, save_count=50)

# ani.save("movie.mp4")

plt.show()

图2

e03049dda42efdade55961dfececacb8.gif

4.带红色小圆点的yield法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

#---定义画布和ax轴---

fig, ax = plt.subplots()

'''

等价于:fig, ax = plt.subplots(11)=fig, ax = plt.subplots(1,1)

=fig, ax1 = plt.subplot()

或者:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

'''

#---x和y的函数关系---

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

#画正弦函数线

l = ax.plot(x, y)

#运动的圆球,ro=就是red的o=红色的圆球,如果是o,就是默认颜色的圆球

#挂在正弦函数线上的球,初始化坐标为空

dot, = ax.plot([], [], 'ro')

#---初始化定义红色圆球的ax坐标取值范围---

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

#---产生圆球的坐标取值范围,符合正弦函数---

def gen_dot():

#i类似x坐标,np.sin(i)类似y坐标

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

#通过yield函数产生

yield newdot

'''

首先比较下return 与 yield的区别:

return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。

yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束

带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。

'''

#---更新小圆球的位置---

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='imagemagick', fps=30)

plt.show()

图3

e3488a4b39eac61356f1d74c1a7c4667.gif

5 timer法:最新matplotlib好像淘汰了,可以运行,但是报错,可以不用管它,学习技术而已。代码如下:

#---导出模块---

import matplotlib.pyplot as plt

import numpy as np

#---fig和ax放在一起

fig, ax = plt.subplots()

#---初始化定义---

points_dot = 100

#复习一下列表知识,一个列表里有100个相同的0的列表

sin_list = [0] * points_dot

indx = 0

#---画正弦函数线---初始化---

line_sin, = ax.plot(range(points_dot), sin_list, label='sin-d', color='blue')

#---定义sin输出函数---

def sin_output(ax):

global indx, sin_list, line_sin

if indx == 20:

indx = 0

indx += 1

#更新sin列表,初始化全是100个0,更新后就是正弦函数的y坐标

sin_list = sin_list[1:] + [np.sin((indx / 10) * np.pi)]

#看看ydata就是y坐标的意思

line_sin.set_ydata(sin_list)

#从新画正弦函数动态曲线

ax.draw_artist(line_sin)

ax.figure.canvas.draw()

#计时器在新版的matplotlib中已经删除,目前能显示,但是报错,可以不管,暂时学学技术,了解一下

timer = fig.canvas.new_timer(interval=100)

timer.add_callback(sin_output, ax)

timer.start()

#x和y轴的刻度定义

ax.set_xlim([0, points_dot])

ax.set_ylim([-2, 2])

#ax.set_autoscale_on(False) #默认False

#0~100,每隔10取刻度值

ax.set_xticks(range(0, points_dot, 10))

ax.set_yticks(range(-2, 3, 1))

#显示网格

ax.grid(True)

#显示图例,固定位置=中心上面

ax.legend(loc='upper center', ncol=4)

plt.show()

'''

报错:

RuntimeError: wrapped C/C++ object of type QTimer has been deleted

提示新版的matplotlib已经删除timer了

'''

图4

1ec094bb91f0dbd79ff67bc62c5d449d.gif

希望喜欢,收藏之后好好复习,生动的图像,加深对python的基础知识的理解,熟悉matplotlib作图,以后拿来就用,通俗易懂。感谢作者分享-http://bjbsair.com/2020-04-07/tech-info/30776.html

1.说明:

1.1 推荐指数:★★★

1.2 python的基础知识复习,通过生动的sin函数制作来复习return和yield,列表、函数定义等知识。

1.3 熟悉matplotlib作图相关知识。

1.4 加深理解sin函数,为以后圆的理解打下坚实基础,cos重复不解释了,将sin适当修改即可。

f5cab809d1d475de28b932ee3d311766.png

2.return法,基本方法,代码:

#---导出模块---

import numpy as np

from matplotlib import pyplot as plt

from matplotlib import animation

#定义画布,默认值,这个fig需要,虽然默认大小设置,fig需要挂在动画上

fig = plt.figure()

#坐标轴刻度

ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#color='blue'=蓝色,否则默认为清淡蓝色

line, = ax.plot([], [], lw=2,color='blue')

# 因为动画,所以初始化列表线条

def init():

line.set_data([], [])

return line, #注意逗号

#定义动画

def animate(i):

#x取值范围从0~2,等差数列,分成1000,越大线条越平滑

x = np.linspace(0, 2, 1000)

#动画x和y的值与i的从0~i的取值有关,才动起来

y = np.sin(2 * np.pi * (x - 0.01 * i))

line.set_data(x, y)

return line, #注意逗号

#将fig挂在动画上面

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

#如果需要保存动画,就这样

#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

#标题名称

plt.title('Sin-a-subplot')

plt.show()

图1

99c99cad98eda9f8f85600e6d779746f.gif

3.np.nan法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

#---定义画布---重点讲到区别和含义---

fig, ax = plt.subplots()

#---函数定义法---讲的很清楚了,很多遍---

#复习一下

#x的坐标取值范围,arange法一般是-2π到2π,这里是从0取,0.01,数值越小曲线越平滑

#注意与linspace取等差数列的区别

x = np.arange(0, 2*np.pi, 0.01)

#这是一步并2步了,相当于y=np.sin(x)

line, = ax.plot(x, np.sin(x))

#---初始化---注意np.nan(NaN)知识复习---

def init():

line.set_ydata([np.nan] * len(x))

#等同于下面

#line.set_ydata([] * len(x))

return line,

'''

有两种丢失数据:

None

np.nan(NaN)

None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。

np.nan(NaN)

np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。

但可以使用np.nan*()函数来计算nan,此时视nan为0。

'''

#---定义动画---

def animate(i):

#line.set_ydata(np.sin(x + i / 100))

#与上面一样效果

line.set_ydata(np.sin(x + 0.01 * i))

return line,

#fig的挂在动画上面

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2, blit=True, save_count=50)

# ani.save("movie.mp4")

plt.show()

图2

e03049dda42efdade55961dfececacb8.gif

4.带红色小圆点的yield法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

#---定义画布和ax轴---

fig, ax = plt.subplots()

'''

等价于:fig, ax = plt.subplots(11)=fig, ax = plt.subplots(1,1)

=fig, ax1 = plt.subplot()

或者:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

'''

#---x和y的函数关系---

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

#画正弦函数线

l = ax.plot(x, y)

#运动的圆球,ro=就是red的o=红色的圆球,如果是o,就是默认颜色的圆球

#挂在正弦函数线上的球,初始化坐标为空

dot, = ax.plot([], [], 'ro')

#---初始化定义红色圆球的ax坐标取值范围---

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

#---产生圆球的坐标取值范围,符合正弦函数---

def gen_dot():

#i类似x坐标,np.sin(i)类似y坐标

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

#通过yield函数产生

yield newdot

'''

首先比较下return 与 yield的区别:

return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。

yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束

带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。

'''

#---更新小圆球的位置---

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='imagemagick', fps=30)

plt.show()

图3

e3488a4b39eac61356f1d74c1a7c4667.gif

5 timer法:最新matplotlib好像淘汰了,可以运行,但是报错,可以不用管它,学习技术而已。代码如下:

#---导出模块---

import matplotlib.pyplot as plt

import numpy as np

#---fig和ax放在一起

fig, ax = plt.subplots()

#---初始化定义---

points_dot = 100

#复习一下列表知识,一个列表里有100个相同的0的列表

sin_list = [0] * points_dot

indx = 0

#---画正弦函数线---初始化---

line_sin, = ax.plot(range(points_dot), sin_list, label='sin-d', color='blue')

#---定义sin输出函数---

def sin_output(ax):

global indx, sin_list, line_sin

if indx == 20:

indx = 0

indx += 1

#更新sin列表,初始化全是100个0,更新后就是正弦函数的y坐标

sin_list = sin_list[1:] + [np.sin((indx / 10) * np.pi)]

#看看ydata就是y坐标的意思

line_sin.set_ydata(sin_list)

#从新画正弦函数动态曲线

ax.draw_artist(line_sin)

ax.figure.canvas.draw()

#计时器在新版的matplotlib中已经删除,目前能显示,但是报错,可以不管,暂时学学技术,了解一下

timer = fig.canvas.new_timer(interval=100)

timer.add_callback(sin_output, ax)

timer.start()

#x和y轴的刻度定义

ax.set_xlim([0, points_dot])

ax.set_ylim([-2, 2])

#ax.set_autoscale_on(False) #默认False

#0~100,每隔10取刻度值

ax.set_xticks(range(0, points_dot, 10))

ax.set_yticks(range(-2, 3, 1))

#显示网格

ax.grid(True)

#显示图例,固定位置=中心上面

ax.legend(loc='upper center', ncol=4)

plt.show()

'''

报错:

RuntimeError: wrapped C/C++ object of type QTimer has been deleted

提示新版的matplotlib已经删除timer了

'''

图4

1ec094bb91f0dbd79ff67bc62c5d449d.gif

希望喜欢,收藏之后好好复习,生动的图像,加深对python的基础知识的理解,熟悉matplotlib作图,以后拿来就用,通俗易懂。感谢作者分享-http://bjbsair.com/2020-04-07/tech-info/30776.html

1.说明:

1.1 推荐指数:★★★

1.2 python的基础知识复习,通过生动的sin函数制作来复习return和yield,列表、函数定义等知识。

1.3 熟悉matplotlib作图相关知识。

1.4 加深理解sin函数,为以后圆的理解打下坚实基础,cos重复不解释了,将sin适当修改即可。

f5cab809d1d475de28b932ee3d311766.png

2.return法,基本方法,代码:

#---导出模块---

import numpy as np

from matplotlib import pyplot as plt

from matplotlib import animation

#定义画布,默认值,这个fig需要,虽然默认大小设置,fig需要挂在动画上

fig = plt.figure()

#坐标轴刻度

ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#color='blue'=蓝色,否则默认为清淡蓝色

line, = ax.plot([], [], lw=2,color='blue')

# 因为动画,所以初始化列表线条

def init():

line.set_data([], [])

return line, #注意逗号

#定义动画

def animate(i):

#x取值范围从0~2,等差数列,分成1000,越大线条越平滑

x = np.linspace(0, 2, 1000)

#动画x和y的值与i的从0~i的取值有关,才动起来

y = np.sin(2 * np.pi * (x - 0.01 * i))

line.set_data(x, y)

return line, #注意逗号

#将fig挂在动画上面

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

#如果需要保存动画,就这样

#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

#标题名称

plt.title('Sin-a-subplot')

plt.show()

图1

99c99cad98eda9f8f85600e6d779746f.gif

3.np.nan法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

#---定义画布---重点讲到区别和含义---

fig, ax = plt.subplots()

#---函数定义法---讲的很清楚了,很多遍---

#复习一下

#x的坐标取值范围,arange法一般是-2π到2π,这里是从0取,0.01,数值越小曲线越平滑

#注意与linspace取等差数列的区别

x = np.arange(0, 2*np.pi, 0.01)

#这是一步并2步了,相当于y=np.sin(x)

line, = ax.plot(x, np.sin(x))

#---初始化---注意np.nan(NaN)知识复习---

def init():

line.set_ydata([np.nan] * len(x))

#等同于下面

#line.set_ydata([] * len(x))

return line,

'''

有两种丢失数据:

None

np.nan(NaN)

None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。

np.nan(NaN)

np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。

但可以使用np.nan*()函数来计算nan,此时视nan为0。

'''

#---定义动画---

def animate(i):

#line.set_ydata(np.sin(x + i / 100))

#与上面一样效果

line.set_ydata(np.sin(x + 0.01 * i))

return line,

#fig的挂在动画上面

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2, blit=True, save_count=50)

# ani.save("movie.mp4")

plt.show()

图2

e03049dda42efdade55961dfececacb8.gif

4.带红色小圆点的yield法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

#---定义画布和ax轴---

fig, ax = plt.subplots()

'''

等价于:fig, ax = plt.subplots(11)=fig, ax = plt.subplots(1,1)

=fig, ax1 = plt.subplot()

或者:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

'''

#---x和y的函数关系---

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

#画正弦函数线

l = ax.plot(x, y)

#运动的圆球,ro=就是red的o=红色的圆球,如果是o,就是默认颜色的圆球

#挂在正弦函数线上的球,初始化坐标为空

dot, = ax.plot([], [], 'ro')

#---初始化定义红色圆球的ax坐标取值范围---

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

#---产生圆球的坐标取值范围,符合正弦函数---

def gen_dot():

#i类似x坐标,np.sin(i)类似y坐标

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

#通过yield函数产生

yield newdot

'''

首先比较下return 与 yield的区别:

return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。

yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束

带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。

'''

#---更新小圆球的位置---

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='imagemagick', fps=30)

plt.show()

图3

e3488a4b39eac61356f1d74c1a7c4667.gif

5 timer法:最新matplotlib好像淘汰了,可以运行,但是报错,可以不用管它,学习技术而已。代码如下:

#---导出模块---

import matplotlib.pyplot as plt

import numpy as np

#---fig和ax放在一起

fig, ax = plt.subplots()

#---初始化定义---

points_dot = 100

#复习一下列表知识,一个列表里有100个相同的0的列表

sin_list = [0] * points_dot

indx = 0

#---画正弦函数线---初始化---

line_sin, = ax.plot(range(points_dot), sin_list, label='sin-d', color='blue')

#---定义sin输出函数---

def sin_output(ax):

global indx, sin_list, line_sin

if indx == 20:

indx = 0

indx += 1

#更新sin列表,初始化全是100个0,更新后就是正弦函数的y坐标

sin_list = sin_list[1:] + [np.sin((indx / 10) * np.pi)]

#看看ydata就是y坐标的意思

line_sin.set_ydata(sin_list)

#从新画正弦函数动态曲线

ax.draw_artist(line_sin)

ax.figure.canvas.draw()

#计时器在新版的matplotlib中已经删除,目前能显示,但是报错,可以不管,暂时学学技术,了解一下

timer = fig.canvas.new_timer(interval=100)

timer.add_callback(sin_output, ax)

timer.start()

#x和y轴的刻度定义

ax.set_xlim([0, points_dot])

ax.set_ylim([-2, 2])

#ax.set_autoscale_on(False) #默认False

#0~100,每隔10取刻度值

ax.set_xticks(range(0, points_dot, 10))

ax.set_yticks(range(-2, 3, 1))

#显示网格

ax.grid(True)

#显示图例,固定位置=中心上面

ax.legend(loc='upper center', ncol=4)

plt.show()

'''

报错:

RuntimeError: wrapped C/C++ object of type QTimer has been deleted

提示新版的matplotlib已经删除timer了

'''

图4

1ec094bb91f0dbd79ff67bc62c5d449d.gif

希望喜欢,收藏之后好好复习,生动的图像,加深对python的基础知识的理解,熟悉matplotlib作图,以后拿来就用,通俗易懂。感谢作者分享-http://bjbsair.com/2020-04-07/tech-info/30776.html

1.说明:

1.1 推荐指数:★★★

1.2 python的基础知识复习,通过生动的sin函数制作来复习return和yield,列表、函数定义等知识。

1.3 熟悉matplotlib作图相关知识。

1.4 加深理解sin函数,为以后圆的理解打下坚实基础,cos重复不解释了,将sin适当修改即可。

f5cab809d1d475de28b932ee3d311766.png

2.return法,基本方法,代码:

#---导出模块---

import numpy as np

from matplotlib import pyplot as plt

from matplotlib import animation

#定义画布,默认值,这个fig需要,虽然默认大小设置,fig需要挂在动画上

fig = plt.figure()

#坐标轴刻度

ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#color='blue'=蓝色,否则默认为清淡蓝色

line, = ax.plot([], [], lw=2,color='blue')

# 因为动画,所以初始化列表线条

def init():

line.set_data([], [])

return line, #注意逗号

#定义动画

def animate(i):

#x取值范围从0~2,等差数列,分成1000,越大线条越平滑

x = np.linspace(0, 2, 1000)

#动画x和y的值与i的从0~i的取值有关,才动起来

y = np.sin(2 * np.pi * (x - 0.01 * i))

line.set_data(x, y)

return line, #注意逗号

#将fig挂在动画上面

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

#如果需要保存动画,就这样

#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

#标题名称

plt.title('Sin-a-subplot')

plt.show()

图1

99c99cad98eda9f8f85600e6d779746f.gif

3.np.nan法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

#---定义画布---重点讲到区别和含义---

fig, ax = plt.subplots()

#---函数定义法---讲的很清楚了,很多遍---

#复习一下

#x的坐标取值范围,arange法一般是-2π到2π,这里是从0取,0.01,数值越小曲线越平滑

#注意与linspace取等差数列的区别

x = np.arange(0, 2*np.pi, 0.01)

#这是一步并2步了,相当于y=np.sin(x)

line, = ax.plot(x, np.sin(x))

#---初始化---注意np.nan(NaN)知识复习---

def init():

line.set_ydata([np.nan] * len(x))

#等同于下面

#line.set_ydata([] * len(x))

return line,

'''

有两种丢失数据:

None

np.nan(NaN)

None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。

np.nan(NaN)

np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。

但可以使用np.nan*()函数来计算nan,此时视nan为0。

'''

#---定义动画---

def animate(i):

#line.set_ydata(np.sin(x + i / 100))

#与上面一样效果

line.set_ydata(np.sin(x + 0.01 * i))

return line,

#fig的挂在动画上面

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2, blit=True, save_count=50)

# ani.save("movie.mp4")

plt.show()

图2

e03049dda42efdade55961dfececacb8.gif

4.带红色小圆点的yield法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

#---定义画布和ax轴---

fig, ax = plt.subplots()

'''

等价于:fig, ax = plt.subplots(11)=fig, ax = plt.subplots(1,1)

=fig, ax1 = plt.subplot()

或者:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

'''

#---x和y的函数关系---

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

#画正弦函数线

l = ax.plot(x, y)

#运动的圆球,ro=就是red的o=红色的圆球,如果是o,就是默认颜色的圆球

#挂在正弦函数线上的球,初始化坐标为空

dot, = ax.plot([], [], 'ro')

#---初始化定义红色圆球的ax坐标取值范围---

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

#---产生圆球的坐标取值范围,符合正弦函数---

def gen_dot():

#i类似x坐标,np.sin(i)类似y坐标

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

#通过yield函数产生

yield newdot

'''

首先比较下return 与 yield的区别:

return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。

yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束

带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。

'''

#---更新小圆球的位置---

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='imagemagick', fps=30)

plt.show()

图3

e3488a4b39eac61356f1d74c1a7c4667.gif

5 timer法:最新matplotlib好像淘汰了,可以运行,但是报错,可以不用管它,学习技术而已。代码如下:

#---导出模块---

import matplotlib.pyplot as plt

import numpy as np

#---fig和ax放在一起

fig, ax = plt.subplots()

#---初始化定义---

points_dot = 100

#复习一下列表知识,一个列表里有100个相同的0的列表

sin_list = [0] * points_dot

indx = 0

#---画正弦函数线---初始化---

line_sin, = ax.plot(range(points_dot), sin_list, label='sin-d', color='blue')

#---定义sin输出函数---

def sin_output(ax):

global indx, sin_list, line_sin

if indx == 20:

indx = 0

indx += 1

#更新sin列表,初始化全是100个0,更新后就是正弦函数的y坐标

sin_list = sin_list[1:] + [np.sin((indx / 10) * np.pi)]

#看看ydata就是y坐标的意思

line_sin.set_ydata(sin_list)

#从新画正弦函数动态曲线

ax.draw_artist(line_sin)

ax.figure.canvas.draw()

#计时器在新版的matplotlib中已经删除,目前能显示,但是报错,可以不管,暂时学学技术,了解一下

timer = fig.canvas.new_timer(interval=100)

timer.add_callback(sin_output, ax)

timer.start()

#x和y轴的刻度定义

ax.set_xlim([0, points_dot])

ax.set_ylim([-2, 2])

#ax.set_autoscale_on(False) #默认False

#0~100,每隔10取刻度值

ax.set_xticks(range(0, points_dot, 10))

ax.set_yticks(range(-2, 3, 1))

#显示网格

ax.grid(True)

#显示图例,固定位置=中心上面

ax.legend(loc='upper center', ncol=4)

plt.show()

'''

报错:

RuntimeError: wrapped C/C++ object of type QTimer has been deleted

提示新版的matplotlib已经删除timer了

'''

图4

1ec094bb91f0dbd79ff67bc62c5d449d.gif

希望喜欢,收藏之后好好复习,生动的图像,加深对python的基础知识的理解,熟悉matplotlib作图,以后拿来就用,通俗易懂。感谢作者分享-http://bjbsair.com/2020-04-07/tech-info/30776.html

1.说明:

1.1 推荐指数:★★★

1.2 python的基础知识复习,通过生动的sin函数制作来复习return和yield,列表、函数定义等知识。

1.3 熟悉matplotlib作图相关知识。

1.4 加深理解sin函数,为以后圆的理解打下坚实基础,cos重复不解释了,将sin适当修改即可。

f5cab809d1d475de28b932ee3d311766.png

2.return法,基本方法,代码:

#---导出模块---

import numpy as np

from matplotlib import pyplot as plt

from matplotlib import animation

#定义画布,默认值,这个fig需要,虽然默认大小设置,fig需要挂在动画上

fig = plt.figure()

#坐标轴刻度

ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#color='blue'=蓝色,否则默认为清淡蓝色

line, = ax.plot([], [], lw=2,color='blue')

# 因为动画,所以初始化列表线条

def init():

line.set_data([], [])

return line, #注意逗号

#定义动画

def animate(i):

#x取值范围从0~2,等差数列,分成1000,越大线条越平滑

x = np.linspace(0, 2, 1000)

#动画x和y的值与i的从0~i的取值有关,才动起来

y = np.sin(2 * np.pi * (x - 0.01 * i))

line.set_data(x, y)

return line, #注意逗号

#将fig挂在动画上面

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

#如果需要保存动画,就这样

#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

#标题名称

plt.title('Sin-a-subplot')

plt.show()

图1

99c99cad98eda9f8f85600e6d779746f.gif

3.np.nan法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

#---定义画布---重点讲到区别和含义---

fig, ax = plt.subplots()

#---函数定义法---讲的很清楚了,很多遍---

#复习一下

#x的坐标取值范围,arange法一般是-2π到2π,这里是从0取,0.01,数值越小曲线越平滑

#注意与linspace取等差数列的区别

x = np.arange(0, 2*np.pi, 0.01)

#这是一步并2步了,相当于y=np.sin(x)

line, = ax.plot(x, np.sin(x))

#---初始化---注意np.nan(NaN)知识复习---

def init():

line.set_ydata([np.nan] * len(x))

#等同于下面

#line.set_ydata([] * len(x))

return line,

'''

有两种丢失数据:

None

np.nan(NaN)

None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。

np.nan(NaN)

np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。

但可以使用np.nan*()函数来计算nan,此时视nan为0。

'''

#---定义动画---

def animate(i):

#line.set_ydata(np.sin(x + i / 100))

#与上面一样效果

line.set_ydata(np.sin(x + 0.01 * i))

return line,

#fig的挂在动画上面

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2, blit=True, save_count=50)

# ani.save("movie.mp4")

plt.show()

图2

e03049dda42efdade55961dfececacb8.gif

4.带红色小圆点的yield法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

#---定义画布和ax轴---

fig, ax = plt.subplots()

'''

等价于:fig, ax = plt.subplots(11)=fig, ax = plt.subplots(1,1)

=fig, ax1 = plt.subplot()

或者:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

'''

#---x和y的函数关系---

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

#画正弦函数线

l = ax.plot(x, y)

#运动的圆球,ro=就是red的o=红色的圆球,如果是o,就是默认颜色的圆球

#挂在正弦函数线上的球,初始化坐标为空

dot, = ax.plot([], [], 'ro')

#---初始化定义红色圆球的ax坐标取值范围---

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

#---产生圆球的坐标取值范围,符合正弦函数---

def gen_dot():

#i类似x坐标,np.sin(i)类似y坐标

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

#通过yield函数产生

yield newdot

'''

首先比较下return 与 yield的区别:

return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。

yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束

带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。

'''

#---更新小圆球的位置---

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='imagemagick', fps=30)

plt.show()

图3

e3488a4b39eac61356f1d74c1a7c4667.gif

5 timer法:最新matplotlib好像淘汰了,可以运行,但是报错,可以不用管它,学习技术而已。代码如下:

#---导出模块---

import matplotlib.pyplot as plt

import numpy as np

#---fig和ax放在一起

fig, ax = plt.subplots()

#---初始化定义---

points_dot = 100

#复习一下列表知识,一个列表里有100个相同的0的列表

sin_list = [0] * points_dot

indx = 0

#---画正弦函数线---初始化---

line_sin, = ax.plot(range(points_dot), sin_list, label='sin-d', color='blue')

#---定义sin输出函数---

def sin_output(ax):

global indx, sin_list, line_sin

if indx == 20:

indx = 0

indx += 1

#更新sin列表,初始化全是100个0,更新后就是正弦函数的y坐标

sin_list = sin_list[1:] + [np.sin((indx / 10) * np.pi)]

#看看ydata就是y坐标的意思

line_sin.set_ydata(sin_list)

#从新画正弦函数动态曲线

ax.draw_artist(line_sin)

ax.figure.canvas.draw()

#计时器在新版的matplotlib中已经删除,目前能显示,但是报错,可以不管,暂时学学技术,了解一下

timer = fig.canvas.new_timer(interval=100)

timer.add_callback(sin_output, ax)

timer.start()

#x和y轴的刻度定义

ax.set_xlim([0, points_dot])

ax.set_ylim([-2, 2])

#ax.set_autoscale_on(False) #默认False

#0~100,每隔10取刻度值

ax.set_xticks(range(0, points_dot, 10))

ax.set_yticks(range(-2, 3, 1))

#显示网格

ax.grid(True)

#显示图例,固定位置=中心上面

ax.legend(loc='upper center', ncol=4)

plt.show()

'''

报错:

RuntimeError: wrapped C/C++ object of type QTimer has been deleted

提示新版的matplotlib已经删除timer了

'''

图4

1ec094bb91f0dbd79ff67bc62c5d449d.gif

希望喜欢,收藏之后好好复习,生动的图像,加深对python的基础知识的理解,熟悉matplotlib作图,以后拿来就用,通俗易懂。感谢作者分享-http://bjbsair.com/2020-04-07/tech-info/30776.html

1.说明:

1.1 推荐指数:★★★

1.2 python的基础知识复习,通过生动的sin函数制作来复习return和yield,列表、函数定义等知识。

1.3 熟悉matplotlib作图相关知识。

1.4 加深理解sin函数,为以后圆的理解打下坚实基础,cos重复不解释了,将sin适当修改即可。

f5cab809d1d475de28b932ee3d311766.png

2.return法,基本方法,代码:

#---导出模块---

import numpy as np

from matplotlib import pyplot as plt

from matplotlib import animation

#定义画布,默认值,这个fig需要,虽然默认大小设置,fig需要挂在动画上

fig = plt.figure()

#坐标轴刻度

ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#color='blue'=蓝色,否则默认为清淡蓝色

line, = ax.plot([], [], lw=2,color='blue')

# 因为动画,所以初始化列表线条

def init():

line.set_data([], [])

return line, #注意逗号

#定义动画

def animate(i):

#x取值范围从0~2,等差数列,分成1000,越大线条越平滑

x = np.linspace(0, 2, 1000)

#动画x和y的值与i的从0~i的取值有关,才动起来

y = np.sin(2 * np.pi * (x - 0.01 * i))

line.set_data(x, y)

return line, #注意逗号

#将fig挂在动画上面

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

#如果需要保存动画,就这样

#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

#标题名称

plt.title('Sin-a-subplot')

plt.show()

图1

99c99cad98eda9f8f85600e6d779746f.gif

3.np.nan法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

#---定义画布---重点讲到区别和含义---

fig, ax = plt.subplots()

#---函数定义法---讲的很清楚了,很多遍---

#复习一下

#x的坐标取值范围,arange法一般是-2π到2π,这里是从0取,0.01,数值越小曲线越平滑

#注意与linspace取等差数列的区别

x = np.arange(0, 2*np.pi, 0.01)

#这是一步并2步了,相当于y=np.sin(x)

line, = ax.plot(x, np.sin(x))

#---初始化---注意np.nan(NaN)知识复习---

def init():

line.set_ydata([np.nan] * len(x))

#等同于下面

#line.set_ydata([] * len(x))

return line,

'''

有两种丢失数据:

None

np.nan(NaN)

None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。

np.nan(NaN)

np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。

但可以使用np.nan*()函数来计算nan,此时视nan为0。

'''

#---定义动画---

def animate(i):

#line.set_ydata(np.sin(x + i / 100))

#与上面一样效果

line.set_ydata(np.sin(x + 0.01 * i))

return line,

#fig的挂在动画上面

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2, blit=True, save_count=50)

# ani.save("movie.mp4")

plt.show()

图2

e03049dda42efdade55961dfececacb8.gif

4.带红色小圆点的yield法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

#---定义画布和ax轴---

fig, ax = plt.subplots()

'''

等价于:fig, ax = plt.subplots(11)=fig, ax = plt.subplots(1,1)

=fig, ax1 = plt.subplot()

或者:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

'''

#---x和y的函数关系---

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

#画正弦函数线

l = ax.plot(x, y)

#运动的圆球,ro=就是red的o=红色的圆球,如果是o,就是默认颜色的圆球

#挂在正弦函数线上的球,初始化坐标为空

dot, = ax.plot([], [], 'ro')

#---初始化定义红色圆球的ax坐标取值范围---

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

#---产生圆球的坐标取值范围,符合正弦函数---

def gen_dot():

#i类似x坐标,np.sin(i)类似y坐标

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

#通过yield函数产生

yield newdot

'''

首先比较下return 与 yield的区别:

return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。

yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束

带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。

'''

#---更新小圆球的位置---

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='imagemagick', fps=30)

plt.show()

图3

e3488a4b39eac61356f1d74c1a7c4667.gif

5 timer法:最新matplotlib好像淘汰了,可以运行,但是报错,可以不用管它,学习技术而已。代码如下:

#---导出模块---

import matplotlib.pyplot as plt

import numpy as np

#---fig和ax放在一起

fig, ax = plt.subplots()

#---初始化定义---

points_dot = 100

#复习一下列表知识,一个列表里有100个相同的0的列表

sin_list = [0] * points_dot

indx = 0

#---画正弦函数线---初始化---

line_sin, = ax.plot(range(points_dot), sin_list, label='sin-d', color='blue')

#---定义sin输出函数---

def sin_output(ax):

global indx, sin_list, line_sin

if indx == 20:

indx = 0

indx += 1

#更新sin列表,初始化全是100个0,更新后就是正弦函数的y坐标

sin_list = sin_list[1:] + [np.sin((indx / 10) * np.pi)]

#看看ydata就是y坐标的意思

line_sin.set_ydata(sin_list)

#从新画正弦函数动态曲线

ax.draw_artist(line_sin)

ax.figure.canvas.draw()

#计时器在新版的matplotlib中已经删除,目前能显示,但是报错,可以不管,暂时学学技术,了解一下

timer = fig.canvas.new_timer(interval=100)

timer.add_callback(sin_output, ax)

timer.start()

#x和y轴的刻度定义

ax.set_xlim([0, points_dot])

ax.set_ylim([-2, 2])

#ax.set_autoscale_on(False) #默认False

#0~100,每隔10取刻度值

ax.set_xticks(range(0, points_dot, 10))

ax.set_yticks(range(-2, 3, 1))

#显示网格

ax.grid(True)

#显示图例,固定位置=中心上面

ax.legend(loc='upper center', ncol=4)

plt.show()

'''

报错:

RuntimeError: wrapped C/C++ object of type QTimer has been deleted

提示新版的matplotlib已经删除timer了

'''

图4

1ec094bb91f0dbd79ff67bc62c5d449d.gif

希望喜欢,收藏之后好好复习,生动的图像,加深对python的基础知识的理解,熟悉matplotlib作图,以后拿来就用,通俗易懂。感谢作者分享-http://bjbsair.com/2020-04-07/tech-info/30776.html

1.说明:

1.1 推荐指数:★★★

1.2 python的基础知识复习,通过生动的sin函数制作来复习return和yield,列表、函数定义等知识。

1.3 熟悉matplotlib作图相关知识。

1.4 加深理解sin函数,为以后圆的理解打下坚实基础,cos重复不解释了,将sin适当修改即可。

f5cab809d1d475de28b932ee3d311766.png

2.return法,基本方法,代码:

#---导出模块---

import numpy as np

from matplotlib import pyplot as plt

from matplotlib import animation

#定义画布,默认值,这个fig需要,虽然默认大小设置,fig需要挂在动画上

fig = plt.figure()

#坐标轴刻度

ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#color='blue'=蓝色,否则默认为清淡蓝色

line, = ax.plot([], [], lw=2,color='blue')

# 因为动画,所以初始化列表线条

def init():

line.set_data([], [])

return line, #注意逗号

#定义动画

def animate(i):

#x取值范围从0~2,等差数列,分成1000,越大线条越平滑

x = np.linspace(0, 2, 1000)

#动画x和y的值与i的从0~i的取值有关,才动起来

y = np.sin(2 * np.pi * (x - 0.01 * i))

line.set_data(x, y)

return line, #注意逗号

#将fig挂在动画上面

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

#如果需要保存动画,就这样

#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

#标题名称

plt.title('Sin-a-subplot')

plt.show()

图1

99c99cad98eda9f8f85600e6d779746f.gif

3.np.nan法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

#---定义画布---重点讲到区别和含义---

fig, ax = plt.subplots()

#---函数定义法---讲的很清楚了,很多遍---

#复习一下

#x的坐标取值范围,arange法一般是-2π到2π,这里是从0取,0.01,数值越小曲线越平滑

#注意与linspace取等差数列的区别

x = np.arange(0, 2*np.pi, 0.01)

#这是一步并2步了,相当于y=np.sin(x)

line, = ax.plot(x, np.sin(x))

#---初始化---注意np.nan(NaN)知识复习---

def init():

line.set_ydata([np.nan] * len(x))

#等同于下面

#line.set_ydata([] * len(x))

return line,

'''

有两种丢失数据:

None

np.nan(NaN)

None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。

np.nan(NaN)

np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。

但可以使用np.nan*()函数来计算nan,此时视nan为0。

'''

#---定义动画---

def animate(i):

#line.set_ydata(np.sin(x + i / 100))

#与上面一样效果

line.set_ydata(np.sin(x + 0.01 * i))

return line,

#fig的挂在动画上面

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2, blit=True, save_count=50)

# ani.save("movie.mp4")

plt.show()

图2

e03049dda42efdade55961dfececacb8.gif

4.带红色小圆点的yield法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

#---定义画布和ax轴---

fig, ax = plt.subplots()

'''

等价于:fig, ax = plt.subplots(11)=fig, ax = plt.subplots(1,1)

=fig, ax1 = plt.subplot()

或者:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

'''

#---x和y的函数关系---

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

#画正弦函数线

l = ax.plot(x, y)

#运动的圆球,ro=就是red的o=红色的圆球,如果是o,就是默认颜色的圆球

#挂在正弦函数线上的球,初始化坐标为空

dot, = ax.plot([], [], 'ro')

#---初始化定义红色圆球的ax坐标取值范围---

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

#---产生圆球的坐标取值范围,符合正弦函数---

def gen_dot():

#i类似x坐标,np.sin(i)类似y坐标

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

#通过yield函数产生

yield newdot

'''

首先比较下return 与 yield的区别:

return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。

yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束

带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。

'''

#---更新小圆球的位置---

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='imagemagick', fps=30)

plt.show()

图3

e3488a4b39eac61356f1d74c1a7c4667.gif

5 timer法:最新matplotlib好像淘汰了,可以运行,但是报错,可以不用管它,学习技术而已。代码如下:

#---导出模块---

import matplotlib.pyplot as plt

import numpy as np

#---fig和ax放在一起

fig, ax = plt.subplots()

#---初始化定义---

points_dot = 100

#复习一下列表知识,一个列表里有100个相同的0的列表

sin_list = [0] * points_dot

indx = 0

#---画正弦函数线---初始化---

line_sin, = ax.plot(range(points_dot), sin_list, label='sin-d', color='blue')

#---定义sin输出函数---

def sin_output(ax):

global indx, sin_list, line_sin

if indx == 20:

indx = 0

indx += 1

#更新sin列表,初始化全是100个0,更新后就是正弦函数的y坐标

sin_list = sin_list[1:] + [np.sin((indx / 10) * np.pi)]

#看看ydata就是y坐标的意思

line_sin.set_ydata(sin_list)

#从新画正弦函数动态曲线

ax.draw_artist(line_sin)

ax.figure.canvas.draw()

#计时器在新版的matplotlib中已经删除,目前能显示,但是报错,可以不管,暂时学学技术,了解一下

timer = fig.canvas.new_timer(interval=100)

timer.add_callback(sin_output, ax)

timer.start()

#x和y轴的刻度定义

ax.set_xlim([0, points_dot])

ax.set_ylim([-2, 2])

#ax.set_autoscale_on(False) #默认False

#0~100,每隔10取刻度值

ax.set_xticks(range(0, points_dot, 10))

ax.set_yticks(range(-2, 3, 1))

#显示网格

ax.grid(True)

#显示图例,固定位置=中心上面

ax.legend(loc='upper center', ncol=4)

plt.show()

'''

报错:

RuntimeError: wrapped C/C++ object of type QTimer has been deleted

提示新版的matplotlib已经删除timer了

'''

图4

1ec094bb91f0dbd79ff67bc62c5d449d.gif

希望喜欢,收藏之后好好复习,生动的图像,加深对python的基础知识的理解,熟悉matplotlib作图,以后拿来就用,通俗易懂。感谢作者分享-http://bjbsair.com/2020-04-07/tech-info/30776.html

1.说明:

1.1 推荐指数:★★★

1.2 python的基础知识复习,通过生动的sin函数制作来复习return和yield,列表、函数定义等知识。

1.3 熟悉matplotlib作图相关知识。

1.4 加深理解sin函数,为以后圆的理解打下坚实基础,cos重复不解释了,将sin适当修改即可。

f5cab809d1d475de28b932ee3d311766.png

2.return法,基本方法,代码:

#---导出模块---

import numpy as np

from matplotlib import pyplot as plt

from matplotlib import animation

#定义画布,默认值,这个fig需要,虽然默认大小设置,fig需要挂在动画上

fig = plt.figure()

#坐标轴刻度

ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#color='blue'=蓝色,否则默认为清淡蓝色

line, = ax.plot([], [], lw=2,color='blue')

# 因为动画,所以初始化列表线条

def init():

line.set_data([], [])

return line, #注意逗号

#定义动画

def animate(i):

#x取值范围从0~2,等差数列,分成1000,越大线条越平滑

x = np.linspace(0, 2, 1000)

#动画x和y的值与i的从0~i的取值有关,才动起来

y = np.sin(2 * np.pi * (x - 0.01 * i))

line.set_data(x, y)

return line, #注意逗号

#将fig挂在动画上面

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

#如果需要保存动画,就这样

#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

#标题名称

plt.title('Sin-a-subplot')

plt.show()

图1

99c99cad98eda9f8f85600e6d779746f.gif

3.np.nan法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

#---定义画布---重点讲到区别和含义---

fig, ax = plt.subplots()

#---函数定义法---讲的很清楚了,很多遍---

#复习一下

#x的坐标取值范围,arange法一般是-2π到2π,这里是从0取,0.01,数值越小曲线越平滑

#注意与linspace取等差数列的区别

x = np.arange(0, 2*np.pi, 0.01)

#这是一步并2步了,相当于y=np.sin(x)

line, = ax.plot(x, np.sin(x))

#---初始化---注意np.nan(NaN)知识复习---

def init():

line.set_ydata([np.nan] * len(x))

#等同于下面

#line.set_ydata([] * len(x))

return line,

'''

有两种丢失数据:

None

np.nan(NaN)

None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。

np.nan(NaN)

np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。

但可以使用np.nan*()函数来计算nan,此时视nan为0。

'''

#---定义动画---

def animate(i):

#line.set_ydata(np.sin(x + i / 100))

#与上面一样效果

line.set_ydata(np.sin(x + 0.01 * i))

return line,

#fig的挂在动画上面

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2, blit=True, save_count=50)

# ani.save("movie.mp4")

plt.show()

图2

e03049dda42efdade55961dfececacb8.gif

4.带红色小圆点的yield法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

#---定义画布和ax轴---

fig, ax = plt.subplots()

'''

等价于:fig, ax = plt.subplots(11)=fig, ax = plt.subplots(1,1)

=fig, ax1 = plt.subplot()

或者:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

'''

#---x和y的函数关系---

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

#画正弦函数线

l = ax.plot(x, y)

#运动的圆球,ro=就是red的o=红色的圆球,如果是o,就是默认颜色的圆球

#挂在正弦函数线上的球,初始化坐标为空

dot, = ax.plot([], [], 'ro')

#---初始化定义红色圆球的ax坐标取值范围---

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

#---产生圆球的坐标取值范围,符合正弦函数---

def gen_dot():

#i类似x坐标,np.sin(i)类似y坐标

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

#通过yield函数产生

yield newdot

'''

首先比较下return 与 yield的区别:

return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。

yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束

带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。

'''

#---更新小圆球的位置---

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='imagemagick', fps=30)

plt.show()

图3

e3488a4b39eac61356f1d74c1a7c4667.gif

5 timer法:最新matplotlib好像淘汰了,可以运行,但是报错,可以不用管它,学习技术而已。代码如下:

#---导出模块---

import matplotlib.pyplot as plt

import numpy as np

#---fig和ax放在一起

fig, ax = plt.subplots()

#---初始化定义---

points_dot = 100

#复习一下列表知识,一个列表里有100个相同的0的列表

sin_list = [0] * points_dot

indx = 0

#---画正弦函数线---初始化---

line_sin, = ax.plot(range(points_dot), sin_list, label='sin-d', color='blue')

#---定义sin输出函数---

def sin_output(ax):

global indx, sin_list, line_sin

if indx == 20:

indx = 0

indx += 1

#更新sin列表,初始化全是100个0,更新后就是正弦函数的y坐标

sin_list = sin_list[1:] + [np.sin((indx / 10) * np.pi)]

#看看ydata就是y坐标的意思

line_sin.set_ydata(sin_list)

#从新画正弦函数动态曲线

ax.draw_artist(line_sin)

ax.figure.canvas.draw()

#计时器在新版的matplotlib中已经删除,目前能显示,但是报错,可以不管,暂时学学技术,了解一下

timer = fig.canvas.new_timer(interval=100)

timer.add_callback(sin_output, ax)

timer.start()

#x和y轴的刻度定义

ax.set_xlim([0, points_dot])

ax.set_ylim([-2, 2])

#ax.set_autoscale_on(False) #默认False

#0~100,每隔10取刻度值

ax.set_xticks(range(0, points_dot, 10))

ax.set_yticks(range(-2, 3, 1))

#显示网格

ax.grid(True)

#显示图例,固定位置=中心上面

ax.legend(loc='upper center', ncol=4)

plt.show()

'''

报错:

RuntimeError: wrapped C/C++ object of type QTimer has been deleted

提示新版的matplotlib已经删除timer了

'''

图4

1ec094bb91f0dbd79ff67bc62c5d449d.gif

希望喜欢,收藏之后好好复习,生动的图像,加深对python的基础知识的理解,熟悉matplotlib作图,以后拿来就用,通俗易懂。感谢作者分享-http://bjbsair.com/2020-04-07/tech-info/30776.html

1.说明:

1.1 推荐指数:★★★

1.2 python的基础知识复习,通过生动的sin函数制作来复习return和yield,列表、函数定义等知识。

1.3 熟悉matplotlib作图相关知识。

1.4 加深理解sin函数,为以后圆的理解打下坚实基础,cos重复不解释了,将sin适当修改即可。

f5cab809d1d475de28b932ee3d311766.png

2.return法,基本方法,代码:

#---导出模块---

import numpy as np

from matplotlib import pyplot as plt

from matplotlib import animation

#定义画布,默认值,这个fig需要,虽然默认大小设置,fig需要挂在动画上

fig = plt.figure()

#坐标轴刻度

ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#color='blue'=蓝色,否则默认为清淡蓝色

line, = ax.plot([], [], lw=2,color='blue')

# 因为动画,所以初始化列表线条

def init():

line.set_data([], [])

return line, #注意逗号

#定义动画

def animate(i):

#x取值范围从0~2,等差数列,分成1000,越大线条越平滑

x = np.linspace(0, 2, 1000)

#动画x和y的值与i的从0~i的取值有关,才动起来

y = np.sin(2 * np.pi * (x - 0.01 * i))

line.set_data(x, y)

return line, #注意逗号

#将fig挂在动画上面

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

#如果需要保存动画,就这样

#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

#标题名称

plt.title('Sin-a-subplot')

plt.show()

图1

99c99cad98eda9f8f85600e6d779746f.gif

3.np.nan法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

#---定义画布---重点讲到区别和含义---

fig, ax = plt.subplots()

#---函数定义法---讲的很清楚了,很多遍---

#复习一下

#x的坐标取值范围,arange法一般是-2π到2π,这里是从0取,0.01,数值越小曲线越平滑

#注意与linspace取等差数列的区别

x = np.arange(0, 2*np.pi, 0.01)

#这是一步并2步了,相当于y=np.sin(x)

line, = ax.plot(x, np.sin(x))

#---初始化---注意np.nan(NaN)知识复习---

def init():

line.set_ydata([np.nan] * len(x))

#等同于下面

#line.set_ydata([] * len(x))

return line,

'''

有两种丢失数据:

None

np.nan(NaN)

None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。

np.nan(NaN)

np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。

但可以使用np.nan*()函数来计算nan,此时视nan为0。

'''

#---定义动画---

def animate(i):

#line.set_ydata(np.sin(x + i / 100))

#与上面一样效果

line.set_ydata(np.sin(x + 0.01 * i))

return line,

#fig的挂在动画上面

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2, blit=True, save_count=50)

# ani.save("movie.mp4")

plt.show()

图2

e03049dda42efdade55961dfececacb8.gif

4.带红色小圆点的yield法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

#---定义画布和ax轴---

fig, ax = plt.subplots()

'''

等价于:fig, ax = plt.subplots(11)=fig, ax = plt.subplots(1,1)

=fig, ax1 = plt.subplot()

或者:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

'''

#---x和y的函数关系---

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

#画正弦函数线

l = ax.plot(x, y)

#运动的圆球,ro=就是red的o=红色的圆球,如果是o,就是默认颜色的圆球

#挂在正弦函数线上的球,初始化坐标为空

dot, = ax.plot([], [], 'ro')

#---初始化定义红色圆球的ax坐标取值范围---

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

#---产生圆球的坐标取值范围,符合正弦函数---

def gen_dot():

#i类似x坐标,np.sin(i)类似y坐标

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

#通过yield函数产生

yield newdot

'''

首先比较下return 与 yield的区别:

return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。

yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束

带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。

'''

#---更新小圆球的位置---

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='imagemagick', fps=30)

plt.show()

图3

e3488a4b39eac61356f1d74c1a7c4667.gif

5 timer法:最新matplotlib好像淘汰了,可以运行,但是报错,可以不用管它,学习技术而已。代码如下:

#---导出模块---

import matplotlib.pyplot as plt

import numpy as np

#---fig和ax放在一起

fig, ax = plt.subplots()

#---初始化定义---

points_dot = 100

#复习一下列表知识,一个列表里有100个相同的0的列表

sin_list = [0] * points_dot

indx = 0

#---画正弦函数线---初始化---

line_sin, = ax.plot(range(points_dot), sin_list, label='sin-d', color='blue')

#---定义sin输出函数---

def sin_output(ax):

global indx, sin_list, line_sin

if indx == 20:

indx = 0

indx += 1

#更新sin列表,初始化全是100个0,更新后就是正弦函数的y坐标

sin_list = sin_list[1:] + [np.sin((indx / 10) * np.pi)]

#看看ydata就是y坐标的意思

line_sin.set_ydata(sin_list)

#从新画正弦函数动态曲线

ax.draw_artist(line_sin)

ax.figure.canvas.draw()

#计时器在新版的matplotlib中已经删除,目前能显示,但是报错,可以不管,暂时学学技术,了解一下

timer = fig.canvas.new_timer(interval=100)

timer.add_callback(sin_output, ax)

timer.start()

#x和y轴的刻度定义

ax.set_xlim([0, points_dot])

ax.set_ylim([-2, 2])

#ax.set_autoscale_on(False) #默认False

#0~100,每隔10取刻度值

ax.set_xticks(range(0, points_dot, 10))

ax.set_yticks(range(-2, 3, 1))

#显示网格

ax.grid(True)

#显示图例,固定位置=中心上面

ax.legend(loc='upper center', ncol=4)

plt.show()

'''

报错:

RuntimeError: wrapped C/C++ object of type QTimer has been deleted

提示新版的matplotlib已经删除timer了

'''

图4

1ec094bb91f0dbd79ff67bc62c5d449d.gif

希望喜欢,收藏之后好好复习,生动的图像,加深对python的基础知识的理解,熟悉matplotlib作图,以后拿来就用,通俗易懂。感谢作者分享-http://bjbsair.com/2020-04-07/tech-info/30776.html

1.说明:

1.1 推荐指数:★★★

1.2 python的基础知识复习,通过生动的sin函数制作来复习return和yield,列表、函数定义等知识。

1.3 熟悉matplotlib作图相关知识。

1.4 加深理解sin函数,为以后圆的理解打下坚实基础,cos重复不解释了,将sin适当修改即可。

f5cab809d1d475de28b932ee3d311766.png

2.return法,基本方法,代码:

#---导出模块---

import numpy as np

from matplotlib import pyplot as plt

from matplotlib import animation

#定义画布,默认值,这个fig需要,虽然默认大小设置,fig需要挂在动画上

fig = plt.figure()

#坐标轴刻度

ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#color='blue'=蓝色,否则默认为清淡蓝色

line, = ax.plot([], [], lw=2,color='blue')

# 因为动画,所以初始化列表线条

def init():

line.set_data([], [])

return line, #注意逗号

#定义动画

def animate(i):

#x取值范围从0~2,等差数列,分成1000,越大线条越平滑

x = np.linspace(0, 2, 1000)

#动画x和y的值与i的从0~i的取值有关,才动起来

y = np.sin(2 * np.pi * (x - 0.01 * i))

line.set_data(x, y)

return line, #注意逗号

#将fig挂在动画上面

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

#如果需要保存动画,就这样

#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

#标题名称

plt.title('Sin-a-subplot')

plt.show()

图1

99c99cad98eda9f8f85600e6d779746f.gif

3.np.nan法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

#---定义画布---重点讲到区别和含义---

fig, ax = plt.subplots()

#---函数定义法---讲的很清楚了,很多遍---

#复习一下

#x的坐标取值范围,arange法一般是-2π到2π,这里是从0取,0.01,数值越小曲线越平滑

#注意与linspace取等差数列的区别

x = np.arange(0, 2*np.pi, 0.01)

#这是一步并2步了,相当于y=np.sin(x)

line, = ax.plot(x, np.sin(x))

#---初始化---注意np.nan(NaN)知识复习---

def init():

line.set_ydata([np.nan] * len(x))

#等同于下面

#line.set_ydata([] * len(x))

return line,

'''

有两种丢失数据:

None

np.nan(NaN)

None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。

np.nan(NaN)

np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。

但可以使用np.nan*()函数来计算nan,此时视nan为0。

'''

#---定义动画---

def animate(i):

#line.set_ydata(np.sin(x + i / 100))

#与上面一样效果

line.set_ydata(np.sin(x + 0.01 * i))

return line,

#fig的挂在动画上面

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2, blit=True, save_count=50)

# ani.save("movie.mp4")

plt.show()

图2

e03049dda42efdade55961dfececacb8.gif

4.带红色小圆点的yield法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

#---定义画布和ax轴---

fig, ax = plt.subplots()

'''

等价于:fig, ax = plt.subplots(11)=fig, ax = plt.subplots(1,1)

=fig, ax1 = plt.subplot()

或者:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

'''

#---x和y的函数关系---

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

#画正弦函数线

l = ax.plot(x, y)

#运动的圆球,ro=就是red的o=红色的圆球,如果是o,就是默认颜色的圆球

#挂在正弦函数线上的球,初始化坐标为空

dot, = ax.plot([], [], 'ro')

#---初始化定义红色圆球的ax坐标取值范围---

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

#---产生圆球的坐标取值范围,符合正弦函数---

def gen_dot():

#i类似x坐标,np.sin(i)类似y坐标

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

#通过yield函数产生

yield newdot

'''

首先比较下return 与 yield的区别:

return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。

yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束

带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。

'''

#---更新小圆球的位置---

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='imagemagick', fps=30)

plt.show()

图3

e3488a4b39eac61356f1d74c1a7c4667.gif

5 timer法:最新matplotlib好像淘汰了,可以运行,但是报错,可以不用管它,学习技术而已。代码如下:

#---导出模块---

import matplotlib.pyplot as plt

import numpy as np

#---fig和ax放在一起

fig, ax = plt.subplots()

#---初始化定义---

points_dot = 100

#复习一下列表知识,一个列表里有100个相同的0的列表

sin_list = [0] * points_dot

indx = 0

#---画正弦函数线---初始化---

line_sin, = ax.plot(range(points_dot), sin_list, label='sin-d', color='blue')

#---定义sin输出函数---

def sin_output(ax):

global indx, sin_list, line_sin

if indx == 20:

indx = 0

indx += 1

#更新sin列表,初始化全是100个0,更新后就是正弦函数的y坐标

sin_list = sin_list[1:] + [np.sin((indx / 10) * np.pi)]

#看看ydata就是y坐标的意思

line_sin.set_ydata(sin_list)

#从新画正弦函数动态曲线

ax.draw_artist(line_sin)

ax.figure.canvas.draw()

#计时器在新版的matplotlib中已经删除,目前能显示,但是报错,可以不管,暂时学学技术,了解一下

timer = fig.canvas.new_timer(interval=100)

timer.add_callback(sin_output, ax)

timer.start()

#x和y轴的刻度定义

ax.set_xlim([0, points_dot])

ax.set_ylim([-2, 2])

#ax.set_autoscale_on(False) #默认False

#0~100,每隔10取刻度值

ax.set_xticks(range(0, points_dot, 10))

ax.set_yticks(range(-2, 3, 1))

#显示网格

ax.grid(True)

#显示图例,固定位置=中心上面

ax.legend(loc='upper center', ncol=4)

plt.show()

'''

报错:

RuntimeError: wrapped C/C++ object of type QTimer has been deleted

提示新版的matplotlib已经删除timer了

'''

图4

1ec094bb91f0dbd79ff67bc62c5d449d.gif

希望喜欢,收藏之后好好复习,生动的图像,加深对python的基础知识的理解,熟悉matplotlib作图,以后拿来就用,通俗易懂。感谢作者分享-http://bjbsair.com/2020-04-07/tech-info/30776.html

1.说明:

1.1 推荐指数:★★★

1.2 python的基础知识复习,通过生动的sin函数制作来复习return和yield,列表、函数定义等知识。

1.3 熟悉matplotlib作图相关知识。

1.4 加深理解sin函数,为以后圆的理解打下坚实基础,cos重复不解释了,将sin适当修改即可。

f5cab809d1d475de28b932ee3d311766.png

2.return法,基本方法,代码:

#---导出模块---

import numpy as np

from matplotlib import pyplot as plt

from matplotlib import animation

#定义画布,默认值,这个fig需要,虽然默认大小设置,fig需要挂在动画上

fig = plt.figure()

#坐标轴刻度

ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))

#color='blue'=蓝色,否则默认为清淡蓝色

line, = ax.plot([], [], lw=2,color='blue')

# 因为动画,所以初始化列表线条

def init():

line.set_data([], [])

return line, #注意逗号

#定义动画

def animate(i):

#x取值范围从0~2,等差数列,分成1000,越大线条越平滑

x = np.linspace(0, 2, 1000)

#动画x和y的值与i的从0~i的取值有关,才动起来

y = np.sin(2 * np.pi * (x - 0.01 * i))

line.set_data(x, y)

return line, #注意逗号

#将fig挂在动画上面

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

#如果需要保存动画,就这样

#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

#标题名称

plt.title('Sin-a-subplot')

plt.show()

图1

99c99cad98eda9f8f85600e6d779746f.gif

3.np.nan法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

#---定义画布---重点讲到区别和含义---

fig, ax = plt.subplots()

#---函数定义法---讲的很清楚了,很多遍---

#复习一下

#x的坐标取值范围,arange法一般是-2π到2π,这里是从0取,0.01,数值越小曲线越平滑

#注意与linspace取等差数列的区别

x = np.arange(0, 2*np.pi, 0.01)

#这是一步并2步了,相当于y=np.sin(x)

line, = ax.plot(x, np.sin(x))

#---初始化---注意np.nan(NaN)知识复习---

def init():

line.set_ydata([np.nan] * len(x))

#等同于下面

#line.set_ydata([] * len(x))

return line,

'''

有两种丢失数据:

None

np.nan(NaN)

None是Python自带的,其类型为python object。因此,None不能参与到任何计算中。

np.nan(NaN)

np.nan是浮点类型,能参与到计算中。但计算的结果总是NaN。

但可以使用np.nan*()函数来计算nan,此时视nan为0。

'''

#---定义动画---

def animate(i):

#line.set_ydata(np.sin(x + i / 100))

#与上面一样效果

line.set_ydata(np.sin(x + 0.01 * i))

return line,

#fig的挂在动画上面

ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2, blit=True, save_count=50)

# ani.save("movie.mp4")

plt.show()

图2

e03049dda42efdade55961dfececacb8.gif

4.带红色小圆点的yield法,代码:

#---导出模块---

import numpy as np

import matplotlib.pyplot as plt

from matplotlib import animation

#---定义画布和ax轴---

fig, ax = plt.subplots()

'''

等价于:fig, ax = plt.subplots(11)=fig, ax = plt.subplots(1,1)

=fig, ax1 = plt.subplot()

或者:

fig = plt.figure()

ax = fig.add_subplot(1,1,1)

'''

#---x和y的函数关系---

x = np.linspace(0, 2*np.pi, 200)

y = np.sin(x)

#画正弦函数线

l = ax.plot(x, y)

#运动的圆球,ro=就是red的o=红色的圆球,如果是o,就是默认颜色的圆球

#挂在正弦函数线上的球,初始化坐标为空

dot, = ax.plot([], [], 'ro')

#---初始化定义红色圆球的ax坐标取值范围---

def init():

ax.set_xlim(0, 2*np.pi)

ax.set_ylim(-1, 1)

return l

#---产生圆球的坐标取值范围,符合正弦函数---

def gen_dot():

#i类似x坐标,np.sin(i)类似y坐标

for i in np.linspace(0, 2*np.pi, 200):

newdot = [i, np.sin(i)]

#通过yield函数产生

yield newdot

'''

首先比较下return 与 yield的区别:

return:在程序函数中返回某个值,返回之后函数不在继续执行,彻底结束。

yield: 带有yield的函数是一个迭代器,函数返回某个值时,会停留在某个位置,返回函数值后,会在前面停留的位置继续执行,直到程序结束

带有 yield 的函数不再是一个普通函数,而是一个生成器generator,可用于迭代。

'''

#---更新小圆球的位置---

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='imagemagick', fps=30)

plt.show()

图3

e3488a4b39eac61356f1d74c1a7c4667.gif

5 timer法:最新matplotlib好像淘汰了,可以运行,但是报错,可以不用管它,学习技术而已。代码如下:

#---导出模块---

import matplotlib.pyplot as plt

import numpy as np

#---fig和ax放在一起

fig, ax = plt.subplots()

#---初始化定义---

points_dot = 100

#复习一下列表知识,一个列表里有100个相同的0的列表

sin_list = [0] * points_dot

indx = 0

#---画正弦函数线---初始化---

line_sin, = ax.plot(range(points_dot), sin_list, label='sin-d', color='blue')

#---定义sin输出函数---

def sin_output(ax):

global indx, sin_list, line_sin

if indx == 20:

indx = 0

indx += 1

#更新sin列表,初始化全是100个0,更新后就是正弦函数的y坐标

sin_list = sin_list[1:] + [np.sin((indx / 10) * np.pi)]

#看看ydata就是y坐标的意思

line_sin.set_ydata(sin_list)

#从新画正弦函数动态曲线

ax.draw_artist(line_sin)

ax.figure.canvas.draw()

#计时器在新版的matplotlib中已经删除,目前能显示,但是报错,可以不管,暂时学学技术,了解一下

timer = fig.canvas.new_timer(interval=100)

timer.add_callback(sin_output, ax)

timer.start()

#x和y轴的刻度定义

ax.set_xlim([0, points_dot])

ax.set_ylim([-2, 2])

#ax.set_autoscale_on(False) #默认False

#0~100,每隔10取刻度值

ax.set_xticks(range(0, points_dot, 10))

ax.set_yticks(range(-2, 3, 1))

#显示网格

ax.grid(True)

#显示图例,固定位置=中心上面

ax.legend(loc='upper center', ncol=4)

plt.show()

'''

报错:

RuntimeError: wrapped C/C++ object of type QTimer has been deleted

提示新版的matplotlib已经删除timer了

'''

图4

1ec094bb91f0dbd79ff67bc62c5d449d.gif

希望喜欢,收藏之后好好复习,生动的图像,加深对python的基础知识的理解,熟悉matplotlib作图,以后拿来就用,通俗易懂。

这篇关于python中正弦函数模块_python3 的matplotlib的4种办法制作动态sin函数程序详述的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/275656

相关文章

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

Python包管理工具pip的升级指南

《Python包管理工具pip的升级指南》本文全面探讨Python包管理工具pip的升级策略,从基础升级方法到高级技巧,涵盖不同操作系统环境下的最佳实践,我们将深入分析pip的工作原理,介绍多种升级方... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

一文详解SpringBoot中控制器的动态注册与卸载

《一文详解SpringBoot中控制器的动态注册与卸载》在项目开发中,通过动态注册和卸载控制器功能,可以根据业务场景和项目需要实现功能的动态增加、删除,提高系统的灵活性和可扩展性,下面我们就来看看Sp... 目录项目结构1. 创建 Spring Boot 启动类2. 创建一个测试控制器3. 创建动态控制器注

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核