在先前的一篇文章當中,小編當時分享瞭如何使用#Python
#gifgif模組來製作
gif格式的圖表,
gifgif格式圖表的新方法,調用的是
matplotlib#的相關模組,其中的步驟與方法相當簡單易懂。
下載和導入資料庫
我們這次用到的資料集是bokeh模組自帶的資料集,通過下面這一行程式碼直接就可以下載
import bokeh bokeh.sampledata.download()然後導入後面要用到的資料集,我們挑選的是指定國家的1950年至今不同年齡階段的人口所佔比重的資料
from bokeh.sampledata.population import data import numpy as np data = filter_loc('United States of America') data.head()output
先繪製若干張靜態的圖表
我們可以先繪製若干張靜態的圖表,然後將這幾張圖表合成一張gif格式的動圖即可,程式碼如下
import seaborn as sns import matplotlib.pyplot as plt import matplotlib.patheffects as fx # 绘制图表的函数 def make_plot(year): # 根据年份来筛选出数据 df = data[data.Year == year] # 制作图表 fig, (ax1, ax2) = plt.subplots(1, 2, sharey = True) ax1.invert_xaxis() fig.subplots_adjust(wspace = 0) ax1.barh(df[df.Sex == 'Male'].AgeGrp, df[df.Sex == 'Male'].percent, label = 'Male') ax2.barh(df[df.Sex == 'Female'].AgeGrp, df[df.Sex == 'Female'].percent, label = 'Female', color = 'C1') country = df.Location.iloc[0] if country == 'United States of America': country == 'US' fig.suptitle(f'......') fig.supxlabel('......') fig.legend(bbox_to_anchor = (0.9, 0.88), loc = 'upper right') ax1.set_ylabel('Age Groups') return fig我們自訂了一個繪製圖表的函數,其中的參數是年份,邏輯很簡單,我們是想根據年份來篩選出數據,然後根據篩選出的數據來繪製圖表,每一年的圖表不盡相同
years = [i for i in set(data.Year) if i < 2022] years.sort() for year in years: fig = make_plot(year) fig.savefig(f'{year}.jpeg',bbox_inches = 'tight')output
gifgif格式的圖表幾個,程式碼如下
import matplotlib.animation as animation fig, ax = plt.subplots() ims = [] for year in years: im = ax.imshow(plt.imread(f'{year}.jpeg'), animated = True) ims.append([im]) ani = animation.ArtistAnimation(fig, ims, interval=600) ani.save('us_population.gif')output
還有另一個想法
可能看到這兒,有人會覺得上面提到的方法稍顯麻煩,畢竟我們需要先生成數十張靜態的圖表,要是電腦的磁碟空間有點緊張的話,或者還沒有這樣的一個地方來存放這數十張的圖表。於是乎就會疑問道,是不是可以一步到位的來。當然也是可以的,例如我們打算繪製1950年到2020年不同年齡階段的人口比例分佈圖,首先第一步在於我們先要繪製1950年,也就是起始年,該年不同年齡階段的人口比例分佈圖,程式碼如下fig, (ax1, ax2) = plt.subplots(1, 2, sharey = True) df = data[data.Year == 1955] y_pos = [i for i in range(len(df[df.Sex == 'Male']))] male = ax1.barh(y_pos, df[df.Sex == 'Male'].percent, label = 'Male', tick_label = df[df.Sex == 'Male'].AgeGrp) female = ax2.barh(y_pos, df[df.Sex == 'Female'].percent, label = 'Female', color = 'C1', tick_label = df[df.Sex == 'Male'].AgeGrp) ax1.invert_xaxis() fig.suptitle('.......') fig.supxlabel('....... (%)') fig.legend(bbox_to_anchor = (0.9, 0.88), loc = 'upper right') ax1.set_ylabel('Age Groups')output
def run(year): # 通过年份来筛选出数据 df = data[data.Year == year] # 针对不同地性别来绘制 total_pop = df.Value.sum() df['percent'] = df.Value / total_pop * 100 male.remove() y_pos = [i for i in range(len(df[df.Sex == 'Male']))] male.patches = ax1.barh(y_pos, df[df.Sex == 'Male'].percent, label = 'Male', color = 'C0', tick_label = df[df.Sex == 'Male'].AgeGrp) female.remove() female.patches = ax2.barh(y_pos, df[df.Sex == 'Female'].percent, label = 'Female', color = 'C1', tick_label = df[df.Sex == 'Female'].AgeGrp) text.set_text(year) return male#, female
然后我们调用animation.FuncAnimation()
方法,
ani = animation.FuncAnimation(fig, run, years, blit = True, repeat = True, interval = 600) ani.save('文件名.gif')
output
这样就可以一步到位生成gif
格式的图表,避免生成数十张繁多地静态图片了。
将若干张<span style="color: #2b2b2b;">gif</span>
动图放置在一张大图当中
最后我们可以将若干张gif
动图放置在一张大的图表当中,代码如下
import matplotlib.animation as animation # 创建一个新的画布 fig, (ax, ax2, ax3) = plt.subplots(1, 3, figsize = (10, 3)) ims = [] for year in years: im = ax.imshow(plt.imread(f'文件1{year}.jpeg'), animated = True) im2 = ax2.imshow(plt.imread(f'文件2{year}.jpeg'), animated = True) im3 = ax3.imshow(plt.imread(f'文件3{year}.jpeg'), animated = True) ims.append([im, im2, im3]) ani = animation.ArtistAnimation(fig, ims, interval=600) ani.save('comparison.gif')
output
以上是用Python繪製超酷的gif動圖,驚艷了所有人的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

WebStorm Mac版
好用的JavaScript開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器