Celluloid让matplotlib动画如此简单1
1Celluloid库
1。1Celluloid库来简化Matplotlib中的动画。
1。2通过创建一个摄像头来实现,该摄像头的目标是在每次图形的某个参数发生变化时拍摄该图形的快照。
1。3Pythoncelluloid用于创建拥有动画效果的Matplotlib图表,国内介绍比较少。
2准备
2。1安装pipinstallihttps:pypi。tuna。tsinghua。edu。cnsimplecelluloid本机安装sudopip3。8installihttps:pypi。tuna。tsinghua。edu。cnsimplecelluloid
2。2环境:python3。8matplotlib3。3。2。
2。3Pythonmatplotlibcelluloid的相关知识。
3效果图
4代码讲解:
4。1第一步:导入模块frommatplotlibimportpyplotaspltfromcelluloidimportCamera
4。2第二步:字体设置,可要可不要(解决负号i后出现8722问题)plt。rcParams〔font。sansserif〕〔SimHei〕显示中文标签本机默认DejaVuSansplt。rcParams〔font。sansserif〕〔DejaVuSans〕plt。rcParams〔axes。unicodeminus〕False
4。3第三步:定义画布和放入摄像头内定义画布figplt。figure()个人爱好,背景颜色为黑色画布外的背景颜色设置fig。patch。setfacecolor(black)定义画布的背景颜色设置axfig。gca()ax。setfacecolor(black)将画布放入摄像头内cameraCamera(fig)
4。4第四步:x和y坐标轴与直线关系,直线选择样式取值范围12是y的取值foriinrange(12):画直线x的取值默认直线plt。plot(〔i〕12)样式:o实线;o虚线;。o虚点线;:o点线随机颜色plt。plot(〔i〕12,o)指定颜色plt。plot(〔i〕12,o,colorgreen)plt。plot(〔i〕12,o,markersize20)垂直从左向右滚动y〔0,1,2,3,4,5,6,7,8,9,10,11〕同上y〔11,10,9,8,7,6,5,4,3,2,1,0〕x〔i〕12垂直从右向左滚动x〔i〕12改号报错untimeWarning:Glyph8722missingfromcurrentfont。font。settext(s,0。0,flagsflags)plt。plot(x,y,o,markersize20)摄像头快照camera。snap()
4。5第五步:结尾动画挂起animationcamera。animate()动画保存animation。save(line。gif)展示plt。show()
5简洁版
5。1效果图:从下往上运动。
5。2完整简洁代码导入模块frommatplotlibimportpyplotaspltfromcelluloidimportCamera定义画布figplt。figure()画布外的背景颜色设置fig。patch。setfacecolor(black)定义画布的背景颜色设置axfig。gca()ax。setfacecolor(black)将画布放入摄像头内cameraCamera(fig)取值范围12是y的取值foriinrange(12):plt。plot(〔i〕12,o,markersize20)摄像头快照camera。snap()动画挂起animationcamera。animate()展示plt。show()
未完待续。