Search code examples
pythonmatplotlibfigure

Continously updating a single plt.figure instead of creating multiple figure-windows


Ive got a loop in which i gather measurement data which needs to be drawn in about 12 subplots (here only 4 subplots) in 1 figure, which should update the subplots during every loop-cycle. However, my code just creates an additional figure-window every loop-cycle instead of updating the single figure-window. Also the measurement data consists of large float-values (e.g. 989.9871234) which crowds my major y-tick labels, so the y-axis are unreadable and i havent found a working solution to limit the number of y-axis-labels. I used the example from Code example as the basis for my code.

Heres my code (unnecessary measurement data gathering omitted):

    fig = plt.figure(figsize=(6, 4))
    sub1 = fig.add_subplot(221)
    sub1.plot(secondspassed_array, resistance1_array)
    sub2 = fig.add_subplot(222)
    sub2.plot(secondspassed_array, resistance2_array)
    sub3 = fig.add_subplot(223)
    sub3.plot(secondspassed_array, resistance3_array)
    sub4 = fig.add_subplot(224)
    sub4.plot(secondspassed_array, resistance4_array)
    plt.plot(secondspassed_array, resistance4_array)
    plt.tight_layout()
    plt.ion()
    plt.pause(0.05)

Solution

  • try to put plt.figure(figsize=(6, 4)) outside the loop