網站首頁 編程語言 正文
一、實時數據可視化的數據準備
import pandas as pd import matplotlib.pyplot as plt
# 設置一般的樣例數據 x=[0,1,2,3,4] # x軸數據 y=[0,1,2,3,4] # y軸數據 # 設置多維數據 dev_x=[25,26,27,28,29,30] # 開發者的年齡 dev_y=[7567,8789,8900,11560,16789,25231] #收入情況 py_dev_y=[5567,6789,9098,15560,20789,23231] # python開發者 js_dev_y=[6567,7789,8098,12356,14789,20231] # java開發者 devsalary=pd.DataFrame([dev_x,dev_y,py_dev_y,js_dev_y])
01.設置圖表主題樣式
之前用的都是經典樣式:
plt.style.use('classic') plt.plot(x,y)
現在換成538樣式:
plt.style.use('fivethirtyeight') # 538統計樣式 from IPython.display import HTML # 在實現動態的過程中必須引入的庫 plt.plot(x,y)
02 使用樣例數據
import random from itertools import count
index=count() x1=[] y1=[]
x1.append(next(index)) y1.append(random.randint(0,50)) plt.plot(x1,y1)
先來試試手動的效果:
該效果即我們要實現的動畫。
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代表時間間隔,數值越小,則時間間隔越短 HTML(ani.to_jshtml())
上面的視頻是演示數據的生成過程,會發現每次變化的時候顏色都會變化。
在視頻底下還有一張完整的圖片,表示在時間節點之中Python生成的序列數:
如果想要每次變化的時候圖像都在原有基礎上變化,則使用如下:
plt.cla()
def animate(i): x1.append(next(index)) y1.append(random.randint(0,50)) plt.cla() #每次變化的時候都是在原有基礎上改變 plt.plot(x1,y1)
二、使用電影票房數據制作動畫
動態實時的數據往往和時間軸有關聯,本次使用的數據: 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)
當我們分別以人數和電影票價格作為x和y軸的數據是,可以看到數據是較為紊亂的:
而動態實時的數據線往往是和時間有關聯的。
因此我們需要把數據進行重新定義。
y1=y.to_list() X1=x.to_list()
def animate(i): x1.append(next(index)) y1.append(y[random.randint(1,49)]) # 表示在50條電影數據中隨機選擇一條 plt.cla() #每次變化的時候都是在原有基礎上改變 plt.plot(x1,y1)
x1=[] y1=[]
ani=FuncAnimation(plt.gcf(),animate,interval=1000) HTML(ani.to_jshtml())
最終呈現的效果如下:
原文鏈接:https://blog.csdn.net/wxfighting/article/details/123350659
相關推薦
- 2022-01-21 win10 如何做到 C盤 的絕對干凈,所有軟件都安裝到D盤,C盤只用來存操作系統。
- 2022-12-22 使用PyCharm調試程序實現過程_python
- 2022-10-10 python基礎知識之try...except...的詳細用法實例_python
- 2022-10-31 利用WinForm實現上左右布局的方法詳解_C#教程
- 2022-04-17 Unity實現簡單的多人聊天工具_C#教程
- 2022-04-20 Httprunner簡介、安裝及基本使用教程_python
- 2022-09-03 PyQt5實現tableWidget?居中顯示_python
- 2022-05-13 Android 10 讀寫文件權限
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支