-
Notifications
You must be signed in to change notification settings - Fork 308
Use matplotlib backend registry when possible #5141
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,9 +45,6 @@ class FormatKwargs(TypedDict): | |
|
|
||
|
|
||
| BACKEND_SPECS = { | ||
| "gtk": ["backend_gtk", "FigureCanvasGTK", "FigureManagerGTK"], | ||
| "gtkagg": ["backend_gtkagg", "FigureCanvasGTKAgg", None], | ||
| "gtkcairo": ["backend_gtkcairo", "FigureCanvasGTKCairo", None], | ||
| "macosx": ["backend_macosx", "FigureCanvasMac", "FigureManagerMac"], | ||
| "qt5agg": ["backend_qt5agg", "FigureCanvasQTAgg", None], | ||
| "qtagg": ["backend_qtagg", "FigureCanvasQTAgg", None], | ||
|
|
@@ -138,6 +135,8 @@ def __init__( | |
| figure_canvas, figure_manager = self._get_canvas_classes() | ||
| self.canvas = figure_canvas(self.figure) | ||
| if figure_manager is not None: | ||
| # with matplotlib >= 3.9, figure_manager should always be not None | ||
| # see _get_canvas_classes for details. | ||
| self.manager = figure_manager(self.canvas, 1) | ||
|
|
||
| self.axes.tick_params( | ||
|
|
@@ -151,11 +150,20 @@ def _create_axes(self, axrect: tuple[float, float, float, float]) -> None: | |
|
|
||
| def _get_canvas_classes(self): | ||
| if self.interactivity: | ||
| key = str(matplotlib.get_backend()).lower() | ||
| key = str(matplotlib.get_backend()) | ||
| else: | ||
| key = "agg" | ||
|
|
||
| module, fig_canvas, fig_manager = BACKEND_SPECS[key] | ||
| if matplotlib.__version_info__ >= (3, 9): | ||
| # once yt has a minimum matplotlib version of 3.9, this branch | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would not bother with this comment; to me, it's 100% implied by the code itself. But, NBD if you think it's helpful.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mainly added it cause BACKEND_SPECS is not defined within the scope of the function. so i'd rather keep it i think? but also don't feel strongly about it.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yeah, when you put it like that, I agree with leaving it. |
||
| # can replace the rest of this function and BACKEND_SPECS can | ||
| # be removed. See https://github.com/yt-project/yt/issues/5138 | ||
| from matplotlib.backends import backend_registry | ||
|
|
||
| mod = backend_registry.load_backend_module(key) | ||
| return mod.FigureCanvas, mod.FigureManager | ||
|
|
||
| module, fig_canvas, fig_manager = BACKEND_SPECS[key.lower()] | ||
|
|
||
| mod = __import__( | ||
| "matplotlib.backends", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import matplotlib | ||
| import pytest | ||
|
|
||
| from yt.visualization.base_plot_types import BACKEND_SPECS | ||
|
|
||
|
|
||
| @pytest.mark.skipif( | ||
| matplotlib.__version_info__ < (3, 9), | ||
| reason="This test requires matplotlib 3.9 or higher.", | ||
| ) | ||
| @pytest.mark.parametrize("backend_key", BACKEND_SPECS.keys()) | ||
| def test_backend_specs(backend_key): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this test lead to the discovery of invalid entries, so it's great. Do you think it's possible to also test that the dictionary is complete ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not in a satisfyingly automated way... you can get a list of all backends with: but i'm not sure we want the non-interactive backends in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the additional info. Yes, such a try/except could improve UX, but I now think the PR is good as is, so, up to you !
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, i'll leave it off then. given the lack of bug reports and questions i don't think it's a huge priority. |
||
| # note: while this test itself requires matplotlib 3.9, it is | ||
| # testing functionality in yt that can be removed once yt has | ||
| # a minimal matplotlib version of 3.9, | ||
| # see https://github.com/yt-project/yt/issues/5138 | ||
| from matplotlib.backends import backend_registry | ||
|
|
||
| assert backend_registry.is_valid_backend(backend_key) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be expressed directly within the condition: we can only test for None on older matplotlib. This approach would be more greppable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ya, I thought about that but we'd still have to set
self.managerfor newer matplotlib so I thought the if statement became less obvious than the comment. i.e., would need this to be:but I wasn't thinking about greppability. which is a good point, so happy to put the above change in and drop the comment.