Python Forum

Full Version: How to plot data to the same figure for every run?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How to plot data to the same figure for every run?

Hi

I wonder how it is possible to plot data to the same figure in python for every run. When I run the code below once, it will plot some data in figure 1. When I run it the second time I want the second set of random numbers plotted on top of the first set in figure 1 (like Matlab would behave when there is a hold on in the end). Instead Python opens another figure 1 and plots the secoond set there. Can I somehow find figure handles and check if figure 1 is open already and then use this as parameter in the plotting function? Or is there a more simple way? The code below is a mini example and plotting another time in the same run is not a solution for me.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(1, 10, 10)
y = np.random.randn(10)

plt.figure(1)
plt.plot(x, y)
plt.ylabel('signal')
plt.xlabel('time')
plt.show()
Use plt.draw() instead of plt.show(). Each call to plt.draw() will replot the image, for example after adding more data to it.
Hi heras

I tried this. When I exchange plt.show() with plt.draw() no figure will be plotted. Using both draw() and show() will result in two separate figures again..
Hi,

I'm sorry that was incorrect. The following works for me:
import matplotlib.pyplot as plt

plt.ion() # This puts plt in interactive mode
plt.plot([1,2,3,4])
input("Press enter")
plt.plot([2,4,1,3])
input("Press enter")

while True: # This prevents the plot from closing
    pass