Hello guys,
I am running a python script on a HPC via SLURM.
I want to save a plot without displaying it.
That seems to be a huge problem.
My script looks like this:
import matplotlib.pyplot as plt
import numpy as np
a = np.array([1,2,2,2,2,3,3,3,4,4,5])
plt.ioff()
plt.hist(a)
plt.savefig(‘testimg.png’)
plt.close()
I thought that plt.ioff() would prevent the plt.hist(a) from creating a figure that wants to be displayed.
Unfortunately I get the following error.
Traceback (most recent call last):
File “PlotTest.py”, line 7, in
plt.hist(a)
File “/fibus/fs1/16/cql7772/.local/lib/python3.7/site-packages/matplotlib/pyplot.py”, line 2631, in hist
return gca().hist(
File “/fibus/fs1/16/cql7772/.local/lib/python3.7/site-packages/matplotlib/pyplot.py”, line 920, in gca
return gcf().gca(**kwargs)
File “/fibus/fs1/16/cql7772/.local/lib/python3.7/site-packages/matplotlib/pyplot.py”, line 597, in gcf
return figure()
File “/fibus/fs1/16/cql7772/.local/lib/python3.7/site-packages/matplotlib/pyplot.py”, line 539, in figure
**kwargs)
File “/fibus/fs1/16/cql7772/.local/lib/python3.7/site-packages/matplotlib/backend_bases.py”, line 3252, in new_figure_manager
return cls.new_figure_manager_given_figure(num, fig)
File “/fibus/fs1/16/cql7772/.local/lib/python3.7/site-packages/matplotlib/backends/_backend_tk.py”, line 946, in new_figure_manager_given_figure
window = tk.Tk(className=“matplotlib”)
File “/fibus/fs1/16/cql7772/.conda/envs/ganomaly/lib/python3.7/tkinter/init.py”, line 2023, in init
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn’t connect to display “localhost:11.0”
Does anyone knows how to prevent this error? I just want to get the plot as a .png file
Many thanks in advance!