網(wǎng)站首頁 編程語言 正文
一、安裝相關(guān)的模塊
首先第一步的話我們需要安裝相關(guān)的模塊,通過pip
命令來安裝
pip install gif
另外由于gif模塊之后會被當(dāng)做是裝飾器放在繪制可視化圖表的函數(shù)上,主要我們依賴的還是Python當(dāng)中繪制可視化圖表的matplotlib
、plotly
、以及altair
這些模塊,因此我們還需要下面這幾個庫
pip install "gif[altair]" ? ? pip install "gif[matplotlib]" pip install "gif[plotly]"
二、gif和matplotlib的結(jié)合
我們先來看gif
和matplotlib
模塊的結(jié)合,我們先來看一個簡單的例子,
代碼如下:
import random from matplotlib import pyplot as plt import gif x = [random.randint(0, 100) for _ in range(100)] y = [random.randint(0, 100) for _ in range(100)] gif.options.matplotlib["dpi"] = 300 @gif.frame def plot(i): ? ? xi = x[i*10:(i+1)*10] ? ? yi = y[i*10:(i+1)*10] ? ? plt.scatter(xi, yi) ? ? plt.xlim((0, 100)) ? ? plt.ylim((0, 100)) frames = [] for i in range(10): ? ? frame = plot(i) ? ? frames.append(frame) gif.save(frames, 'example.gif', duration=3.5, unit="s", between="startend")
output:
代碼的邏輯并不難理解,首先我們需要定義一個函數(shù)來繪制圖表并且?guī)?code>gif裝飾器,接著我們需要一個空的列表,通過for循環(huán)將繪制出來的對象放到這個空列表當(dāng)中然后保存成gif格式的文件即可。
三、gif和plotly的結(jié)合
除了和matplotlib
的聯(lián)用之外,gif
和plotly
之間也可以結(jié)合起來用
代碼如下:
import random import plotly.graph_objects as go import pandas as pd import gif df = pd.DataFrame({ ? ? 't': list(range(10)) * 10, ? ? 'x': [random.randint(0, 100) for _ in range(100)], ? ? 'y': [random.randint(0, 100) for _ in range(100)] }) @gif.frame def plot(i): ? ? d = df[df['t'] == i] ? ? fig = go.Figure() ? ? fig.add_trace(go.Scatter( ? ? ? ? x=d["x"], ? ? ? ? y=d["y"], ? ? ? ? mode="markers" ? ? )) ? ? fig.update_layout(width=500, height=300) ? ? return fig frames = [] for i in range(10): ? ? frame = plot(i) ? ? frames.append(frame) gif.save(frames, 'example_plotly.gif', duration=100)
output:
整體的代碼邏輯和上面的相似,這里也就不做具體的說明了
四、matplotlib多子圖動態(tài)可視化
上面繪制出來的圖表都是在單張圖表當(dāng)中進(jìn)行的,那當(dāng)然了我們還可以在多張子圖中進(jìn)行動態(tài)可視化的展示,
代碼如下:
# 讀取數(shù)據(jù) df = pd.read_csv('weather_hourly_darksky.csv') df = df.rename(columns={"time": "date"}) @gif.frame def plot(df, date): ? ? df = df.loc[df.index[0]:pd.Timestamp(date)] ? ? fig, (ax1, ax2, ax3) = plt.subplots(3, figsize=(10, 6), dpi=100) ? ? ax1.plot(df.temperature, marker='o', linestyle='--', linewidth=1, markersize=3, color='g') ? ? maxi = round(df.temperature.max() + 3) ? ? ax1.set_xlim([START, END]) ? ? ax1.set_ylim([0, maxi]) ? ? ax1.set_ylabel('TEMPERATURE', color='green') ? ? ax2.plot(df.windSpeed, marker='o', linestyle='--', linewidth=1, markersize=3, color='b') ? ? maxi = round(df.windSpeed.max() + 3) ? ? ax2.set_xlim([START, END]) ? ? ax2.set_ylim([0, maxi]) ? ? ax2.set_ylabel('WIND', color='blue') ? ? ax3.plot(df.visibility, marker='o', linestyle='--', linewidth=1, markersize=3, color='r') ? ? maxi = round(df.visibility.max() + 3) ? ? ax3.set_xlim([START, END]) ? ? ax3.set_ylim([0, maxi]) ? ? ax3.set_ylabel('VISIBILITY', color='red') frames = [] for date in pd.date_range(start=df.index[0], end=df.index[-1], freq='1M'): ? ? frame = plot(df, date) ? ? frames.append(frame) gif.save(frames, "文件名稱.gif", duration=0.5, unit='s')
output:
五、動態(tài)氣泡圖
最后我們用plotly
模塊來繪制一個動態(tài)的氣泡圖,
代碼如下:
import gif import plotly.graph_objects as go import numpy as np np.random.seed(1) N = 100 x = np.random.rand(N) y = np.random.rand(N) colors = np.random.rand(N) sz = np.random.rand(N) * 30 layout = go.Layout( ? ? xaxis={'range': [-2, 2]}, ? ? yaxis={'range': [-2, 2]}, ? ? margin=dict(l=10, r=10, t=10, b=10) ) @gif.frame def plot(i): ? ? fig = go.Figure(layout=layout) ? ? fig.add_trace(go.Scatter( ? ? ? ? x=x[:i], ? ? ? ? y=y[:i], ? ? ? ? mode="markers", ? ? ? ? marker=go.scatter.Marker( ? ? ? ? ? ? size=sz[:i], ? ? ? ? ? ? color=colors[:i], ? ? ? ? ? ? opacity=0.6, ? ? ? ? ? ? colorscale="Viridis" ? ? ? ? ) ? ? )) ? ? fig.update_layout(width=500, height=300) ? ? return fig frames = [] for i in range(100): ? ? frame = plot(i) ? ? frames.append(frame) gif.save(frames, "bubble.gif")
output:
原文鏈接:https://blog.csdn.net/qq_34160248/article/details/123052069
相關(guān)推薦
- 2022-04-04 Kotlin中協(xié)程的創(chuàng)建過程詳析_Android
- 2022-04-03 Docker?部署RocketMQ的詳細(xì)操作_docker
- 2023-01-12 利用C#實(shí)現(xiàn)進(jìn)程管理器_C#教程
- 2022-10-23 Linux服務(wù)器VPS的Windows?DD包詳細(xì)的制作教程_Linux
- 2023-01-23 Python列表對象中元素的刪除操作方法_python
- 2022-07-20 詳解Go程序添加遠(yuǎn)程調(diào)用tcpdump功能_Golang
- 2022-04-11 css實(shí)現(xiàn)左邊div固定寬度,右邊div自適應(yīng)撐滿剩下的寬度
- 2022-05-02 分布式利器redis及redisson的延遲隊(duì)列實(shí)踐_Redis
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支