Python中用Matplotlib制作动态数据图的代码是什么
Admin 2022-09-21 群英技术资讯 782 次浏览
这篇主要是介绍“Python中用Matplotlib制作动态数据图的代码是什么”的内容了,下文有实例供大家参考,对大家了解操作过程或相关知识有一定的帮助,而且实用性强,希望这篇文章能帮助大家解决Python中用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())
在绘图数据部分改自己的数据既可为所欲为的使用了~
效果图如下

代码
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代码类似,不过多解释。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了TensorFlow2如何实现前向传播,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章主要为大家介绍了Python作用域与名字空间的源码学习笔记,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪<BR>
什么是运算符重载,就是让自定义的类生成的对象(实例)能够使用运算符进行操作,这篇文章主要给大家介绍了关于Python运算符重载的相关资料,需要的朋友可以参考下
这篇文章主要介绍了Python内置函数详细解析,Python 自带了很多的内置函数,极大地方便了我们的开发,下文小编总结了一些内置函数的相关内容,需要的小伙伴可以参考一下
我们之前介绍了怎样用python写一个扑克牌21点小游戏,这次我们来看看用python实现植物大战僵尸小游戏,对于植物大战僵尸相比不少朋友都有玩过,对植物大战僵尸是如何写出来感兴趣的朋友就继续往下看。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008