Do not enable default Matplotlib backend when checking what the backend is #38
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #37 and #25.
When importing
backend_inline
the function_enable_matplotlib_integration()
is automatically called and this checks what the current Matplotlib backend is. Previously callingmatplotlib.get_backend()
if no backend was currently set would as a side-effect force it to be the default backend for that OS rather than leaving it unset. Changing it tomatplotlib.rcParams._get("backend")
avoids the side-effect thus fixing the problem.Although
rcParams._get
is a private function this is in fact the correct function to use as explained in the Matplotlib source code:https://github.com/matplotlib/matplotlib/blob/eb812a8e479d0bcf43a30232c31afec78963ed53/lib/matplotlib/__init__.py#L698-L713
To reproduce the original problem and confirm this PR fixes it use the following on the
main
branch and this PR branch:~/.ipython/profile_default/ipython_kernel_config.py
containing the single linec.InteractiveShellApp.matplotlib = 'inline'
.jupyter lab
orjupyter qtconsole
and run the following code:import matplotlib.pyplot as plt; plt.plot([1,3,2])
On
main
branch this uses the wrong Matplotlib backend and on this PR branch it used the correctinline
backend.I have manually tested other use cases of using Matplotlib in
ipython
andjupyter
and it looks like this fix does not introduce any new problems.