Python實現(xiàn)Matplotlib,Seaborn動態(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)圖動起來 animator = animation.FuncAnimation(fig, draw_barchart, frames=range(1968, 2019)) # Jupyter Notebook里展示動圖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實現(xiàn)Matplotlib,Seaborn動態(tài)數(shù)據(jù)圖的示例代碼的文章就介紹到這了,更多相關(guān)Python動態(tài)數(shù)據(jù)圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python使用IPython調(diào)試debug程序
這篇文章主要為大家介紹了python使用IPython調(diào)試debug程序詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05Python中文分詞庫jieba,pkusegwg性能準(zhǔn)確度比較
這篇文章主要介紹了Python中文分詞庫jieba,pkusegwg性能準(zhǔn)確度比較,需要的朋友可以參考下2020-02-02asyncio 的 coroutine對象 與 Future對象使用指南
asyncio是Python 3.4版本引入的標(biāo)準(zhǔn)庫,直接內(nèi)置了對異步IO的支持。asyncio的編程模型就是一個消息循環(huán)。今天我們就來詳細(xì)討論下asyncio 中的 coroutine 與 Future對象2016-09-09pytorch 實現(xiàn)cross entropy損失函數(shù)計算方式
今天小編就為大家分享一篇pytorch 實現(xiàn)cross entropy損失函數(shù)計算方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01python中 chr unichr ord函數(shù)的實例詳解
這篇文章主要介紹了python中 chr unichr ord函數(shù)的實例詳解的相關(guān)資料,需要的朋友可以參考下2017-08-08selenium查找網(wǎng)頁出現(xiàn)加載卡頓或失敗的解決方法
這篇文章主要為大家詳細(xì)介紹了selenium查找網(wǎng)頁時如何處理網(wǎng)站資源一直加載非??D或者失敗的情況,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-10-10