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

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

在 matplotlib 中對散點和線進行動畫處理

在 matplotlib 中對散點和線進行動畫處理

一只萌萌小番薯 2023-08-08 16:59:42
我試圖在 matplotlib 中對線條和散點圖進行動畫處理,并編寫了以下 Python 腳本:-from a import Pitch, get_pointsimport numpy as npimport matplotlib.pyplot as pltimport matplotlib.animation as animationpitch = Pitch(line_color='grey', pitch_color='#121212', orientation='horizontal')fig, ax = pitch.create_pitch()?x_start, y_start = (50, 35)x_end, y_end = (90, 45)x_1, y_1 = get_points(x_start, y_start, x_end, y_end, 0.55)x_2, y_2 = get_points(x_end, y_end, x_start, y_start, 0.55)x = np.linspace(x_1, x_2, 50)y = np.linspace(y_1, y_2, 50)sc_1 = ax.scatter([], [], color="green", zorder=4)line, = ax.plot([], [], color="crimson", zorder=4)sc_2 = ax.scatter([], [], color="gold", zorder=4)def animate(i):? ? ## plot scatter point? ? sc_1.set_offsets([x_start, y_start])? ? ## plot line? ? line.set_data(x[:i], y[:i])? ? ## plot scatter point? ? sc_2.set_offsets([x_end, y_end])? ? return sc_1, line, sc_2ani = animation.FuncAnimation(??? ? fig=fig, func=animate, interval=20, blit=True, save_count=50)??plt.show()我想要的是:在紅線到達黃點的位置后,黃色散點應該出現在圖中。我的程序從一開始就顯示黃點,我希望它在動畫結束時彈出。我可以在代碼中添加/更新什么來進行所需的更改?
查看完整描述

1 回答

?
蕭十郎

TA貢獻1815條經驗 獲得超13個贊

我添加了簡單的條件sc_2。


    if i == len(x):

        sc_2.set_offsets([x_end, y_end])

我測試的代碼:


import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation as animation



fig, ax = plt.subplots()


x_start, y_start = (0, 0)

x_end, y_end = (90, 90)


x_1, y_1 = 0, 0

x_2, y_2 = 90, 90


plt.xlim((0, 100))

plt.ylim((0,100))


x = np.linspace(x_1, x_2, 50)

y = np.linspace(y_1, y_2, 50)


sc_1 = ax.scatter([], [], color="green", zorder=4)

line, = ax.plot([], [], color="crimson", zorder=4)

sc_2 = ax.scatter([], [], color="gold", zorder=4)



def animate(i):

    ## plot scatter point

    sc_1.set_offsets([x_start, y_start])


    ## plot line

    line.set_data(x[:i], y[:i])


    ## plot scatter point

    if i == len(x):

        sc_2.set_offsets([x_end, y_end])


    return sc_1, line, sc_2


ani = animation.FuncAnimation(

    fig=fig, func=animate, interval=100, blit=True, save_count=50)


plt.show()


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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