Python Forum
How to plot data to the same figure for every run?
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to plot data to the same figure for every run?
#1
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()
Reply
#2
Use plt.draw() instead of plt.show(). Each call to plt.draw() will replot the image, for example after adding more data to it.
Reply
#3
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..
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  1D plot frome 3D data layla 7 1,097 Jul-16-2023, 10:51 PM
Last Post: Pedroski55
  How to plot 2 graphs in one figure? man0s 1 1,336 Apr-25-2022, 09:18 AM
Last Post: Axel_Erfurt
  extract and plot data from a txt file usercat123 2 1,208 Apr-20-2022, 06:50 PM
Last Post: usercat123
  Searching Module to plot large data G_rizzle 0 1,421 Dec-06-2021, 08:00 AM
Last Post: G_rizzle
  Not able to figure out how to create bar plot on aggregate data - Python darpInd 1 2,251 Mar-30-2020, 11:37 AM
Last Post: jefsummers
  It doesn't show scatter plot but it says: "<Figure size 640x480 with 1 Axes>" dcardonaa 0 3,455 Oct-10-2019, 02:34 AM
Last Post: dcardonaa
  HeatMap plot with arduino serial data tshivam 0 3,177 Oct-08-2018, 10:57 AM
Last Post: tshivam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020