0

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.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
F F
  • 1

2 Answers2

1

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.

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Pablo C
  • 4,661
  • 2
  • 8
  • 24
0

Updating an existing plot usually entails creating a .

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')

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
  • Thanks! Unfortunately your code does not work in my Python... Just plain like you posted, it doesn't do anything besides plotting the last plot in the "Plot" Pane of Spyder. – F F May 26 '23 at 13:55