Skip to content

[Bug]: Backend falls back to Agg even though TkAgg will work #31715

@parthkalgaonkar

Description

@parthkalgaonkar

Bug summary

I've set MPLBACKEND to be "TkAgg". When I import matplotlib.pyplot, the backend switches to "agg". I'm assuming this is some fallback behavior, which would be understandable except for the fact that TkAgg works just fine

If I manually set backend with matplotlib.use("TkAgg") before importing pyplot, things work as expected.

I would like to avoid having to set the backend in python code, since I might use this with other backends as well

Code for reproduction

# test1.py
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
assert plt.get_backend() == "TkAgg"  # This works. Plotting also uses Tk as expected

# test2.py
import matplotlib.pyplot as plt
assert plt.get_backend() == "TkAgg"  # AssertError. Got "agg" instead

Actual outcome

No output for test1.py

AssertionError for test2.py

Expected outcome

No output for either

Additional information

Output with DEBUG level logging, in case that might prove useful

DEBUG:matplotlib:matplotlib data path: xxxxxREDACTEDxxxxx/.venv/lib/python3.13/site-packages/matplotlib/mpl-data
DEBUG:matplotlib:CONFIGDIR=xxxxxREDACTEDxxxxx
DEBUG:matplotlib:interactive is False
DEBUG:matplotlib:platform is linux
DEBUG:matplotlib:CACHEDIR=xxxxxREDACTEDxxxxx
DEBUG:matplotlib.font_manager:Using fontManager instance from xxxxxREDACTEDxxxxx/fontlist-v390.json
DEBUG:matplotlib.pyplot:Loaded backend agg version v2.2.

Operating system

NixOS

Matplotlib Version

3.10.9

Matplotlib Backend

TkAgg

Python version

3.13.12

Jupyter version

No response

Installation

uv

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions