'Matplotlib - pyplot is overwriting the backend agg config

I need to change the default matplotlib backend from agg to TkAgg.

I have added the line backend: TkAgg to my matplotlibrc file located at ~/.config/matplotlib/matplotlibrc.

If from the terminal I run:

>>> python
>>> import matplotlib as mpl
>>> print(mpl.get_backend())
>>> import matplotlib.pyplot as plt
>>> print(mpl.get_backend())

I get:

TkAgg
agg

So it seems that pyplot is somehow overwriting the backend I've configured. Is this expected behaviour? Is there any way I can prevent it from happening?

Interestingly, if I change the backend to WebAgg in the matplotlibrc config file, this isn't overwritten when importing pyplot.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source