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 相关文章推荐
python中pycurl库的用法实例
Sep 30 Python
将Python代码嵌入C++程序进行编写的实例
Jul 31 Python
python中私有函数调用方法解密
Apr 29 Python
Python命令启动Web服务器实例详解
Feb 23 Python
Python编程之gui程序实现简单文件浏览器代码
Dec 08 Python
对Python 语音识别框架详解
Dec 24 Python
在python中对变量判断是否为None的三种方法总结
Jan 23 Python
浅谈Python批处理文件夹中的txt文件
Mar 11 Python
Python 等分切分数据及规则命名的实例代码
Aug 16 Python
在OpenCV里使用Camshift算法的实现
Nov 22 Python
python自定义函数def的应用详解
Jun 03 Python
Python字符串三种格式化输出
Sep 17 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
jq的get传参数在utf-8中乱码问题的解决php版
2008/07/23 PHP
php中根据某年第几天计算出日期年月日的代码
2011/02/24 PHP
PHP中绘制图像的一些函数总结
2014/11/19 PHP
PHP查询快递信息的方法
2015/03/07 PHP
PHP实现批量修改文件后缀名的方法
2015/07/30 PHP
PHP文件及文件夹操作之创建、删除、移动、复制
2016/07/13 PHP
jQuery 使用手册(七)
2009/09/23 Javascript
jquery1.83 之前所有与异步列队相关的模块详细介绍
2012/11/13 Javascript
JS通过相同的name进行表格求和代码
2013/08/18 Javascript
js/jquery判断浏览器的方法小结
2014/09/02 Javascript
自定义刻度jQuery进度条及插件
2015/09/02 Javascript
JS右下角广告窗口代码(可收缩、展开及关闭)
2015/09/04 Javascript
利用JavaScript阻止表单提交的两种方法
2016/08/11 Javascript
Node.JS利用PhantomJs抓取网页入门教程
2017/05/19 Javascript
Vue 菜单栏点击切换单个class(高亮)的方法
2018/08/22 Javascript
详解Vue-cli3 项目在安卓低版本系统和IE上白屏问题解决
2019/04/14 Javascript
vue中的inject学习教程
2019/04/24 Javascript
JS实现从对象获取对象中单个键值的方法示例
2019/06/05 Javascript
Javascript实现简易天数计算器
2020/05/18 Javascript
Python实现查看系统启动项功能示例
2018/05/10 Python
pandas 小数位数 精度的处理方法
2018/06/09 Python
python实现简单flappy bird
2018/12/24 Python
Python基本语法之运算符功能与用法详解
2019/10/22 Python
解决python调用自己文件函数/执行函数找不到包问题
2020/06/01 Python
使用Keras 实现查看model weights .h5 文件的内容
2020/06/09 Python
python抢购软件/插件/脚本附完整源码
2021/03/04 Python
南威尔士家居商店:Leekes
2016/10/25 全球购物
庆祝教师节活动方案
2014/01/31 职场文书
工作证明范本(2篇)
2014/09/14 职场文书
党的群众路线教育实践活动个人对照检查材料(企业)
2014/11/05 职场文书
2015元旦晚会主持人开场白+结束语
2014/12/14 职场文书
撤诉书怎么写
2015/05/19 职场文书
法制教育观后感
2015/06/17 职场文书
《普罗米修斯》教学反思
2016/02/22 职场文书
职场领导同事生日简短祝福语
2019/08/06 职场文书
Pytorch 如何实现常用正则化
2021/05/27 Python