今日表情 ????
先上图片:
再上视频:
最后上代码:
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)
收工。????
公众号后台回复关键词:动态图。添加作者微信获取源代码和数据集。
万水千山总是情,点个在看行不行?⭐️