如何在不创建多个图形的情况下更新绘图?

huangapple go评论67阅读模式
英文:

How can I update a plot without creating multiple figures?

问题

I am using Python 3.9 in Spyder and have a problem with plotting.
Whenever I plot something, in the "Plot" Pane, a new plot pops up.

For my application, I want to update the plot very quickly but every time I plot the figure, a new figure appears in the "Plot" Pane and after 500 plots it gets very laggy.
With hours of searching on the web, I have found no good way to do it.

Ideally, the plot contents should just be removed and not the figure or axes.

My code is as follows:

import matplotlib.pyplot as plt # Module for plotting data
import time # Module for acquiring times

for i in range(1,11):

    fig = plt.figure()
    ax = plt.axes(projection = '3d')
    
    ax.set_xlim(-10,10)
    ax.set_ylim(-10,10)
    ax.set_zlim(-10,10)
    
    ax.quiver(0,0,0,i,i,i)
    plt.show()
    time.sleep(0.1)

Its very straightforward and easy, but all it produces is 10 individual plots in the "Plot" pane (I muted Inline Plotting).
Preferably, I would have one plot which just updates, either in the "Plot" Pane of Spyder or as a separate window.

英文:

I am using Python 3.9 in Spyder and have a problem with plotting.
Whenever I plot something, in the "Plot" Pane, a new plot pops up.

For my application, I want to update the plot very quickly but every time I plot the figure, a new figure appears in the "Plot" Pane and after 500 plots it gets very laggy.
With hours of searching on the web, I have found no good way to do it.

Ideally, the plot contents should just be removed and not the figure or axes.

My code is as follows:

import matplotlib.pyplot as plt # Module for plotting data
import time # Module for acquiring times

for i in range(1,11):

    fig = plt.figure()
    ax = plt.axes(projection = '3d')
    
    ax.set_xlim(-10,10)
    ax.set_ylim(-10,10)
    ax.set_zlim(-10,10)
    
    ax.quiver(0,0,0,i,i,i)
    plt.show()
    time.sleep(0.1)

Its very straight forwards and easy, but all it produces is 10 individual plots in the "Plot" pane (I muted Inline Plotting).
Preferably, I would have one plot which just updates, either in the "Plot" Pane of Spyder or as a seperate window.

答案1

得分: 1

以下是您要翻译的代码部分:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = plt.axes(projection='3d')

for i in range(1, 11):
   
    ax.set_xlim(-10, 10)
    ax.set_ylim(-10, 10)
    ax.set_zlim(-10, 10)
    ax.quiver(0, 0, 0, i, i, i)
    plt.draw()
    plt.pause(0.01)
    ax.clear()

图表将每0.01秒更新一次。

英文:

You can try something like this:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = plt.axes(projection = '3d')

for i in range(1,11):
   
    ax.set_xlim(-10,10)
    ax.set_ylim(-10,10)
    ax.set_zlim(-10,10)
    ax.quiver(0,0,0,i,i,i)
    plt.draw()
    plt.pause(.01)
    ax.clear()

The plot will get updated every 0.01 seconds.

如何在不创建多个图形的情况下更新绘图?

答案2

得分: 0

Updating an existing plot usually entails creating a [tag:matplotlib-animation].

As per this answer.

import matplotlib.animation as animation
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

def update(i):
    ax.clear()
    ax.set_xlim(-10, 10)
    ax.set_ylim(-10, 10)
    ax.set_zlim(-10, 10)
    ax.quiver(0, 0, 0, 10, 10, 10)

ani = animation.FuncAnimation(fig, update, frames=11, interval=1)

# this is not needed unless you want to save the file
# ani.save('quiver.gif', writer='pillow')

如何在不创建多个图形的情况下更新绘图?

英文:

Updating an existing plot usually entails creating a [tag:matplotlib-animation].

As per this answer.

import matplotlib.animation as animation
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

def update(i):
    ax.clear()
    ax.set_xlim(-10, 10)
    ax.set_ylim(-10, 10)
    ax.set_zlim(-10, 10)
    ax.quiver(0, 0, 0, 10, 10, 10)
        
ani = animation.FuncAnimation(fig, update, frames=11, interval=1)

# this is not needed unless you want to save the file
# ani.save('quiver.gif', writer='pillow')

如何在不创建多个图形的情况下更新绘图?

huangapple
  • 本文由 发表于 2023年5月25日 20:00:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332028.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定