# Python Matplotlib + Tkinter. Matplotlib reverts to "agg" backend because it can't find a valid display

**URL:** https://discourse.nixos.org/t/python-matplotlib-tkinter-matplotlib-reverts-to-agg-backend-because-it-cant-find-a-valid-display/45064
**Category:** Help
**Created:** [May 8, 2024, 10:04am UTC](https://discourse.nixos.org/t/python-matplotlib-tkinter-matplotlib-reverts-to-agg-backend-because-it-cant-find-a-valid-display/45064 "2024-05-08T10:04:12Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![holden-c](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@holden-c](https://discourse.nixos.org/u/holden-c)
#### Post date: [May 8, 2024, 10:04am UTC](https://discourse.nixos.org/t/python-matplotlib-tkinter-matplotlib-reverts-to-agg-backend-because-it-cant-find-a-valid-display/45064/1 "2024-05-08T10:04:12Z")

</div>

Hi,

I am using python and poetry, provided by NixOS to install a poetry virtual environment (no poetry2nix here).

I have set the matplotlib backend to “TkAgg” using the “MPLBACKEND” env var.

But when I try to plot something, the backend reverts to “agg”. I have traced the cause to the misidentificaiton of a headless session, which causes mpl to fallback to “agg” as the backend. The relevant clause is here:

> <https://github.com/matplotlib/matplotlib/blob/bc13abfc15eb3fc97f8e6dc057d0883e788c7f08/lib/matplotlib/cbook.py#L80C1-L81C26>

I can force matplotlib to use “TkAgg” with `matplotlib.use("TkAgg")`, however this solution isn’t tenable for shared projects.

I assume that this `.display_is_valid()` method is returning headless because it can’t find some Wayland or X11 libraries it’s expecting to find. But would appreciate any pointers regarding how I can resolve this.

Many thanks for any help.

---

<div class="post-metadata">

### Author: ![holden-c](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/h/dec6dc/32.png) [@holden-c](https://discourse.nixos.org/u/holden-c)
#### Post date: [May 8, 2024, 1:18pm UTC](https://discourse.nixos.org/t/python-matplotlib-tkinter-matplotlib-reverts-to-agg-backend-because-it-cant-find-a-valid-display/45064/2 "2024-05-08T13:18:47Z")

</div>

For anyone else facing similar issues - I resolved this by adding the X11 library to the LD\_LIBRARY\_PATH of my development shell. I am using X11, not sure if this works for WAYLAND.

```auto
...
      devShells."x86_64-linux".default = pkgs.mkShell {

        buildInputs = [pkgs.python3Full pkgs.poetry];

        LD_LIBRARY_PATH = "$LD_LIBRARY_PATH:${
            (pkgs.lib.makeLibraryPath [
              pkgs.stdenv.cc.cc
              pkgs.zlib # numpy
              pkgs.xorg.libX11 # matplotlib
            ])
          };
        MPLBACKEND = "TkAgg";
...

```
