0

So there was this thread talking about why matplotlib does not show when executing

python myfile.py

The most popular answer was about resetting matplotlibrc file. What would need to be reset in this file?

Also, some suggest to include matplotlib.use('TkAgg') in the import. Below is my python file:

#myfile.py

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import numpy as np


def dyn_draw():
    x = np.linspace(0, 6 * np.pi, 100)
    y = np.sin(x)

    plt.ion()

    fig = plt.figure()
    ax = fig.add_subplot(111)
    line1, = ax.plot(x, y, 'r-')  # Returns a tuple of line objects, thus the comma

    for phase in np.linspace(0, 10 * np.pi, 500):
        line1.set_ydata(np.sin(x + phase))
        fig.canvas.draw()
        fig.canvas.flush_events()

if __name__ == "__main__":
    dyn_draw()

Running that with python myfile.py, I get the following error:

ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running

Another thread here discussed this error. But the solution still does not work (i.e. restarting the kernel).

How to plot this particular python code with the python command?

Tristan Tran
  • 1,351
  • 1
  • 10
  • 36

1 Answers1

0

You are trying to run matplotlib interatively in a non-interactive environment. The code compiles, but errors at run-time because you running a script.

Have you tried running this in Google Colab? The resource provides free (to a certain point) GPU usage and block coding for interactive visualizations.

Best of luck :)

Justin
  • 1