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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)連續(xù)圖文識(shí)別
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)連續(xù)圖文識(shí)別功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
pytorch加載自己的圖片數(shù)據(jù)集的2種方法詳解
數(shù)據(jù)預(yù)處理在解決深度學(xué)習(xí)問題的過程中,往往需要花費(fèi)大量的時(shí)間和精力,下面這篇文章主要給大家介紹了關(guān)于pytorch加載自己的圖片數(shù)據(jù)集的2種方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
Python?實(shí)操顯示數(shù)據(jù)圖表并固定時(shí)間長(zhǎng)度
這篇文章主要介紹了Python?實(shí)操顯示數(shù)據(jù)圖表并固定時(shí)間長(zhǎng)度,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
Pandas中datetime數(shù)據(jù)類型的使用
本文主要介紹了Pandas中datetime數(shù)據(jù)類型的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
Python 生成 -1~1 之間的隨機(jī)數(shù)矩陣方法
今天小編就為大家分享一篇Python 生成 -1~1 之間的隨機(jī)數(shù)矩陣方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
python中pivot()函數(shù)基礎(chǔ)知識(shí)點(diǎn)
在本篇內(nèi)容里小編給大家分享的是一篇關(guān)于python中pivot()函數(shù)基礎(chǔ)知識(shí)點(diǎn)內(nèi)容,對(duì)此有興趣的朋友們可以參考學(xué)習(xí)下。2021-01-01
基于python分享一款地理數(shù)據(jù)可視化神器keplergl
這篇文章主要介紹了分享一款地理數(shù)據(jù)可視化神器keplergl,keplergl是由Uber開源的一款地理數(shù)據(jù)可視化工具,通過keplergl我們可以在Jupyter?notebook中使用,下文分享需要的小伙伴可以參考一下2022-02-02

