Python matplotlib繪制實時數(shù)據(jù)動畫
一、實時數(shù)據(jù)可視化的數(shù)據(jù)準備
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)計樣式 from IPython.display import HTML # 在實現(xiàn)動態(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)
先來試試手動的效果:
該效果即我們要實現(xiàn)的動畫。
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ù)值越小,則時間間隔越短 HTML(ani.to_jshtml())
上面的視頻是演示數(shù)據(jù)的生成過程,會發(fā)現(xiàn)每次變化的時候顏色都會變化。
在視頻底下還有一張完整的圖片,表示在時間節(jié)點之中Python生成的序列數(shù):
如果想要每次變化的時候圖像都在原有基礎(chǔ)上變化,則使用如下:
plt.cla()
def animate(i): x1.append(next(index)) y1.append(random.randint(0,50)) plt.cla() #每次變化的時候都是在原有基礎(chǔ)上改變 plt.plot(x1,y1)
二、使用電影票房數(shù)據(jù)制作動畫
動態(tài)實時的數(shù)據(jù)往往和時間軸有關(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)
當我們分別以人數(shù)和電影票價格作為x和y軸的數(shù)據(jù)是,可以看到數(shù)據(jù)是較為紊亂的:
而動態(tài)實時的數(shù)據(jù)線往往是和時間有關(guān)聯(lián)的。
因此我們需要把數(shù)據(jù)進行重新定義。
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ù)中隨機選擇一條 plt.cla() #每次變化的時候都是在原有基礎(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ù)據(jù)動畫的文章就介紹到這了,更多相關(guān)Python matplotlib數(shù)據(jù)動畫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)Mysql數(shù)據(jù)統(tǒng)計及numpy統(tǒng)計函數(shù)
這篇文章主要介紹了Python實現(xiàn)Mysql數(shù)據(jù)統(tǒng)計的實例代碼,給大家介紹了Python數(shù)據(jù)分析numpy統(tǒng)計函數(shù)的相關(guān)知識,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07Python中函數(shù)的創(chuàng)建與調(diào)用你了解嗎
這篇文章主要為大家詳細介紹了Python中函數(shù)的創(chuàng)建與調(diào)用,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03tensorflow基本操作小白快速構(gòu)建線性回歸和分類模型
這篇文章主要介紹了tensorflow基本操作,快速構(gòu)建線性回歸和分類模型,圖文代碼示例非常詳細,有需要的朋友可以借鑒參考下,希望可以對大家有所幫助2021-08-08