Skip to content

Changed the return type of streamplot to a container object #24388

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/matplotlib/container.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import collections

from matplotlib import cbook
from matplotlib.artist import Artist

Expand All @@ -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"])
Expand Down
36 changes: 22 additions & 14 deletions lib/matplotlib/streamplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -237,20 +230,35 @@ 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)
return stream_container
return StreamplotSet(lc, ac)


class StreamplotSet(Container):
"""
`~.Container` object for artists created in an `~.Axes.streamplot` plot.

It can be treated like a namedtuple with fields ``(lines, arrows)``

class StreamplotSet:
.. versionchanged major.minor ::

def __init__(self, lines, arrows):
Attributes
----------
lines: `~.LineCollection`
` collection of `.Line2d` segments that make up the streamlines
arrows: `~.PatchCollection`
collection of `.FancyArrow` arrows half-way along each streamline
"""

def __init__(self, lines, arrows, **kwargs):
self.lines = lines
self.arrows = arrows
super().__init__(lines, arrows, **kwargs)


# Coordinate definitions
# ========================


class DomainMap:
"""
Map representing different coordinate systems.
Expand Down