在第20行中,创建实际的动画对象。 Blit参数只重新绘制更改的部分。
这是在Matplolib中制作动画的基本知识。 只要稍微调整一下代码,就可以创建有趣的可视化。 接下来,我们来看几个可视化的例子。
与放大的线圈一样,GreeksforGreeks有一个创建图形的好例子。 我们一起在animation模块的帮助下制作慢慢扩展的可动线圈。 这段代码非常类似于正弦波图表,只有很小的调整。
import matplotlib.pyplot as plt
importmatplotlib.animationasanimation
导入编号为NP
PLT.style.use(Dark_background ) )。
fig=plt.figure (
ax=PLT.axes (xlim=(-50,50 ),ylim=(-50,50 ) )
line,=ax.plot([],[],lw=2) )。
# initial ization函数
定义(:
# creating an empty plot/frame
line.set_data([],[] ) )。
return line,
# lists to store x and y axis points
xdata,ydata=[],[]
# animation函数
defanimate(I ) :
# t is a parameter
t=0.1*i
# x,y values to be plotted
x=t*NP.sin(t ) )
y=t*NP.cos(t )
# appending new points to x,y axes points list
是xdata.append(x )
ydata.append(y ) )。
line.set_data(xdata,ydata ) )。
return line,
# setting a title for the plot
PLT.title (creatingagrowingcoilwithmatplotlib! ' )
# hiding the axis详细信息
PLT.AXIS(off ) )。
# call the animator
anim=animation.func animation (fig,animate,init_func=init,
frames=500,interval=20,blit=True )
# savetheanimationasmp 4视频文件
anim.save('coil.gif ',writer='imagemagick ' )
实时更新映射对于创建其他与时间相关的动态数据(如股票数据、传感器数据等)很有用。 创建基础地图,并在系统中输入更多数据时自动更新基础地图。 在此,制作某假设公司所在月内的股价图。
#导入库
import matplotlib.pyplot as plt
importmatplotlib.animationasanimation
fig=plt.figure (
#creating a subplot
ax1=fig.add _ subplot (1,1,1 ) ) ) ) )。
defanimate(I ) :
DATA=open('stock.txt ',' r ' ).read ) )。
lines=data.split('\n ' )
xs=[]
ys=[]
for line in lines:
x,y=line.split (',') # Delimiter is comma
xs.append(float ) x ) )
ys.append(float ) )
ax1.clear () )
ax1.plot(xs,ys ) )。
PLT.xlabel('date ) )。
PLT.ylabel('price ) )。
PLT.title (livegraphwithmatplotlib ) )。
ani=animation.func animation (fig,animate,interval=1000 ) )。
plt.show () )
打开终端并运行python文件,可以得到如下所示自动更新的图表。
更新间隔为1000毫秒或1秒。
3D图中的动画常用于创建3D图形,如果能把这些图形的视点制作成动画呢? 这是一种在更改摄影机视图后使用所有生成的图像创建动画的方法。 pythongraphgallery(python图形库)具有执行这些任务的特殊部分。
首先创建一个名为volcano的文件夹,并将其放在与记事本相同的目录中。 然后,将用于动画的所有图形保存在此文件夹中。
#库
from mpl _ toolkits.MP lot3dimportaxes 3d
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
# get the data (csvfileishostedontheweb )。
URL=' https://python-graph-gallery.com/WP-content/uploads/volcano.CSV '
data=pd.read_csv(url )
# Transform it to a long format
df=data.unstack ().reset_index ) )。
df.columns=['X ',' y ',' Z']
# andtransformtheoldcolumnameinsomethingnumeric
df['x']=PD.categorical(df['x ' ) ]
df['X']=df['X'].cat.codes
# We are going to do 20 plots,for 20 different angles
forangleinrange (70,210,2 ) :
# Make the plot
fig=plt.figure (
ax=fig.GCA(projection='3d ' ) )。
ax.plot_trisurf(df(y ),df (x ),df (z ),cmap=plt.cm.viridis,linewidth=0.2 ) )
ax.view_init(30,angle ) ) ) ) )。
filename=' volcano/volcano _ step ' str (angle ) '.png ) )
PLT.savefig(filename,dpi=96 ) )。
plt.gca (
这样就可以在Volcano文件夹中创建多个PNG文件。 然后,使用ImageMagick将这些PNG文件转换为动画,该软件是用于创建、编辑和合成图像的软件。 打开终端,导航到Volcano文件夹,然后输入以下命令:
convert -delay 10 Volcano*
. pnganimated_volcano.gif
使用Celluloid模块对Celluloid进行动画处理是一个python模块,可以简化在matplotlib中创建动画的过程。 这个库创建matplotlib图,然后创建相机。 然后,再次启用此图,创建每帧的动画,并使用上述相机拍摄快照。 最后,使用捕获的所有帧创建动画。
文章来源:《气候变化研究进展》 网址: http://www.qhbhyjjzzz.cn/zonghexinwen/2022/1207/1123.html
气候变化研究进展投稿 | 气候变化研究进展编辑部| 气候变化研究进展版面费 | 气候变化研究进展论文发表 | 气候变化研究进展最新目录
Copyright © 2021 《气候变化研究进展》杂志社 版权所有 Power by DedeCms
投稿电话: 投稿邮箱: