Python實(shí)現(xiàn)Matplotlib,Seaborn動(dòng)態(tài)數(shù)據(jù)圖的示例代碼
Matplotlib
效果圖如下

主要使用matplotlib.animation.FuncAnimation,上核心代碼,
# 定義靜態(tài)繪圖函數(shù)
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)
# 調(diào)用matplotlib.animation.FuncAnimation讓靜態(tài)圖動(dòng)起來
animator = animation.FuncAnimation(fig,
draw_barchart,
frames=range(1968, 2019))
# Jupyter Notebook里展示動(dòng)圖animation
HTML(animator.to_jshtml())在繪圖數(shù)據(jù)部分改自己的數(shù)據(jù)既可為所欲為的使用了~
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代碼類似,不過多解釋。
到此這篇關(guān)于Python實(shí)現(xiàn)Matplotlib,Seaborn動(dòng)態(tài)數(shù)據(jù)圖的示例代碼的文章就介紹到這了,更多相關(guān)Python動(dòng)態(tài)數(shù)據(jù)圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python數(shù)據(jù)可視化完全指南之Matplotlib+Seaborn+Plotly用法詳解
- Python數(shù)據(jù)可視化庫:Matplotlib、Seaborn、Plotly、Bokeh等對(duì)比與選擇
- python安裝?Matplotlib?庫和Seaborn?庫的示例詳解
- Python數(shù)據(jù)可視化之Pandas、Matplotlib與Seaborn的高效實(shí)戰(zhàn)指南
- Python繪圖工具使用Matplotlib、Seaborn和Pyecharts繪制散點(diǎn)圖詳解
- Python使用Matplotlib和Seaborn繪制常用圖表的技巧
- Python數(shù)據(jù)可視化之Matplotlib和Seaborn的使用教程詳解
- Python?matplotlib?seaborn繪圖教程詳解
- python可視化分析的實(shí)現(xiàn)(matplotlib、seaborn、ggplot2)
- Python AI基礎(chǔ):Matplotlib和Seaborn兩大可視化庫的原理和使用(實(shí)踐代碼)
相關(guān)文章
Python數(shù)據(jù)類型之String字符串實(shí)例詳解
這篇文章主要介紹了Python數(shù)據(jù)類型之String字符串,結(jié)合實(shí)例形式詳細(xì)講解了Python字符串的概念、定義、連接、格式化、轉(zhuǎn)換、查找、截取、判斷等常見操作技巧,需要的朋友可以參考下2019-05-05
Pandas:Series和DataFrame刪除指定軸上數(shù)據(jù)的方法
今天小編就為大家分享一篇Pandas:Series和DataFrame刪除指定軸上數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11
Pandas分組聚合之groupby()、agg()方法的使用教程
今天看到pandas的聚合函數(shù)agg,比較陌生,平時(shí)的工作中處理數(shù)據(jù)的時(shí)候使用的也比較少,為了加深印象,總結(jié)一下使用的方法,下面這篇文章主要給大家介紹了關(guān)于Pandas分組聚合之groupby()、agg()方法的使用教程,需要的朋友可以參考下2023-01-01
Python實(shí)現(xiàn)視頻轉(zhuǎn)圖片的兩種方案
文章介紹了兩種Python視頻轉(zhuǎn)圖片的方案,方案1按幀提取,方案2按時(shí)間間隔提取,核心功能包括關(guān)鍵函數(shù)與參數(shù)的說明、實(shí)用優(yōu)化點(diǎn)和使用注意事項(xiàng),標(biāo)注工具推薦包括LabelImg、LabelMe和CVAT,需要的朋友可以參考下2026-02-02
python正則表達(dá)式匹配[]中間為任意字符的實(shí)例
今天小編就為大家分享一篇python正則表達(dá)式匹配[]中間為任意字符的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12

