Python matplotlib繪制實(shí)時(shí)數(shù)據(jù)動(dòng)畫
一、實(shí)時(shí)數(shù)據(jù)可視化的數(shù)據(jù)準(zhǔn)備
import pandas as pd import matplotlib.pyplot as plt
# 設(shè)置一般的樣例數(shù)據(jù) x=[0,1,2,3,4] # x軸數(shù)據(jù) y=[0,1,2,3,4] # y軸數(shù)據(jù) # 設(shè)置多維數(shù)據(jù) dev_x=[25,26,27,28,29,30] # 開發(fā)者的年齡 dev_y=[7567,8789,8900,11560,16789,25231] #收入情況 py_dev_y=[5567,6789,9098,15560,20789,23231] # python開發(fā)者 js_dev_y=[6567,7789,8098,12356,14789,20231] # java開發(fā)者 devsalary=pd.DataFrame([dev_x,dev_y,py_dev_y,js_dev_y])
01.設(shè)置圖表主題樣式
之前用的都是經(jīng)典樣式:
plt.style.use('classic') plt.plot(x,y)
現(xiàn)在換成538樣式:
plt.style.use('fivethirtyeight') # 538統(tǒng)計(jì)樣式 from IPython.display import HTML # 在實(shí)現(xiàn)動(dòng)態(tài)的過程中必須引入的庫 plt.plot(x,y)
02 使用樣例數(shù)據(jù)
import random from itertools import count
index=count() x1=[] y1=[]
x1.append(next(index)) y1.append(random.randint(0,50)) plt.plot(x1,y1)
先來試試手動(dòng)的效果:
該效果即我們要實(shí)現(xiàn)的動(dòng)畫。
def animate(i): x1.append(next(index)) y1.append(random.randint(0,50)) plt.plot(x1,y1)
from matplotlib.animation import FuncAnimation ani=FuncAnimation(plt.gcf(),animate,interval=1000) # interval=1000代表時(shí)間間隔,數(shù)值越小,則時(shí)間間隔越短 HTML(ani.to_jshtml())
上面的視頻是演示數(shù)據(jù)的生成過程,會(huì)發(fā)現(xiàn)每次變化的時(shí)候顏色都會(huì)變化。
在視頻底下還有一張完整的圖片,表示在時(shí)間節(jié)點(diǎn)之中Python生成的序列數(shù):
如果想要每次變化的時(shí)候圖像都在原有基礎(chǔ)上變化,則使用如下:
plt.cla()
def animate(i): x1.append(next(index)) y1.append(random.randint(0,50)) plt.cla() #每次變化的時(shí)候都是在原有基礎(chǔ)上改變 plt.plot(x1,y1)
二、使用電影票房數(shù)據(jù)制作動(dòng)畫
動(dòng)態(tài)實(shí)時(shí)的數(shù)據(jù)往往和時(shí)間軸有關(guān)聯(lián),本次使用的數(shù)據(jù): cnboo1.xlsx
import pandas as pd cnbodf=pd.read_excel('cnboo1.xlsx') cnbodfsort=cnbodf.sort_values(by=['BO'],ascending=False)
def mkpoints(x,y): return len(str(x))*(y/25)-3 cnbodfsort['points']=cnbodfsort.apply(lambda x:mkpoints(x.BO,x.PERSONS),axis=1)
cnbodfgb=cnbodfsort.groupby("TYPE").mean(["bo","prices","persons","points"]) cnbodfsort['type1']=cnbodfsort['TYPE'].apply(lambda x:x.split("/")[0]) cnbodfgb=cnbodfsort.groupby(["type1"])["ID","BO","PRICE","PERSONS","points"].mean() cnbodfgbsort=cnbodfgb.sort_values("BO",ascending=False)
x=cnbodfsort['PERSONS'] y=cnbodfsort['PRICE'] plt.plot(x,y)
當(dāng)我們分別以人數(shù)和電影票價(jià)格作為x和y軸的數(shù)據(jù)是,可以看到數(shù)據(jù)是較為紊亂的:
而動(dòng)態(tài)實(shí)時(shí)的數(shù)據(jù)線往往是和時(shí)間有關(guān)聯(lián)的。
因此我們需要把數(shù)據(jù)進(jìn)行重新定義。
y1=y.to_list() X1=x.to_list()
def animate(i): x1.append(next(index)) y1.append(y[random.randint(1,49)]) # 表示在50條電影數(shù)據(jù)中隨機(jī)選擇一條 plt.cla() #每次變化的時(shí)候都是在原有基礎(chǔ)上改變 plt.plot(x1,y1)
x1=[] y1=[]
ani=FuncAnimation(plt.gcf(),animate,interval=1000) HTML(ani.to_jshtml())
最終呈現(xiàn)的效果如下:
到此這篇關(guān)于Python matplotlib繪制實(shí)時(shí)數(shù)據(jù)動(dòng)畫的文章就介紹到這了,更多相關(guān)Python matplotlib數(shù)據(jù)動(dòng)畫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Python實(shí)現(xiàn)操作控制鼠標(biāo)和鍵盤
Python 有很多的庫可以實(shí)現(xiàn)各種各樣的功能,比如使用 pynput 操作,下面小編就來和大家詳細(xì)介紹一下如何使用pynput進(jìn)行操作控制鼠標(biāo)和鍵盤吧2024-02-02python定時(shí)任務(wù)timeloop庫用法實(shí)例詳解
有些時(shí)候我們需要每隔一段時(shí)間就要執(zhí)行一段程序,或者是往復(fù)循環(huán)執(zhí)行某一個(gè)任務(wù),下面這篇文章主要給大家介紹了關(guān)于python定時(shí)任務(wù)timeloop庫用法的相關(guān)資料,需要的朋友可以參考下2023-01-01Python實(shí)現(xiàn)Mysql數(shù)據(jù)統(tǒng)計(jì)及numpy統(tǒng)計(jì)函數(shù)
這篇文章主要介紹了Python實(shí)現(xiàn)Mysql數(shù)據(jù)統(tǒng)計(jì)的實(shí)例代碼,給大家介紹了Python數(shù)據(jù)分析numpy統(tǒng)計(jì)函數(shù)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07Python中函數(shù)的創(chuàng)建與調(diào)用你了解嗎
這篇文章主要為大家詳細(xì)介紹了Python中函數(shù)的創(chuàng)建與調(diào)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03tensorflow基本操作小白快速構(gòu)建線性回歸和分類模型
這篇文章主要介紹了tensorflow基本操作,快速構(gòu)建線性回歸和分類模型,圖文代碼示例非常詳細(xì),有需要的朋友可以借鑒參考下,希望可以對(duì)大家有所幫助2021-08-08