0

I am attempting to write a sample chart that updates on every 0.5 seconds.

The code is like so:

import matplotlib.pyplot as plt
import numpy as np
import time

# Some example data to display
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

fig, axs = plt.subplots(2, 2)

i = 5
for row in x:
    axs[0, 0].plot(x[len(x) - i:], y[len(y) - i:])
    axs[0, 0].set_title("main")

    axs[1, 0].plot(x, y ** 2)
    axs[1, 0].set_title(f"shares {i} with main")
    axs[1, 0].sharex(axs[0, 0])

    axs[0, 1].plot(x + 1, y + 1)
    axs[0, 1].set_title("unrelated")

    axs[1, 1].plot(x + 2, y + 2)
    axs[1, 1].set_title("also unrelated")

    fig.canvas.draw()
    fig.canvas.flush_events()
    fig.tight_layout()
    i += 1
    plt.show()

    print('THIS NEVER PRINTS - IT SEEMS TO BLOCK ON THE show() CALL ABOVE')
    time.sleep(0.5)

If you run it, the charts will be displayed. But that show() call seems to block, and the loop never executes to update the charts.

How can I make it update?

Edy Bourne
  • 5,679
  • 13
  • 53
  • 101
  • 2
    Are you wanting to have the figure appear and then dynamically update the content over time? [Interactive mode for matplotlib](https://matplotlib.org/3.3.0/users/interactive.html) might help out, or perhaps using the `plt.pause` function [as per this link](https://stackoverflow.com/questions/11874767/how-do-i-plot-in-real-time-in-a-while-loop-using-matplotlib) – PeptideWitch Feb 10 '21 at 00:39
  • That was it! If you post as an answer I can accept - thanks! – Edy Bourne Feb 10 '21 at 00:58

0 Answers0