0

Despite plt.ioff() I can't save figures on headless AWS instance. I am using the following code

title = 'Losses'

plt.ioff()

plt.figure(1)
plt.plot(losses, 'r^', label='train')
plt.plot(val_losses, 'g^', label='validate')
plt.legend(loc='upper left')
plt.title(title)
plt.savefig(os.path.join(target_directory, 'losses.png'))

How to overcome?

UPDATE

Writing

import matplotlib
matplotlib.use('Agg')

at the very beginning of a script causes warning

This call to matplotlib.use() has no effect because the backend has already been chosen; matplotlib.use() must be called before pylab, matplotlib.pyplot, or matplotlib.backends is imported for the first time.

UPDATE 2

Placing .matplotlibrc in current directory with the following content

backend      : agg

also does not help

Dims
  • 47,675
  • 117
  • 331
  • 600
  • The warning tells you that something imports matplotlib.pyplot before your script starts. Since we have no information whatsoever about your system, your workflow and the way you call your script, how do you imagine getting help here? – ImportanceOfBeingErnest Jul 28 '17 at 16:56
  • I am imaginining general instructions to initialize `pyplot` in desired way. If "something" can intervent desired initialization, it should be a way to track this. Imagine you had a car, which ignit only if someone doesn't ignit a bus on another street and you had no control over this. I would say it is bad design, right? – Dims Jul 28 '17 at 17:18
  • If you want to complain about the design of matplotlib, you can go to the matplotlib issue tracker on GitHub. If, in contrary you want help with your problem, you may decide to provide the requested information. Also mind that the solution from the linked question has worked for 377 people so far, so chances are high that this is really a problem of your specific setup. – ImportanceOfBeingErnest Jul 28 '17 at 17:26
  • I agree that this is the problem with my setup. But I can't post tons of files it consists of. So, I need to find "something" which prevents pyplot init myself. For this, I need an information and asking the question in consequence... – Dims Jul 28 '17 at 17:38
  • Actually, I found the solution: I need to put initialization lines in the beginning of my main caller script, which I don't like. I would like to have a module, which does it's work independently of the caller. – Dims Jul 28 '17 at 17:40
  • 1
    So to summarize, you have a script that imports pyplot and that script imports another script, which also imports pyplot. As the warning says, you need to **set the backend before the first import of pyplot**. From a design point of view it is clear that you cannot change the backend in the middle of a process. So you need to think for yourself how to organize your workflow to be able to set the backend at the very beginning. – ImportanceOfBeingErnest Jul 28 '17 at 17:49
  • I would like to have a product: a library, which works as it should work. I am not the author of the pyplot, so I can't allow myself to just print a warning and fail the job. – Dims Jul 28 '17 at 17:56
  • If you want to use a library like matplotlib for your product, you need to live with what the library offers. If the library does not allow to change the backend midway in a process (which would make the library completely unstable), you need to accept that and design your product such that it handles all possible cases correctly. The warning is meant for you as a product designer to make you aware that your design is not correct. It is thus a service of matplotlib for you to help you. – ImportanceOfBeingErnest Jul 28 '17 at 18:08
  • I am not responsible for design of the caller. – Dims Jul 28 '17 at 18:44
  • So. How ot make `.matplotlibrc` solution work? If `.matplotlibrc` file is existing inside current directory, then what can be that "something", which still has time to preset the backend? – Dims Jul 28 '17 at 18:46
  • What is that "caller"? It sounds a bit like that "caller" is the "something" that imports pyplot and thus sets the backend. – ImportanceOfBeingErnest Jul 28 '17 at 18:49

0 Answers0