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的While循环语句的使用方法
Oct 13 Python
python Django框架实现自定义表单提交
Mar 25 Python
python简单读取大文件的方法
Jul 01 Python
python中的二维列表实例详解
Jun 19 Python
解决tensorflow1.x版本加载saver.restore目录报错的问题
Jul 26 Python
Django中日期处理注意事项与自定义时间格式转换详解
Aug 06 Python
解决python中os.listdir()函数读取文件夹下文件的乱序和排序问题
Oct 17 Python
Linux下安装python3.6和第三方库的教程详解
Nov 09 Python
python mac下安装虚拟环境的图文教程
Apr 12 Python
树莓派3 搭建 django 服务器的实例
Aug 29 Python
python GUI库图形界面开发之PyQt5表单布局控件QFormLayout详细使用方法与实例
Mar 06 Python
彻底搞懂python 迭代器和生成器
Sep 07 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
PHP定时任务延缓执行的实现
2014/10/08 PHP
PHP编程中的__clone()方法使用详解
2015/11/27 PHP
PHP封装函数实现生成随机的字符串验证码
2017/01/24 PHP
如何在centos8自定义目录安装php7.3
2019/11/28 PHP
php7 图形用户界面GUI 开发示例
2020/02/22 PHP
Google Suggest ;-) 基于js的动态下拉菜单
2006/10/11 Javascript
对YUI扩展的Gird组件 Part-2
2007/03/10 Javascript
用JavaScript实现UrlEncode和UrlDecode的脚本代码
2008/07/23 Javascript
jquery实现可拖动DIV自定义保存到数据的实例
2013/11/20 Javascript
PhotoShop给图片自动添加边框及EXIF信息的JS脚本
2015/02/15 Javascript
使用AngularJS处理单选框和复选框的简单方法
2015/06/19 Javascript
jquery控制显示服务器生成的图片流
2015/08/04 Javascript
jQuery插件HighCharts绘制简单2D柱状图效果示例【附demo源码】
2017/03/21 jQuery
vue 弹框产生的滚动穿透问题的解决
2018/09/21 Javascript
node实现生成带参数的小程序二维码并保存到本地功能示例
2018/12/05 Javascript
JS+canvas画布实现炫酷的旋转星空效果示例
2019/02/13 Javascript
基于elementUI实现图片预览组件的示例代码
2019/03/31 Javascript
微信小程序HTTP接口请求封装代码实例
2019/09/05 Javascript
Vue+ElementUI使用vue-pdf实现预览功能
2019/11/26 Javascript
javascript设计模式之装饰者模式
2020/01/30 Javascript
Vue 的双向绑定原理与用法揭秘
2020/05/06 Javascript
openlayers 3实现车辆轨迹回放
2020/09/24 Javascript
Python中二维列表如何获取子区域元素的组成
2017/01/19 Python
Django框架实现逆向解析url的方法
2018/07/04 Python
由面试题加深对Django的认识理解
2019/07/19 Python
python3实现微型的web服务器
2019/09/03 Python
Chupi官网:在爱尔兰手工制作的订婚、结婚戒指和精美珠宝
2020/09/28 全球购物
校园网站的创业计划书范文
2013/12/30 职场文书
50岁生日感言
2014/01/23 职场文书
学生喝酒检讨书
2014/02/06 职场文书
专业技术职务聘任书
2014/03/29 职场文书
勤俭节约演讲稿
2014/05/08 职场文书
三好生演讲稿
2014/09/12 职场文书
2014年公务员个人工作总结
2014/11/22 职场文书
人工作失职检讨书
2015/05/05 职场文书
使用Springboot实现健身房管理系统
2021/07/01 Java/Android