Python实现Matplotlib,Seaborn动态数据图


Posted in Python onMay 06, 2022

Matplotlib

效果图如下

Python实现Matplotlib,Seaborn动态数据图

主要使用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

效果图如下

Python实现Matplotlib,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代码类似,不过多解释。

到此这篇关于Python实现Matplotlib,Seaborn动态数据图的文章就介绍到这了!


Tags in this post...

Python 相关文章推荐
Djang中静态文件配置方法
Jul 30 Python
详解Python3中的Sequence type的使用
Aug 01 Python
Python3实现的爬虫爬取数据并存入mysql数据库操作示例
Jun 06 Python
Tensorflow 查看变量的值方法
Jun 14 Python
Python使用ctypes调用C/C++的方法
Jan 29 Python
解决pyecharts在jupyter notebook中使用报错问题
Apr 23 Python
基于Python的图像数据增强Data Augmentation解析
Aug 13 Python
使用OpCode绕过Python沙箱的方法详解
Sep 03 Python
对tensorflow中tf.nn.conv1d和layers.conv1d的区别详解
Feb 11 Python
浅析Django 接收所有文件,前端展示文件(包括视频,文件,图片)ajax请求
Mar 09 Python
解决Django transaction进行事务管理踩过的坑
Apr 24 Python
Python使用psutil库对系统数据进行采集监控的方法
Aug 23 Python
PYTHON InceptionV3模型的复现详解
代码复现python目标检测yolo3详解预测
讲解Python实例练习逆序输出字符串
May 06 #Python
python turtle绘图
May 04 #Python
python blinker 信号库
May 04 #Python
python三子棋游戏
May 04 #Python
python神经网络 使用Keras构建RNN训练
May 04 #Python
You might like
WINXP下apache+php4+mysql
2006/11/25 PHP
php源码加密 仿微盾PHP加密专家(PHPCodeLock)
2010/05/06 PHP
php 面向对象的一个例子
2011/04/12 PHP
ThinkPHP中I(),U(),$this->post()等函数用法
2014/11/22 PHP
PHP多进程之pcntl_fork的实例详解
2017/10/15 PHP
Laravel Validator自定义错误返回提示消息并在前端展示
2019/05/09 PHP
thinkPHP和onethink微信支付插件分享
2019/08/11 PHP
讲两件事:1.this指针的用法小探. 2.ie的attachEvent和firefox的addEventListener在事件处理上的区别
2007/04/12 Javascript
JavaScript 数组循环引起的思考
2010/01/01 Javascript
javascript函数以及基础写法100多条实用整理
2013/01/13 Javascript
js setTimeout opener的用法示例详解
2013/10/23 Javascript
jquery引用方法时传递参数原理分析
2014/10/13 Javascript
javascript使用call调用微信API
2014/12/15 Javascript
JavaScript Math.ceil 方法(对数值向上取整)
2015/01/09 Javascript
jquery编写Tab选项卡滚动导航切换特效
2020/07/17 Javascript
JavaScript中点击事件的写法
2016/06/28 Javascript
JS 全屏和退出全屏详解及实例代码
2016/11/07 Javascript
node.js学习之交互式解释器REPL详解
2016/12/08 Javascript
Angular7中创建组件/自定义指令/管道的方法实例详解
2019/04/02 Javascript
JS实现使用POST方式发送请求
2019/08/30 Javascript
微信小程序wxs实现吸顶效果
2020/01/08 Javascript
微信小程序实现身份证取景框拍摄
2020/09/09 Javascript
[03:24]2014DOTA2国际邀请赛 神秘商店生意火爆
2014/07/18 DOTA
pandas 根据列的值选取所有行的示例
2018/11/07 Python
python-视频分帧&多帧合成视频实例
2019/12/10 Python
基于Python爬取51cto博客页面信息过程解析
2020/08/25 Python
Python爬取网页信息的示例
2020/09/24 Python
css3 条纹化和透明化表格Firefox下测试成功
2014/04/15 HTML / CSS
如何写自我鉴定
2014/03/19 职场文书
2015年学生管理工作总结
2015/05/26 职场文书
人代会简报
2015/07/21 职场文书
职工食堂管理制度
2015/08/06 职场文书
教师节主题班会方案
2015/08/17 职场文书
会计继续教育培训心得体会
2016/01/19 职场文书
mysql的MVCC多版本并发控制的实现
2021/04/14 MySQL
Angular性能优化之第三方组件和懒加载技术
2021/05/10 Javascript