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如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

基于Python打造一个智能单词管理神器

《基于Python打造一个智能单词管理神器》这篇文章主要为大家详细介绍了如何使用Python打造一个智能单词管理神器,从查询到导出的一站式解决,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 项目概述:为什么需要这个工具2. 环境搭建与快速入门2.1 环境要求2.2 首次运行配置3. 核心功能使用指

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

使用Python和Pyecharts创建交互式地图

《使用Python和Pyecharts创建交互式地图》在数据可视化领域,创建交互式地图是一种强大的方式,可以使受众能够以引人入胜且信息丰富的方式探索地理数据,下面我们看看如何使用Python和Pyec... 目录简介Pyecharts 简介创建上海地图代码说明运行结果总结简介在数据可视化领域,创建交互式地