Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not enable default Matplotlib backend when checking what the backend is #38

Merged
merged 3 commits into from
Dec 10, 2024

Conversation

ianthomas23
Copy link
Collaborator

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 calling matplotlib.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 to matplotlib.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:

  1. Create file ~/.ipython/profile_default/ipython_kernel_config.py containing the single line c.InteractiveShellApp.matplotlib = 'inline'.
  2. Use either jupyter lab or jupyter 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 correct inline backend.

I have manually tested other use cases of using Matplotlib in ipython and jupyter and it looks like this fix does not introduce any new problems.

@ianthomas23
Copy link
Collaborator Author

The test image changes are on the main branch rather than this PR; I have included them here rather than create a separate PR.

@ianthomas23
Copy link
Collaborator Author

I've converted this to Draft as there are changes planned in Matplotlib that will make the fix different and better.

@ianthomas23 ianthomas23 marked this pull request as ready for review November 8, 2024 08:38
@ianthomas23
Copy link
Collaborator Author

This is now ready. After this is merged I would like a new release.

@Carreau Carreau merged commit d7df790 into ipython:main Dec 10, 2024
4 checks passed
@Carreau
Copy link
Member

Carreau commented Dec 10, 2024

Sorry I missed that ping me if this get delayed.

@Carreau
Copy link
Member

Carreau commented Dec 10, 2024

I've made you maintainer to unblock you a bit.

@ianthomas23 ianthomas23 deleted the 37-no-auto-enable branch December 10, 2024 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Do not automatically resolve default Matplotlib backend
2 participants