Subject: Re: bug#70687: python-matplotlib not respecting env var MPLBACKEND=TkAgg

From: Jake <[email-masked]>
Date: Thu, 3 Apr 2025 11:36:49 +0000

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 <[email-masked]> 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
>