在先前的一篇文章當中,小編當時分享瞭如何使用#Python
#gifgif模組來製作
gif格式的圖表,
gifgif格式圖表的新方法,調用的是
matplotlib#的相關模組,其中的步驟與方法相當簡單易懂。
bokeh模組自帶的資料集,通過下面這一行程式碼直接就可以下載
import bokeh bokeh.sampledata.download()
from bokeh.sampledata.population import data import numpy as np data = filter_loc('United States of America') data.head()
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')
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')
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')
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中文網其他相關文章!