Python实现Matplotlib,Seaborn动态数据图的示例代码
作者:pythonic生物人 发布时间:2023-02-03 23:47:22
标签:Python,Matplotlib,Seaborn,动态数据图
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


猜你喜欢
- 代码如下:--根据MAX(MIN)ID CREATE PROC [dbo].[proc_select_id] @pageindex int=
- 目录1、索引失效原因2、再来看看哪些情况会破坏索引的有序性。- 对索引字段做函数操作- 隐式类型转换- 隐式字符编码转换3、总结1、索引失效
- 如下所示:#! /usr/bin/python3# coding = utf-8# from PyQt5 import QtGui,QtCo
- 在python的时间使用时,我们无非就是输出字符串的形式,又或者是其他的形式跟字符串之间的来回转换。时间数组对于我们获取具体的年或是天数,都
- 注:本文涉及的是解压缩版的安装安装教程下载mysql地址是:http://dev.mysql.com/downloads/mysql/解压缩
- 线性回归在整个财务中广泛应用于众多应用程序中。在之前的教程中,我们使用普通最小二乘法(OLS)计算了公司的beta与相对索引的比较。现在,我
- 本文实例讲述了C# Ado.net读取SQLServer数据库存储过程列表及参数信息的方法。分享给大家供大家参考,具体如下:得到数据库存储过
- 1. 事务介绍MVCC之前,先介绍下事务:事务是为了保证数据库中数据的完整性和一致性。事务的4个基本要素:原子性(Atomicity):要么
- 一、安装依赖包pip install --index https://pypi.mirrors.ustc.edu.cn/simple/ py
- 本文实例为大家分享了python实现ANN的具体代码,供大家参考,具体内容如下1.简要介绍神经网络神经网络是具有适应性的简单单元组成的广泛并
- Adobe AIR 在我们国庆节的时候发布了beta2版。 同时发布的还有: FLEX 3.0 beta2 官方说这一版本已经基本上接近正式
- 以XML格式查看查询结果通过使用传统—xml 选项调用MySQL命令行客户程序,你可以以XML格式(而不是传统的列表形式
- 在学习Python3的正则表达式的时候遇到一个例子#!/usr/bin/python3import reline = "Cats
- 因为外贸网站,禁止同行抄袭,所以防止中国ip访问访问,访问的时候有密码提示,这样的代码如何写.请给一个提示.或者有好的代码,请分享下。 &n
- 数据结构和算法算法:解决问题的方法和步骤评价算法的好坏:渐近时间复杂度和渐近空间复杂度。渐近时间复杂度的大O标记:- 常量时间复杂度 - 布
- Firefox 的 Jetpack 可以让我们很轻松地创建 Firefox 插件,仅通过已掌握的前端技能(HTML/CSS/JS),估计让人
- 如下所示,代码为:array也可直接使用上面代码。测试如下:来源:https://blog.csdn.net/u011624019/arti
- vue2.0里,不再有自带的过滤器,需要自己定义过滤器。定义的方法如下: 注册一个自定义过滤器,它接收两个参数:过滤器 ID 和过滤器函数。
- 工作时同事间几mb小文件的传输,一般使用QQ或者微信就足够了,但当传输文件几百MB或者几十G时,这种方法的效率就显得不足了。本篇就是简单说明
- 如果你会查询这些相关的问题,说明你是一个正在或者准备从事IT的程序猿,对于一个程序猿而言,不会使用linux系统的程序猿不是一好的程序猿哦!