Let's say we have a computer that we access either locally or remotely (both with X server forwarding [ssh -X
] and without it ssh
).
When using matplotlib we probably want to use a different backend depending on the kind of session we're in. Agg
when there is no X server so we get plots and not exceptions, and TkAgg
when there is so we can play a bit more with our plots.
You can set the default matplotlib backend through the
matplotlibrc
files both system-wide and user-wide.You can set which backend you want to use for that script at its first lines using
matplotlib.use
or evenmatplotlib.rcParams
.
But all these are static solutions, that work well if you always want to use the same backend (per computer, per user or even per script)
If you want the backend to be chosen in a given script depending on the availability X server, you can check the display at the beginning of the script and then load whichever backend you prefer. However, you need to insert this lines of code at the beggining of every script you want to run in both scenarios...
Is there any way to select the backend depending on the availability of a display but as a configuration that applies system-wide or user-wide, for any script that is run? I'm thinking of something like a default backend when there is display available and a fall-back backend when there is not, that applies without modifying the scripts (unless the scripts specifically select some backend). Is this possible?
I would like matplotlib to have this fallback functionality as a setting somewhere, but I can't find anything.
So I made this script that you can put in
.bashrc
to fall back automatically ifDISPLAY
is not set.You can adapt the backends to use with and without
DISPLAY
(replaceTkAgg
andAgg
) and change the location of matplotlibrc (two places).This is particularly useful if you have a homedir that you use through
ssh
and through graphical interface (like the question); otherwise you can just create a staticmatplotlibrc
.