matpoltlib 动态画图

本文详细介绍了使用Python的Matplotlib库创建动态图表的过程,包括多个子图的动画效果实现,展示了如何通过FuncAnimation函数更新数据并实时显示。适用于数据可视化和动态演示场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

# first set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax1 = fig.add_subplot(3, 1, 1, xlim=(0, 2), ylim=(-4, 4))
ax2 = fig.add_subplot(3, 1, 2, xlim=(0, 2), ylim=(-4, 4))
ax3 = fig.add_subplot(3, 1, 3, xlim=(0, 2), ylim=(-4, 4))
line, = ax1.plot([], [], lw=2)
line2, = ax2.plot([], [], lw=2)
line3, = ax3.plot([], [], lw=2)


def init():
	line.set_data([], [])
	line2.set_data([], [])
	line3.set_data([], [])
	return line, line2,line3


# animation function.  this is called sequentially
def animate(i):
	x = np.linspace(0, 2, 100)
	y = np.sin(2 * np.pi * (x - 0.01 * i))
	line.set_data(x, y)

	x2 = np.linspace(0, 2, 100)
	y2 = np.cos(2 * np.pi * (x2 - 0.01 * i)) * np.sin(2 * np.pi * (x - 0.01 * i))
	line2.set_data(x2, y2)

	x3 = np.linspace(0, 2, 100)
	y3 = np.cos(2 * np.pi * (x3 - 0.01 * i)) * np.sin(2 * np.pi * (x - 0.01 * i))
	line3.set_data(x3, y3)
	return line, line2

if __name__ == '__main__':


	anim1 = animation.FuncAnimation(fig, animate, init_func=init, frames=50, interval=10)
	plt.show()

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

# first set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax_list=[]
for ax_one in range(20):
	ax1 = fig.add_subplot(4,5,ax_one+1, xlim=(0, 2), ylim=(-4, 4))
	line, = ax1.plot([], [], lw=2)
	ax_list.append((ax1,line))


def init():
	line_list=[]
	for one_line in ax_list:
		one_line[1].set_data([], [])
		line_list.append(one_line[1])
	return tuple(line_list)


# animation function.  this is called sequentially
def animate(i):

	x = np.linspace(0, 2, 100)
	y = np.sin(2 * np.pi * (x - 0.01 * i))
	line_list = []
	for one_line in ax_list:
		one_line[1].set_data(x, y)
		line_list.append(one_line[1])
	return tuple(line_list)

if __name__ == '__main__':

	anim1 = animation.FuncAnimation(fig, animate, init_func=init, frames=50, interval=10)
	plt.show()



from matplotlib import pyplot as plt
from matplotlib import animation
import matplotlib.font_manager as fm
fig = plt.figure()
ax_list=[]
for ax_one in range(3):
	ax1 = fig.add_subplot(3,1,ax_one+1, ylim=(0,100))
	ax1.spines['top'].set_color('none')
	ax1.spines['right'].set_color('none')
	myfont = fm.FontProperties(fname=r'/home/chenyang/PycharmProjects/show_face_decetor/fonts/simsun.ttf')
	name_list = ['Sur', "Fea", "Dis", "Hap", "Sad", "Ang", "Nat", "x", "y", "z"]
	index = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
	index = [float(c) for c in index]
	plt.ylim(ymax=110, ymin=0)
	plt.xticks(index, name_list, rotation=20, fontsize=10)
	plt.ylabel("lsd", fontproperties=myfont, fontsize=20)
	ax_list.append(ax1)





def init():
	line_list=[]
	for one_line in ax_list:
		line_list.append(one_line)
	return tuple(line_list)


# animation function.  this is called sequentially
def animate(i):

	line_list = []
	for ax_one in ax_list:
		plt.ylabel(str(i), fontproperties=myfont, fontsize=20)

		line_list.append(ax_one.bar(range(len(name_list)),[i,23,23,23,4,34,3,34,3,23], color='bgr'))
	return tuple(line_list)

if __name__ == '__main__':

	anim1 = animation.FuncAnimation(fig, animate, init_func=init, frames=100, interval=10)
	plt.show()

from matplotlib import pyplot as plt
from matplotlib import animation

import random

# first set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax_list=[]
for ax_one in range(20):
	ax1 = fig.add_subplot(4,5,ax_one+1, xlim=(0, 9), ylim=(0, 100))
	line, = ax1.plot([], [], lw=2)
	ax_list.append((ax1,line))


def init():
	line_list=[]
	for one_line in ax_list:
		one_line[1].set_data([], [])
		line_list.append(one_line[1])
	return tuple(line_list)


# animation function.  this is called sequentially
l_=[]
for _ in range(1000):
	x=[]
	for _ in range(10):
		x.append(random.randrange(100))
	l_.append(x)



def animate(i):
	x = [range(10)]
	y = l_.pop(0)
	line_list = []

	for one_line in ax_list:
		one_line[1].set_data(x, y)
		line_list.append(one_line[1])

	return tuple(line_list)

if __name__ == '__main__':

	anim1 = animation.FuncAnimation(fig, animate, init_func=init, frames=100000, interval=10)
	plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东方佑

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值