Avoid potentially changing the matplotlib backend when scraping#1102
Merged
larsoner merged 1 commit intosphinx-gallery:masterfrom Mar 15, 2023
Merged
Avoid potentially changing the matplotlib backend when scraping#1102larsoner merged 1 commit intosphinx-gallery:masterfrom
larsoner merged 1 commit intosphinx-gallery:masterfrom
Conversation
Contributor
Thanks for the deep investigation and fix, I agree this makes sense! I think a regression test here would be hard so I'm not going to ask for it... but if you see an easy way to add one feel free in a follow-up PR :) Thanks @ayshih ! |
Contributor
Author
|
Thanks for the quick merge! I do have an idea that might work for a regression test that I'll try soon. If it works, I'll PR it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The matplotlib backend is set to
aggin the function_import_matplotlib(), which is called both (1) when resetting matplotlib for each example and (2) when scraping each code block for matplotlib plots (matplotlib_scraper()). The forcing of the backend toaggin the latter case means that an example is unable to make use of non-aggbackends.My particular use case is that I want to use
mplcairoin an example to show how to use its blending operators, e.g., the example begins:With the current code, the matplotlib backend is immediately set back to
aggafter scraping the first code block, and so the subsequent code blocks (and the eventual plot) do not work properly.This PR replaces the call of
_import_matplotlib()inmatplotlib_scraper()with direct imports ofmatplotlibandmatplotlib.pyplot, so the backend will not be set toaggwhen scraping for matplotlib plots. Recall that the backend will still continue to be set toaggwhen resetting matplotlib between examples. So, the result of this PR is that an explicit call to change the backend within an example will now persist through the rest of that example, but otherwise the backend will beagg.