欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python使用matplotlib創(chuàng)建Gif動(dòng)圖的思路

 更新時(shí)間:2022年04月26日 09:54:49   作者:bashendixie5  
這篇文章主要介紹了Python使用matplotlib創(chuàng)建Gif動(dòng)圖,我們將討論matplotlib提供的名為“Animation”的動(dòng)畫(huà)庫(kù)之一,Python二維繪圖庫(kù)是Matplolib可以輕松創(chuàng)建繪圖、直方圖、條形圖、散點(diǎn)圖等,需要的朋友可以參考下

1、Matplotlib 簡(jiǎn)介

數(shù)據(jù)可視化有助于更有效地講述有關(guān)數(shù)據(jù)的故事并使其易于呈現(xiàn)。有時(shí)很難用靜態(tài)圖表來(lái)解釋數(shù)據(jù)的變化,為此,我們將討論matplotlib提供的名為“Animation”的動(dòng)畫(huà)庫(kù)之一。以下是要涵蓋的主題。

最流行的Python二維繪圖庫(kù)是Matplolib。大多數(shù)人從Matplotlib開(kāi)始他們的探索性數(shù)據(jù)分析之旅。它可以輕松創(chuàng)建繪圖、直方圖、條形圖、散點(diǎn)圖等。與Pandas和Seaborn一樣,它可以創(chuàng)建更復(fù)雜的視覺(jué)效果。

但是也有一些缺陷:

Matplotlib的命令式 API,通常過(guò)于冗長(zhǎng)。

有時(shí)糟糕的風(fēng)格默認(rèn)值。

對(duì)網(wǎng)絡(luò)和交互式圖表的支持不佳。

對(duì)于大型和復(fù)雜的數(shù)據(jù)通常很慢。

2、繪制動(dòng)畫(huà)正弦和余弦波

參考代碼如下

import matplotlib.animation as anime
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
 
fig = plt.figure()
l, = plt.plot([], [], 'k-')
l2, = plt.plot([], [], 'm--')
p1, = plt.plot([], [], 'ko')
p2, = plt.plot([], [], 'mo')
plt.xlabel('xlabel')
plt.ylabel('ylabel')
plt.title('title')
plt.xlim(-5, 5)
plt.ylim(-5, 5)
def func(x):
    return np.sin(x) * 3
def func2(x):
    return np.cos(x) * 3
metadata = dict(title="Movie", artist="sourabh")
writer = anime.PillowWriter(fps=15, metadata=metadata)
xlist = []
ylist = []
ylist2 = []
xlist2 = []
with writer.saving(fig, "sin+cosinewave.gif", 100):
    for xval in np.linspace(-5, 5, 100):
        xlist.append(xval)
        ylist.append(func(xval))
        l.set_data(xlist, ylist)
        l2.set_data(xlist2, ylist2)
        p1.set_data(xval, func(xval))
        writer.grab_frame()
        xlist2.append(xval)
        ylist2.append(func2(xval))
        p2.set_data(xval, func2(xval))

動(dòng)畫(huà)效果圖如下。

3、繪制曲面圖

參考代碼如下,這段代碼會(huì)運(yùn)行一段時(shí)間。

import matplotlib
from matplotlib import cm
import matplotlib.animation as anime
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed(29680801)
fig, ax = plt.subplots(subplot_kw=dict(projection='3d'))
plt.xlim(-5, 5)
plt.ylim(-5, 5)
metadata = dict(title="Movie", artist="sourabh")
writer = anime.PillowWriter(fps=15, metadata=metadata)
def func(x, y, r, t):
    return np.cos(r / 2 + t) * np.exp(-np.square(r) / 50)
xdata = np.linspace(-10, 10, 1000)
ydata = np.linspace(-10, 10, 1000)
x_list, y_list = np.meshgrid(xdata, ydata)
r_list = np.sqrt(np.square(x_list) + np.square(y_list))
with writer.saving(fig, "exp3d.gif", 100):
    for t in np.linspace(0, 20, 160):
        z = func(x_list, y_list, r_list, t)
        ax.set_zlim(-1, 1)
        ax.plot_surface(x_list, y_list, z, cmap=cm.viridis)
        writer.grab_frame()
        plt.cla()

動(dòng)畫(huà)效果如下 

4、繪制回歸圖

參考代碼如下

import matplotlib
from matplotlib import cm
import matplotlib.animation as anime
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
np.random.seed(23680545)
metadata = dict(title="Movie", artist="sourabh")
writer = anime.PillowWriter(fps=15, metadata=metadata)
fig = plt.figure()
plt.xlim(-8, 8)
plt.ylim(-8, 8)
def func(x):
    return x * 1.2 + 0.1 + np.random.normal(0, 2, x.shape)
x = np.random.uniform(-7, 7, 10)
x = np.sort(x)
y = func(x)
coeff = np.polyfit(x, y, 1)
print(coeff)
xline = np.linspace(-6, 6, 40)
yline = np.polyval(coeff, xline)
lPnt, = plt.plot(x, y, 'o')
l, = plt.plot(xline, yline, 'k-', linewidth=3)
plt.show()
fig = plt.figure()
plt.xlim(-10, 10)
plt.ylim(-10, 10)
lPnt, = plt.plot([], [], 'o')
l, = plt.plot([], [], 'k-', linewidth=3)
x_List = []
y_List = []
x_pnt = []
y_pnt = []
with writer.saving(fig, "fitPlot.gif", 100):
    for xval, yval in zip(x, y):
        x_pnt.append(xval)
        y_pnt.append(yval)
        lPnt.set_data(x_pnt, y_pnt)
        l.set_data(x_List, y_List)
        writer.grab_frame()
        writer.grab_frame()
    for x_val, y_val in zip(xline, xline):
        x_List.append(x_val)
        y_List.append(y_val)
        lPnt.set_data(x_pnt, y_pnt)
        l.set_data(x_List, y_List)
        writer.grab_frame()
    for i in range(10):
        writer.grab_frame()

效果圖如下

到此這篇關(guān)于Python使用matplotlib創(chuàng)建Gif動(dòng)圖的文章就介紹到這了,更多相關(guān)Python創(chuàng)建Gif動(dòng)圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Python 實(shí)現(xiàn) ZeroMQ 的三種基本工作模式

    詳解Python 實(shí)現(xiàn) ZeroMQ 的三種基本工作模式

    ZMQ是一個(gè)簡(jiǎn)單好用的傳輸層,像框架一樣的一個(gè) socket library,他使得 Socket 編程更加簡(jiǎn)單、簡(jiǎn)潔和性能更高。 ,這篇文章主要介紹了Python 實(shí)現(xiàn) ZeroMQ 的三種基本工作模式,需要的朋友可以參考下
    2020-03-03
  • 兩種方法檢查Python中的變量是否為字符串

    兩種方法檢查Python中的變量是否為字符串

    在 Python 中,每個(gè)變量都有一個(gè)數(shù)據(jù)類型, 數(shù)據(jù)類型表示變量?jī)?nèi)部存儲(chǔ)的數(shù)據(jù)類型,本文通過(guò)示例介紹兩種不同的方法來(lái)檢查 Python 中的變量是否為字符串,感興趣的朋友一起看看吧
    2023-11-11
  • python實(shí)現(xiàn)二分類和多分類的ROC曲線教程

    python實(shí)現(xiàn)二分類和多分類的ROC曲線教程

    這篇文章主要介紹了python實(shí)現(xiàn)二分類和多分類的ROC曲線教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • Python之tkinter面板PanedWindow的使用

    Python之tkinter面板PanedWindow的使用

    這篇文章主要介紹了Python之tkinter面板PanedWindow的使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • python解析xml文件實(shí)例分析

    python解析xml文件實(shí)例分析

    這篇文章主要介紹了python解析xml文件的方法,實(shí)例分析了Python針對(duì)XML文件節(jié)點(diǎn)及字段的獲取技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2015-05-05
  • 在python中按照特定順序訪問(wèn)字典的方法詳解

    在python中按照特定順序訪問(wèn)字典的方法詳解

    今天小編就為大家分享一篇在python中按照特定順序訪問(wèn)字典的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • 使用Python+Flask開(kāi)發(fā)博客項(xiàng)目并實(shí)現(xiàn)內(nèi)網(wǎng)穿透

    使用Python+Flask開(kāi)發(fā)博客項(xiàng)目并實(shí)現(xiàn)內(nèi)網(wǎng)穿透

    Flask是一個(gè)使用python編寫(xiě)的輕量級(jí)Web框架,這篇文章我們將使用這個(gè)框架編寫(xiě)一個(gè)屬于自己的博客網(wǎng)站!并教你如何通過(guò)使用內(nèi)網(wǎng)穿透工具處理項(xiàng)目,讓本地的項(xiàng)目可以在公網(wǎng)訪問(wèn),感興趣的可以了解一下
    2021-11-11
  • python實(shí)現(xiàn)對(duì)doc,txt,xls文檔的讀寫(xiě)操作

    python實(shí)現(xiàn)對(duì)doc,txt,xls文檔的讀寫(xiě)操作

    這篇文章主要介紹了python實(shí)現(xiàn)對(duì)doc,txt,xls文檔的讀寫(xiě)操作,正如標(biāo)題所見(jiàn),文章包括三個(gè)部分python實(shí)現(xiàn)對(duì)doc文檔的讀取、python實(shí)現(xiàn)對(duì)txt文檔的讀取和python實(shí)現(xiàn)對(duì)xls表格的讀取,需要的朋友可以參考一下
    2022-04-04
  • Python3實(shí)現(xiàn)打印任意寬度的菱形代碼

    Python3實(shí)現(xiàn)打印任意寬度的菱形代碼

    這篇文章主要介紹了Python3實(shí)現(xiàn)打印任意寬度的菱形代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • GPU排隊(duì)腳本實(shí)現(xiàn)空閑觸發(fā)python腳本實(shí)現(xiàn)示例

    GPU排隊(duì)腳本實(shí)現(xiàn)空閑觸發(fā)python腳本實(shí)現(xiàn)示例

    有的服務(wù)器是多用戶使用,GPU的資源常常被占據(jù)著,很可能在夜間GPU空閑了,但來(lái)不及運(yùn)行自己的腳本。如果沒(méi)有和別人共享服務(wù)器的話,自己的多個(gè)程序想排隊(duì)使用GPU,也可以用這個(gè)腳本
    2021-11-11

最新評(píng)論