最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python matplotlib繪制實時數(shù)據(jù)動畫

 更新時間:2022年03月09日 09:02:39   作者:渴望成為寂寞勝者  
Matplotlib作為Python的2D繪圖庫,它以各種硬拷貝格式和跨平臺的交互式環(huán)境生成出版質(zhì)量級別的圖形。本文將利用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)文章

最新評論

阳新县| 白山市| 湘阴县| 昂仁县| 天柱县| 泽库县| 濉溪县| 鸡西市| 肇庆市| 苏州市| 姚安县| 丽水市| 交城县| 临江市| 红安县| 定边县| 松原市| 乌鲁木齐县| 卫辉市| 定陶县| 滨海县| 淮阳县| 托克托县| 闽侯县| 任丘市| 施甸县| 古田县| 米泉市| 凤山县| 鹿邑县| 诸暨市| 荥经县| 宁津县| 辽宁省| 厦门市| 昌平区| 五大连池市| 卢龙县| 林州市| 三江| 衡水市|