anntzer / mplcursors

Interactive data selection cursors for Matplotlib.
https://mplcursors.readthedocs.io
zlib License
113 stars 19 forks source link

Unable to provide configuration via environmental variable #68

Open ascended121 opened 8 months ago

ascended121 commented 8 months ago

I'm attempting to configure mplcursors via the following code:

import os
os.environ["MPLCURSORS"] = '{"hover": 1}'
import mplcursors

however this doesn't appear to work... no cursors appear when I click on the resulting plots. I'm not sure why, because my understanding is that this sets env vars for the python process. Seems that setting the env vars before mplcursors is imported would cause it to see them, just as if they had been set at the CLI.

When I call my script via

MPLCURSORS='{"hover": 1}' python3 my_script.py

I see cursors as expected.

I'd like to be able to globally configure cursors (since I have a lot of plots), but I don't want the user to have to remember to set env vars when they invoke the script, which is why I'm trying to set them in the code itself.

Is this method of setting env variables expected to work? Does it matter where the env var is set relative to the mplcursors import? Or relative to the matplotlib import?

Is there an easier way of configuring mplcursors globally without using env vars? Perhaps a module variable that could be set?

anntzer commented 8 months ago

Setting the environment variable a posteriori is not expected to work given the way things are implemented right now. It would indeed be nice if it did; ideally registration should be rewritten to be based on the new rcParams["figure.hooks"] system but I can't promise any timeline for that. (There's no other global config available either.)