ContextLab / hypertools

A Python toolbox for gaining geometric insights into high-dimensional data
http://hypertools.readthedocs.io/en/latest/
MIT License
1.82k stars 160 forks source link

revamp setting matplotlib backend #242

Closed paxtonfitzpatrick closed 3 years ago

paxtonfitzpatrick commented 3 years ago

the plot submodule currently tries to set the backend to TkAgg when hypertools Is first imported, and shows a warning if it fails.
https://github.com/ContextLab/hypertools/blob/9ac3dc11123419f2f00d596ac5920db2486cc0a1/hypertools/plot/__init__.py#L1-L10

This creates a whole bunch of issues:

  1. silently changes the backend that may have been previously set by the user
  2. If tkinter isn't installed or set up, matplotlib falls back to the non-GUI Agg backend. This means plots won't display at all, even if there is a different non-tkinter backend available
  3. In environments without X11 installed (e.g., small Docker containers), the warning is either shown or has to be suppressed manually any time hypertools is imported, even if not for plotting.
  4. if imported into a notebook, it creates a phantom Python process that can't be killed without the IPython kernel dying
  5. if hypertools is imported after matplotlib.pyplot, no plots will show at all because of a post-execute callback that matplotlib sets when imported into a notebook where the backend hasn't been manually set yet.

I'm working on a fix to this and will open a PR once finished