From 833d45a82ce1ea2ec064d3ada593c9eb2026884f Mon Sep 17 00:00:00 2001 From: hannah Date: Sun, 6 Nov 2022 22:59:09 -0500 Subject: [PATCH 1/2] Changed streamplot return type: StreamplotSet->StreamplotContainer Deprecated StreamplotSet Added StreamplotContainer --- lib/matplotlib/streamplot.py | 54 ++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/streamplot.py b/lib/matplotlib/streamplot.py index f4cd1becf113..b745537bd2d8 100644 --- a/lib/matplotlib/streamplot.py +++ b/lib/matplotlib/streamplot.py @@ -7,6 +7,7 @@ import matplotlib as mpl from matplotlib import _api, cm, patches +from matplotlib.container import Container import matplotlib.colors as mcolors import matplotlib.collections as mcollections import matplotlib.lines as mlines @@ -76,17 +77,9 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None, Returns ------- - StreamplotSet - Container object with attributes + `~.StreamplotContainer` - - ``lines``: `.LineCollection` of streamlines - - - ``arrows``: `.PatchCollection` containing `.FancyArrowPatch` - objects representing the arrows half-way along stream lines. - - This container will probably change in the future to allow changes - to the colormap, alpha, etc. for both lines and arrows, but these - changes should be backward compatible. + .. versionchanged major.minor:: """ grid = Grid(x, y) mask = StreamMask(density) @@ -237,20 +230,57 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None, axes.add_patch(p) axes.autoscale_view() - stream_container = StreamplotSet(lc, ac) + stream_container = StreamplotContainer((lc, ac)) return stream_container +@_api.deprecated("major.minor", alternative="streamplot.StreamplotContainer") class StreamplotSet: - def __init__(self, lines, arrows): self.lines = lines self.arrows = arrows +class StreamplotContainer(Container): + """ + `~.Container` object for artists created in an `~.Axes.streamplot` plot. + + It can be treated like a namedtuple with fields ``(lines, arrows)`` + + .. versionadded major.minor :: + + Attributes + ---------- + lines: `~.LineCollection` +` The collection of `.Line2d` segments that make up the streamlines + arrows: `~.PatchCollection` + The collection of `.FancyArrow` arrows half-way along each streamline + + .. note:: + This container will probably change in the future to allow changes + to the colormap, alpha, etc. for both lines and arrows, but these + changes should be backward compatible. + """ + + def __init__(self, lines_arrows, **kwargs): + """ + Parameters + ---------- + lines_arrows : tuple + Tuple of (lines, arrows) + ``lines``: `.LineCollection` of streamlines. + ``arrows``: `.PatchCollection` of `.FancyArrowPatch` arrows + """ + lines, arrows = lines_arrows + self.lines = lines + self.arrows = arrows + super().__init__(lines_arrows, **kwargs) + + # Coordinate definitions # ======================== + class DomainMap: """ Map representing different coordinate systems. From aedc7195502c6c8db680df1280e0f6aa64d43b03 Mon Sep 17 00:00:00 2001 From: hannah Date: Thu, 10 Nov 2022 21:10:06 -0500 Subject: [PATCH 2/2] change container new to support passing multiple artists --- lib/matplotlib/container.py | 9 ++++++++- lib/matplotlib/streamplot.py | 36 +++++++----------------------------- 2 files changed, 15 insertions(+), 30 deletions(-) diff --git a/lib/matplotlib/container.py b/lib/matplotlib/container.py index a58e55ca196c..21197f9faa12 100644 --- a/lib/matplotlib/container.py +++ b/lib/matplotlib/container.py @@ -1,3 +1,5 @@ +import collections + from matplotlib import cbook from matplotlib.artist import Artist @@ -15,7 +17,12 @@ def __repr__(self): .format(type(self).__name__, len(self))) def __new__(cls, *args, **kwargs): - return tuple.__new__(cls, args[0]) + if isinstance(args[0], collections.abc.Iterable): + iterable_of_artists = args[0] + else: + iterable_of_artists = (a for a in args if isinstance(a, Artist)) + + return tuple.__new__(cls, iterable_of_artists) def __init__(self, kl, label=None): self._callbacks = cbook.CallbackRegistry(signals=["pchanged"]) diff --git a/lib/matplotlib/streamplot.py b/lib/matplotlib/streamplot.py index b745537bd2d8..56ebb0ec3f33 100644 --- a/lib/matplotlib/streamplot.py +++ b/lib/matplotlib/streamplot.py @@ -230,51 +230,29 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None, axes.add_patch(p) axes.autoscale_view() - stream_container = StreamplotContainer((lc, ac)) - return stream_container + return StreamplotSet(lc, ac) -@_api.deprecated("major.minor", alternative="streamplot.StreamplotContainer") -class StreamplotSet: - def __init__(self, lines, arrows): - self.lines = lines - self.arrows = arrows - - -class StreamplotContainer(Container): +class StreamplotSet(Container): """ `~.Container` object for artists created in an `~.Axes.streamplot` plot. It can be treated like a namedtuple with fields ``(lines, arrows)`` - .. versionadded major.minor :: + .. versionchanged major.minor :: Attributes ---------- lines: `~.LineCollection` -` The collection of `.Line2d` segments that make up the streamlines +` collection of `.Line2d` segments that make up the streamlines arrows: `~.PatchCollection` - The collection of `.FancyArrow` arrows half-way along each streamline - - .. note:: - This container will probably change in the future to allow changes - to the colormap, alpha, etc. for both lines and arrows, but these - changes should be backward compatible. + collection of `.FancyArrow` arrows half-way along each streamline """ - def __init__(self, lines_arrows, **kwargs): - """ - Parameters - ---------- - lines_arrows : tuple - Tuple of (lines, arrows) - ``lines``: `.LineCollection` of streamlines. - ``arrows``: `.PatchCollection` of `.FancyArrowPatch` arrows - """ - lines, arrows = lines_arrows + def __init__(self, lines, arrows, **kwargs): self.lines = lines self.arrows = arrows - super().__init__(lines_arrows, **kwargs) + super().__init__(lines, arrows, **kwargs) # Coordinate definitions