1

im trying to make a program that plots points, shows the plt, and pauses for 0.1 sec. Now everytime i run the program the plt.pause(0.1) function seems to be additive. Anyone have a clue how to fix that? I timed it using the code below but you can feel the time-gaps between runs get longer.

        start = time.process_time()
        plt.show(block=False)
        print("2: ", time.process_time() - start)
        start = time.process_time()
        plt.pause(0.1)
        print("3: ", time.process_time() - start)

Output in terminal (First time it run and last time before closing it)

2:  0.0
3:  1.328125

2:  0.0
3:  4.703125

1 Answers1

0

Welcome to SO!

Matplotlib does not necessarily draw a figure canvas the moment you call plt.show(). IIRC, with block=False the figure canvas is not actually drawn until the first plt.pause command (or when control is returned to the user at the end of script execution). So nothing actually happens at the plt.show call, which is hence fast. The first plt.pause call is slow, as that is the point in the script when the figure is actually drawn. If you add another plt.pause call, the execution time is short again.

import time
import matplotlib.pyplot as plt

plt.figure()

start = time.process_time()
plt.show(block=False)
print("2: ", time.process_time() - start)

start = time.process_time()
plt.pause(0.1)
print("3: ", time.process_time() - start)

start = time.process_time()
plt.pause(0.1)
print("4: ", time.process_time() - start)

# 2:  0.00012074099999992427
# 3:  0.020836499999999925
# 4:  0.007637112999999918
Paul Brodersen
  • 11,221
  • 21
  • 38