python制作动态排序图,,一、代码import
python制作动态排序图,,一、代码import
一、代码
import pandas as pdimport matplotlib.pyplot as pltimport matplotlib.ticker as tickerimport matplotlib.animation as animationdf = pd.read_csv(r‘动态排序数据.csv‘, usecols=[‘name‘, ‘group‘, ‘year‘, ‘value‘])colors = dict(zip( [‘India‘, ‘Europe‘, ‘Asia‘, ‘Latin America‘, ‘Middle East‘, ‘North America‘, ‘Africa‘], [‘#adb0ff‘, ‘#ffb3ff‘, ‘#90d595‘, ‘#e48381‘, ‘#aafbff‘, ‘#f7bb5f‘, ‘#eafb50‘]))group_lk = df.set_index(‘name‘)[‘group‘].to_dict()fig, ax = plt.subplots(figsize=(15, 8))def draw_barchart(year): dff = df[df[‘year‘].eq(year)].sort_values(by=‘value‘, ascending=True).tail(10) ax.clear() ax.barh(dff[‘name‘], dff[‘value‘], color=[colors[group_lk[x]] for x in dff[‘name‘]]) dx = dff[‘value‘].max() / 200 for i, (value, name) in enumerate(zip(dff[‘value‘], dff[‘name‘])): ax.text(value - dx, i, name, size=14, weight=600, ha=‘right‘, va=‘bottom‘) ax.text(value - dx, i - .25, group_lk[name], size=10, color=‘#444444‘, ha=‘right‘, va=‘baseline‘) ax.text(value + dx, i, f‘{value:,.0f}‘, size=14, ha=‘left‘, va=‘center‘) # ... polished styles ax.text(1, 0.4, year, transform=ax.transAxes, color=‘#777777‘, size=46, ha=‘right‘, weight=800) ax.text(0, 1.06, ‘Population (thousands)‘, transform=ax.transAxes, size=12, color=‘#777777‘) ax.xaxis.set_major_formatter(ticker.StrMethodFormatter(‘{x:,.0f}‘)) ax.xaxis.set_ticks_position(‘top‘) ax.tick_params(axis=‘x‘, colors=‘#777777‘, labelsize=12) ax.set_yticks([]) ax.margins(0, 0.01) ax.grid(which=‘major‘, axis=‘x‘, linestyle=‘-‘) ax.set_axisbelow(True) ax.text(0, 1.11, ‘The most populous cities in the world from 1968 to 2018‘, transform=ax.transAxes, size=24, weight=600, ha=‘left‘) ax.text(1, 0, ‘by maple‘, transform=ax.transAxes, ha=‘right‘,size=15, color=‘#777777‘, bbox=dict(facecolor=‘white‘, alpha=0.8, edgecolor=‘white‘)) plt.box(False)animator = animation.FuncAnimation(fig=fig, func=draw_barchart, frames=range(1968, 2019))animator.save("动态排序.gif")
数据地址:
https://pan.baidu.com/s/1Qml2z48B4-6PEB1Pjg4v_w
提取密码:2jb4
python制作动态排序图
相关内容
- python爬取opgg的LOL英雄数据,,完整源码链接:htt
- ubuntu18 tensorflow cpu fast_rcnn,,(flappbird
- python 合并k个有序链表,,from heapq
- python-pygame的触碰方法,, 1 #!/usr/
- Python学习第一天,,一、Python的介
- python -- Counter 类,,python --
- Python制作回合制手游外挂简单教程(上),,引入:每次
- python多版本管理工具--pyenv安装和使用,,文章目录一.
- 一篇文章助你理解Python2中字符串编码问题,,前几天给
- Python学习的第三次的总结,,元组 #组内元素可读
评论关闭