Python实现Matplotlib,Seaborn动态数据图的示例代码
作者:pythonic生物人 时间:2023-02-03 23:47:22
Matplotlib
效果图如下
主要使用matplotlib.animation.FuncAnimation
,上核心代码,
# 定义静态绘图函数
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')
# 注释文本
ax.text(1,
0.4,
year,
transform=ax.transAxes,
color='#777777',
size=46,
ha='right',
weight=800)
ax.text(0,
1.06,
'单位 (每1000)',
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.12,
'1500~2018年世界人口最多城市',
transform=ax.transAxes,
size=24,
weight=600,
ha='left')
plt.box(False)
# 调用matplotlib.animation.FuncAnimation让静态图动起来
animator = animation.FuncAnimation(fig,
draw_barchart,
frames=range(1968, 2019))
# Jupyter Notebook里展示动图animation
HTML(animator.to_jshtml())
在绘图数据部分改自己的数据既可为所欲为的使用了~
Seaborn
效果图如下
代码
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import seaborn as sns
import numpy as np
import palettable
def get_data(i=0):
x, y = np.random.normal(loc=i, scale=3, size=(2, 260))
return x, y
x, y = get_data()
g = sns.JointGrid(x=x, y=y, size=4)
g.fig.set_size_inches(10, 8)
lim = (-10, 10)
def prep_axes(g, xlim, ylim):
g.ax_joint.clear()
g.ax_joint.set_xlim(xlim)
g.ax_joint.set_ylim(ylim)
g.ax_marg_x.clear()
g.ax_marg_x.set_xlim(xlim)
g.ax_marg_y.clear()
g.ax_marg_y.set_ylim(ylim)
plt.setp(g.ax_marg_x.get_xticklabels(), visible=False)
plt.setp(g.ax_marg_y.get_yticklabels(), visible=False)
plt.setp(g.ax_marg_x.yaxis.get_majorticklines(), visible=False)
plt.setp(g.ax_marg_x.yaxis.get_minorticklines(), visible=False)
plt.setp(g.ax_marg_y.xaxis.get_majorticklines(), visible=False)
plt.setp(g.ax_marg_y.xaxis.get_minorticklines(), visible=False)
plt.setp(g.ax_marg_x.get_yticklabels(), visible=False)
plt.setp(g.ax_marg_y.get_xticklabels(), visible=False)
def animate(i):
g.x, g.y = get_data(i)
prep_axes(g, lim, lim)
g.plot_joint(sns.kdeplot,
cmap='Paired')
g.plot_marginals(sns.kdeplot, color='blue', shade=True)
frames = np.sin(np.linspace(0, 2 * np.pi, 17)) * 5
ani = matplotlib.animation.FuncAnimation(g.fig,
animate,
frames=frames,
repeat=True)
HTML(ani.to_jshtml())
和Matplotlib代码类似,不过多解释。
来源:https://mp.weixin.qq.com/s/gSaQvmT1j4IQmGQOrejGTw
标签:Python,Matplotlib,Seaborn,动态数据图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在windows下使用python进行串口通讯的方法
2022-01-03 11:10:19
pycharm配置git(图文教程)
2021-10-08 19:09:43
![](https://img.aspxhome.com/file/2023/3/92273_0s.png)
Python实现识别手写数字 Python图片读入与处理
2022-07-14 17:48:10
![](https://img.aspxhome.com/file/2023/7/127797_0s.jpg)
mysql 通配符(sql 高级过滤)
2024-01-24 17:15:39
详解SQL Server数据库状态和文件状态
2024-01-26 14:09:28
python为Django项目上的每个应用程序创建不同的自定义404页面(最佳答案)
2022-07-26 19:08:11
SqlServer 2005 T-SQL Query 学习笔记(2)
2024-01-20 20:21:51
php注册系统和使用Xajax即时验证用户名是否被占用
2023-09-12 05:27:55
![](https://img.aspxhome.com/file/2023/4/55524_0s.png)
SQL语句的执行原理分析
2024-01-15 03:17:59
ASP获取刚插入记录的自动编号ID
2008-11-17 20:41:00
使用python模拟高斯分布例子
2021-04-27 05:22:22
![](https://img.aspxhome.com/file/2023/9/118359_0s.jpg)
Python读取word文本操作详解
2023-08-23 15:32:50
![](https://img.aspxhome.com/file/2023/3/61993_0s.jpg)
js保存当前路径(cookies记录)
2024-06-21 06:44:16
![](https://img.aspxhome.com/file/2023/9/56619_0s.png)
PHP导出数据超时的优化建议
2023-06-29 18:36:32
浅谈MySQL模糊查询中通配符的转义
2024-01-26 06:51:57
asp内置对象ObjectContext详解
2007-09-18 13:16:00
MAC下修改mysql默认字符集为utf8的方法
2024-01-20 15:37:16
![](https://img.aspxhome.com/file/2023/8/66158_0s.png)
有关缓存 Cache 的随想
2008-06-09 14:25:00
![](https://img.aspxhome.com/file/UploadPic/20086/9/200869143245569s.jpg)
Python入门篇之条件、循环
2021-09-16 16:10:00
Django在win10下的安装并创建工程
2021-10-12 18:54:57
![](https://img.aspxhome.com/file/2023/6/100846_0s.png)