GNU bug report logs - #70687
python-matplotlib not respecting env var MPLBACKEND=TkAgg

Previous Next

Package: guix;

Reported by: Jake <jforst.mailman <at> gmail.com>

Date: Wed, 1 May 2024 01:25:01 UTC

Severity: normal

To reply to this bug, email your comments to 70687 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-guix <at> gnu.org:
bug#70687; Package guix. (Wed, 01 May 2024 01:25:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Jake <jforst.mailman <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-guix <at> gnu.org. (Wed, 01 May 2024 01:25:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Jake <jforst.mailman <at> gmail.com>
To: bug-guix <at> gnu.org
Subject: python-matplotlib not respecting env var MPLBACKEND=TkAgg
Date: Wed, 1 May 2024 01:23:19 +0000
[Message part 1 (text/plain, inline)]
Hello

We are supposed to be able to control the backend used by Python's
Matplotlib via the MPLBACKEND environment variable [1]. This doesn't appear
to work for the TkAgg backend:

#+begin_src sh

  guix shell python python-matplotlib -- bash -c 'MPLBACKEND=TkAgg python3
-c "import matplotlib; print(matplotlib.get_backend()); import
matplotlib.pyplot as plt; plt.plot([1],[2]); plt.show()"'

#+end_src

#+RESULTS:
: TkAgg
: <string>:1: UserWarning: FigureCanvasAgg is non-interactive, and thus
cannot be shown

However, the TkAgg backend does work if it is hardcoded in the Python
script using `matplotlib.use()`:

#+begin_src sh

  guix shell python python-matplotlib -- python3 -c "import matplotlib;
matplotlib.use('TkAgg'); print(matplotlib.get_backend()); import
matplotlib.pyplot as plt; plt.plot([1],[2]); plt.show()"

#+end_src

#+RESULTS:
: TkAgg

Thanks
Jake

[1]: https://matplotlib.org/stable/users/explain/figure/backends.html
[Message part 2 (text/html, inline)]

Information forwarded to bug-guix <at> gnu.org:
bug#70687; Package guix. (Thu, 03 Apr 2025 11:38:02 GMT) Full text and rfc822 format available.

Message #8 received at 70687 <at> debbugs.gnu.org (full text, mbox):

From: Jake <jforst.mailman <at> gmail.com>
To: 70687 <at> debbugs.gnu.org
Cc: me <at> bonfacemunyoki.com, sharlatanus <at> gmail.com, tanguy <at> bioneland.org,
 lars <at> 6xq.net
Subject: Re: bug#70687: python-matplotlib not respecting env var
 MPLBACKEND=TkAgg
Date: Thu, 3 Apr 2025 11:36:49 +0000
[Message part 1 (text/plain, inline)]
It appears that importing matplotlib.pyplot resets the Matplotlib backend
to Agg.

  $ guix shell python python-matplotlib -- bash -c 'MPLBACKEND=tkagg
python3 -c "import matplotlib; print(matplotlib.get_backend()); import
matplotlib.pyplot; print(matplotlib.get_backend())"'

  TkAgg
  agg


On Wed, May 1, 2024 at 1:25 AM Jake <jforst.mailman <at> gmail.com> wrote:

> Hello
>
> We are supposed to be able to control the backend used by Python's
> Matplotlib via the MPLBACKEND environment variable [1]. This doesn't appear
> to work for the TkAgg backend:
>
> #+begin_src sh
>
>   guix shell python python-matplotlib -- bash -c 'MPLBACKEND=TkAgg python3
> -c "import matplotlib; print(matplotlib.get_backend()); import
> matplotlib.pyplot as plt; plt.plot([1],[2]); plt.show()"'
>
> #+end_src
>
> #+RESULTS:
> : TkAgg
> : <string>:1: UserWarning: FigureCanvasAgg is non-interactive, and thus
> cannot be shown
>
> However, the TkAgg backend does work if it is hardcoded in the Python
> script using `matplotlib.use()`:
>
> #+begin_src sh
>
>   guix shell python python-matplotlib -- python3 -c "import matplotlib;
> matplotlib.use('TkAgg'); print(matplotlib.get_backend()); import
> matplotlib.pyplot as plt; plt.plot([1],[2]); plt.show()"
>
> #+end_src
>
> #+RESULTS:
> : TkAgg
>
> Thanks
> Jake
>
> [1]: https://matplotlib.org/stable/users/explain/figure/backends.html
>
[Message part 2 (text/html, inline)]

Information forwarded to bug-guix <at> gnu.org:
bug#70687; Package guix. (Sat, 05 Apr 2025 23:01:01 GMT) Full text and rfc822 format available.

Message #11 received at 70687 <at> debbugs.gnu.org (full text, mbox):

From: Liliana Marie Prikler <liliana.prikler <at> gmail.com>
To: Jake <jforst.mailman <at> gmail.com>, 70687 <at> debbugs.gnu.org
Cc: me <at> bonfacemunyoki.com, tanguy <at> bioneland.org, sharlatanus <at> gmail.com,
 lars <at> 6xq.net
Subject: Re: bug#70687: python-matplotlib not respecting env var
 MPLBACKEND=TkAgg
Date: Sun, 06 Apr 2025 01:00:44 +0200
Hi Jake,

Am Donnerstag, dem 03.04.2025 um 11:36 +0000 schrieb Jake:
> It appears that importing matplotlib.pyplot resets the Matplotlib
> backend to Agg.
> 
>   $ guix shell python python-matplotlib -- bash -c 'MPLBACKEND=tkagg
> python3 -c "import matplotlib; print(matplotlib.get_backend());
> import matplotlib.pyplot; print(matplotlib.get_backend())"'
> 
>   TkAgg
>   agg
I recently encountered the same issue.  The issue is that matplotlib
internally sources a configuration file that sets the backend *after*
reading the environment variable.  To circumvent this, you use
something along the lines of the following code until the issue is
fixed:

  from matplotlib import set_backend 
  from os import environ
  
  […]

  if __name__ == '__main__':
    if 'MPLBACKEND' in environ: set_backend(environ['MPLBACKEND'])
    […]

Cheers




This bug report was last modified 19 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.