英文:
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')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论