亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

給 matplotlib plot / gif 添加背景圖片

給 matplotlib plot / gif 添加背景圖片

眼眸繁星 2023-05-16 15:56:13
我有一個任務多次調用繪圖并根據結果創建 gif(請參閱下面的可重現示例)。我的問題是:我想為這個 gif 添加背景圖像。我的猜測是,我必須在附加之前將它添加到循環中的某個位置。到目前為止我發現了什么,雖然很搞笑并沒有真正涉及到我的問題:在 matplotlib 圖上繪制裁剪后的背景圖像import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport matplotlib.animation as animationfrom matplotlib.animation import PillowWriterimport timerng = np.random.default_rng()fig = plt.figure(figsize=[10, 9])ims = []for i in range(200):    df = pd.DataFrame(rng.integers(0, 100, size=(100, 2)), columns=list('xy'))    x = df["x"]    y = df["y"]    im = plt.plot(x, y, "b.")    ims.append(im)    print(i)            ani = animation.ArtistAnimation(fig, ims, interval=500, blit=True,                                repeat_delay=1000)writer = PillowWriter(fps=2)ani.save("demo2.gif", writer=writer)
查看完整描述

1 回答

?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

我對此沒有太多經驗,但我能夠顯示我事先調用的圖像plt.imshow()。demo.gif由于文件大小限制,無法上傳。抱歉,示例中使用的圖片效果不佳。對不起。


import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation

from matplotlib.animation import PillowWriter

import time

import matplotlib.image as mpimg


rng = np.random.default_rng()

fig = plt.figure(figsize=[10, 9])


img = mpimg.imread('lena_thumbnail_center_square.jpg')


ims = []

for i in range(200):

    df = pd.DataFrame(rng.integers(0, 100, size=(100, 2)), columns=list('xy'))

    x = df["x"]

    y = df["y"]

    im = plt.plot(x, y, "b.")

    ims.append(im)

#     print(i)   


ani = animation.ArtistAnimation(fig, ims, interval=500, blit=True,

                                repeat_delay=1000)

plt.imshow(img)


writer = PillowWriter(fps=2)

ani.save("demo2.gif", writer=writer)

http://img1.sycdn.imooc.com//6463373a000129d405430526.jpg

查看完整回答
反對 回復 2023-05-16
  • 1 回答
  • 0 關注
  • 185 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號