Home>

It is almost as the title.

I want to make an animation with matplotlib,
At first, I tried to write a program that "display coordinates [1,5] for 3 seconds, then display coordinates [2,6]", but

A window with coordinates [1,5] appears. If i delete the window, a window with coordinates [1,5] appears after 3 seconds.

It will be

behavior. (Well, it's natural that it works because I'm writing a program)

Please tell me how to display only the graph for 3 seconds without changing the window.

※ I do not want to use matplotlib.animation, so please use other than this><

The following is the current program.

import numpy as np
import matplotlib.pyplot as plt
import time
fig = plt.figure () # set figure
ax = fig.add_subplot (111) # Add graph drawing area
ax.set_xlim (0, 100) # Set axis range
ax.set_ylim (0, 100)
plt.plot (1, 5, marker = '.', markersize = 20, color = 'lightsalmon')
plt.show ()
time.sleep (3)
plt.plot (2, 6, marker = '.', markersize = 20, color = 'lightsalmon')
plt.show ()
  • Answer # 1

    import matplotlib.pyplot as plt
    fig = plt.figure () # set figure
    ax = fig.add_subplot (111) # Add graph drawing area
    ax.set_xlim (0, 100) # Set axis range
    ax.set_ylim (0, 100)
    ax.plot (1, 5, marker = '.', markersize = 20, color = 'lightsalmon')
    plt.pause (3) # wait 3 seconds
    ax.lines.pop () # erase point
    ax.plot (2, 6, marker = '.', markersize = 20, color = 'lightsalmon')
    plt.show () # As far as I checked at hand, this seems to be fine, but I'm not sure if it's really good

    pauseis a function that has been written in the document as "It is recommended to use animation if you do it properly" or "It is experimental and cannot guarantee fine operation", so this method is ideal It is not a translation. Think of it as an answer that you could have just tried in my environment (depending on the environment, the above code may not work).

    matplotlib.pyplot.pause — Matplotlib 3.1.1 documentation