世界各国历年GDP排名前15?Python动态图告诉你!

今日表情 ????

先上图片

再上视频

最后上代码


import numpy as np 
import pandas as pd 
from matplotlib import pyplot as plt
import matplotlib.animation as  animation 

import imageio
import os 
from PIL import Image
import pandas_alive 


cmap = [
'#2E91E5',
'#1CA71C',
'#DA16FF',
'#B68100',
'#EB663B',
'#00A08B',
'#FC0080',
'#6C7C32',
'#862A16',
'#620042',
'#DA60CA',
'#0D2A63']*100

plt.rcParams['animation.writer'] = 'html'
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

def rgba_to_rgb(img_rgba):
    img_rgb = Image.new("RGB", img_rgba.size, (255, 255, 255))
    img_rgb.paste(img_rgba, mask=img_rgba.split()[3]) 
    return img_rgb 

def html_to_gif(html_file, gif_file, duration=0.5):
    path = html_file.replace(".html","_frames")
    images = [os.path.join(path,x) for x in sorted(os.listdir(path))]
    frames = [imageio.imread(x) for x in images]
    if frames[0].shape[-1]==4:
        frames = [np.array(rgba_to_rgb(Image.fromarray(x))) for x in frames]
    imageio.mimsave(gif_file, frames, 'gif', duration=duration)
    return gif_file

dfgdp = pd.read_csv("./data/gdp_of_world.csv",sep = "\t")
dfgdp = dfgdp.set_index("country")
dfgdp.index.name = None
df = dfgdp.loc[:,~dfgdp.columns.isin(['name','region', 'income_group', 'capital', 'currency'])].T 

df.index = pd.to_datetime(df.index)
df = df/1e8 

html_file = "世界各国历年GDP排名.html"
gif_file = html_file.replace(".html",".gif")

def current_total(values):
    total = values.sum()
    s = '世界GDP总额: {:.2f}亿美元'.format(total)
    return {'x': .90, 'y': .2, 's': s, 'ha': 'right', 'size': 11}

chart = df.plot_animated(filename = html_file,title="世界各国历年GDP排名",
        figsize = (6,4),dpi=144,period_summary_func=current_total,
        n_visible=15,bar_texttemplate = '{x:.2f}',filter_column_colors = True,period_fmt="%Y年")

html_to_gif(html_file,gif_file,duration = 0.2)

收工。????

公众号后台回复关键词:动态图。添加作者微信获取源代码和数据集。

万水千山总是情,点个在看行不行?⭐️ 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值