Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e98db08

Browse files
committed
DOCS: @timhoffm collections.py suggestions
1 parent f87cc48 commit e98db08

File tree

1 file changed

+102
-87
lines changed

1 file changed

+102
-87
lines changed

lib/matplotlib/collections.py

Lines changed: 102 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,36 @@
2727
"linewidth": ["linewidths", "lw"],
2828
})
2929
class Collection(artist.Artist, cm.ScalarMappable):
30-
"""
30+
r"""
3131
Base class for Collections. Must be subclassed to be usable.
3232
33-
A Collection represents a sequence of `~.patches.Patch` objects that the
34-
library believes would be more efficient to draw together than
35-
individually. For example, when a single path is being drawn repeatedly at
36-
different offsets, the renderer can typically execute a `draw_marker` call
37-
much more efficiently than a series of repeated calls to `draw_path` with
38-
the offsets put in one-by-one.
33+
A Collection represents a sequence of `~.patches.Patch`\s that can be drawn
34+
more efficiently together than individually. For example, when a single
35+
path is being drawn repeatedly at different offsets, the renderer can
36+
typically execute a ``draw_marker()`` call much more efficiently than a
37+
series of repeated calls to ``draw_path()`` with the offsets put in
38+
one-by-one.
39+
40+
Most properties of a collection can be configured per-element. Therefore,
41+
Collections have "plural" versions of many of the properties of a
42+
`~.patches.Patch` (e.g. `.Collection.get_paths` instead of
43+
`~.patches.Patch.get_path`). Exceptions are the *zorder*, *hatch*,
44+
*pickradius*, *capstyle* and *joinstyle* properties, which can only be set
45+
globally for the whole collection.
3946
40-
All properties in a collection can be either sequences or scalars; if
41-
scalars, they will be converted to sequences. The property of the ith
42-
element of the collection is::
47+
Besides these exceptions, all properties can be specified as single values
48+
(applying to all elements) or sequences of values. The property of the
49+
``i``th element of the collection is::
4350
44-
prop[i % len(props)]
51+
prop[i % len(prop)]
4552
46-
Because of this, Collections have "plural" versions of many of the
47-
properties of a `~.patches.Patch` (e.g. `Collection.get_paths` instead of
48-
`~.patches.Patch.get_path`). Exceptions are the *hatch*, *pickradius*,
49-
*capstyle* and *joinstyle* properties, these can only be set globally for
50-
the whole collection.
5153
5254
Each Collection can optionally be used as its own `~.cm.ScalarMappable` by
5355
passing the *norm* and *cmap* parameters to its constructor. If the
5456
Collection's `~.cm.ScalarMappable` matrix ``_A`` has been set (via a call
55-
to `Collection.set_array`), then at draw time this internal scalar mappable
56-
will be used to set the ``facecolors`` and ``edgecolors``, ignoring those
57-
that were manually passed in.
57+
to `.Collection.set_array`), then at draw time this internal scalar
58+
mappable will be used to set the ``facecolors`` and ``edgecolors``,
59+
ignoring those that were manually passed in.
5860
"""
5961
_offsets = np.zeros((0, 2))
6062
_transOffset = transforms.IdentityTransform()
@@ -91,13 +93,19 @@ def __init__(self,
9193
**kwargs
9294
):
9395
"""
96+
For all "plural" parameters, the type listed is the expected type when
97+
passing a single value to be used for the whole collection. For any of
98+
these inputs, a Sequence of the specified type is also allowed.
99+
94100
Parameters
95101
----------
96-
edgecolors : default: :rc:`patch.edgecolor`
97-
Edge color for each patch making up the collection.
98-
facecolors : default: :rc:`patch.facecolor`
102+
edgecolors : color, default: :rc:`patch.edgecolor`
103+
Edge color for each patch making up the collection. The special
104+
value 'face' can be passed to make the edgecolor match the
105+
facecolor.
106+
facecolors : color, default: :rc:`patch.facecolor`
99107
Face color for each patch making up the collection.
100-
linewidths : default: :rc:`patch.linewidth`
108+
linewidths : float, default: :rc:`patch.linewidth`
101109
Line width for each patch making up the collection.
102110
linestyles : str or tuple, default: 'solid'
103111
Valid strings are ['solid', 'dashed', 'dashdot', 'dotted', '-',
@@ -108,14 +116,14 @@ def __init__(self,
108116
where *onoffseq* is an even length tuple of on and off ink lengths
109117
in points. For examples, see
110118
:doc:`/gallery/lines_bars_and_markers/linestyles`.
111-
capstyle : default: :rc:`patch.capstyle`
119+
capstyle : str, default: :rc:`patch.capstyle`
112120
Style to use for capping lines for all paths in the collection.
113-
joinstyle : default: :rc:`patch.joinstyle`
121+
joinstyle : str, default: :rc:`patch.joinstyle`
114122
Style to use for joining lines for all paths in the collection.
115-
antialiaseds : default: :rc:`patch.antialiased`
123+
antialiaseds : bool, default: :rc:`patch.antialiased`
116124
Whether each pach in the collection should be drawn with
117125
antialiasing.
118-
offsets : Tuple[float, float], default: (0, 0)
126+
offsets : (float, float), default: (0, 0)
119127
A vector by which to translate each patch after rendering (default
120128
is no translation). The translation is performed in screen (pixel)
121129
coordinates (i.e. after the Artist's transform is applied).
@@ -126,25 +134,25 @@ def __init__(self,
126134
If set to 'data' (deprecated), *offsets* will be treated as if it
127135
is in data coordinates instead of in screen coordinates.
128136
norm : `~.colors.Normalize`, optional, default: None
129-
Forwarded to `~.cm.ScalarMappable.__init__`. The default of
137+
Forwarded to `~.cm.ScalarMappable`. The default of
130138
``None`` means that the first draw call will set ``vmin`` and
131139
``vmax`` using the minimum and maximum values of the data.
132140
cmap : `~.colors.Colormap`, optional, default: None
133-
Forwarded to `~.cm.ScalarMappable.__init__`. The default of
141+
Forwarded to `~.cm.ScalarMappable`. The default of
134142
``None`` will result in :rc:`image.cmap` being used.
135-
hatch : Optional[str], default: None
143+
hatch : str, optional, default: None
136144
Hatching pattern to use in filled paths, if any. Valid strings are
137145
['/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*']. See
138146
:doc:`/gallery/shapes_and_collections/hatch_demo` for the meaning
139147
of each hatch type.
140-
pickradius : float, default: 5
141-
If ``pickradius <= 0``, then `Collection.contains` will return
148+
pickradius : float, default: 5.0
149+
If ``pickradius <= 0``, then `.Collection.contains` will return
142150
``True`` whenever the test point is inside of one of the polygons
143151
formed by the control points of a Path in the Collection. On the
144152
other hand, if it is greater than 0, then we instead check if the
145153
test point is contained in a stroke of width ``2*pickradius``
146154
following any of the Paths in the Collection.
147-
urls : str or Sequence[str], default: None
155+
urls : str, default: None
148156
A URL for each patch to link to once drawn. Currently only works
149157
for the SVG backend. See :doc:`/gallery/misc/hyperlinks_sgskip` for
150158
examples.
@@ -223,24 +231,21 @@ def get_offset_transform(self):
223231
return t
224232

225233
def get_datalim(self, transData):
226-
"""
227-
Get the automatic datalim of the collection.
228-
229-
This operation depends on the transforms for the data in the
230-
collection and whether the collection has offsets.
231-
232-
1. offsets = None, transform child of transData: use the paths for
233-
the automatic limits (i.e. for LineCollection in streamline).
234-
2. offsets != None: offset_transform is child of transData:
235-
236-
a. transform is child of transData: use the path + offset for
237-
limits (i.e for bar).
238-
b. transform is not a child of transData: just use the offsets
239-
for the limits (i.e. for scatter)
240-
241-
3. otherwise return a null Bbox.
242-
243-
"""
234+
# Calculate the data limits and return them as a `.Bbox`.
235+
#
236+
# This operation depends on the transforms for the data in the
237+
# collection and whether the collection has offsets:
238+
#
239+
# 1. offsets = None, transform child of transData: use the paths for
240+
# the automatic limits (i.e. for LineCollection in streamline).
241+
# 2. offsets != None: offset_transform is child of transData:
242+
#
243+
# a. transform is child of transData: use the path + offset for
244+
# limits (i.e for bar).
245+
# b. transform is not a child of transData: just use the offsets
246+
# for the limits (i.e. for scatter)
247+
#
248+
# 3. otherwise return a null Bbox.
244249

245250
transform = self.get_transform()
246251
transOffset = self.get_offset_transform()
@@ -439,10 +444,11 @@ def contains(self, mouseevent):
439444
if not self.get_visible():
440445
return False, {}
441446

442-
if isinstance(self._picker, Number) and self._picker is not True:
443-
pickradius = float(self._picker) # Artist._picker not just a bool
444-
else:
445-
pickradius = self._pickradius
447+
pickradius = (
448+
float(self._picker)
449+
if isinstance(self._picker, Number) and
450+
self._picker is not True # the bool, not just nonzero or 1
451+
else self._pickradius)
446452

447453
if self.axes:
448454
self.axes._unstale_viewLim()
@@ -466,21 +472,22 @@ def set_urls(self, urls):
466472
"""
467473
Parameters
468474
----------
469-
urls : List[str] or None
475+
urls : list of str or None
470476
471477
Notes
472478
-----
473-
Currently only works for the SVG backend.
479+
URLs are currently only implemented by the SVG backend. They are
480+
ignored by all other backends.
474481
"""
475482
self._urls = urls if urls is not None else [None]
476483
self.stale = True
477484

478485
def get_urls(self):
479486
"""
480-
Returns
481-
-------
482-
The URL that each patch should link to once drawn. See
483-
:doc:`/gallery/misc/hyperlinks_sgskip` for a examples.
487+
Return a list of URLs, one for each element of the collection.
488+
489+
The list contains *None* for elements without a URL. See
490+
:doc:`/gallery/misc/hyperlinks_sgskip` for an example.
484491
"""
485492
return self._urls
486493

@@ -925,21 +932,21 @@ def draw(self, renderer):
925932

926933
class PathCollection(_CollectionWithSizes):
927934
"""
928-
The most basic `Collection` subclass, created e.g. by `~.Axes.scatter`.
935+
The most basic `.Collection` subclass, created e.g. by `~.Axes.scatter`.
929936
"""
930937

931938
def __init__(self, paths, sizes=None, **kwargs):
932939
"""
933940
Parameters
934941
----------
935-
paths : Sequence[matplotlib.path.Path]
936-
The paths that will make up the `Collection`.
937-
sizes : Sequence[float]
938-
Scalar by which to scale each drawn `~.path.Path`. One unit squared
939-
in the Path's data space is scaled to be ``sizes**2`` points when
940-
rendered.
942+
paths : list of matplotlib.path.Path
943+
The paths that will make up the `.Collection`.
944+
sizes : array-like of float
945+
The factor by which to scale each drawn `~.path.Path`. One unit
946+
squared in the Path's data space is scaled to be ``sizes**2``
947+
points when rendered.
941948
**kwargs
942-
Forwarded to `Collection`.
949+
Forwarded to `.Collection`.
943950
"""
944951

945952
super().__init__(**kwargs)
@@ -959,17 +966,17 @@ def legend_elements(self, prop="colors", num="auto",
959966
"""
960967
Create legend handles and labels for a PathCollection.
961968
962-
Each legend handle is just a `.Line2D` representing the Path that was
963-
drawn, and each label is just a string what each Path represents.
969+
Each legend handle is a `.Line2D` representing the Path that was drawn,
970+
and each label is a string what each Path represents.
964971
965972
This is useful for obtaining a legend for a `~.Axes.scatter` plot;
966973
e.g.::
967974
968975
scatter = plt.scatter([1, 2, 3], [4, 5, 6], c=[7, 2, 3])
969976
plt.legend(*scatter.legend_elements())
970977
971-
would create three legend elements, one for each color with the
972-
numerical values passed to *c* as the labels.
978+
creates three legend elements, one for each color with the numerical
979+
values passed to *c* as the labels.
973980
974981
Also see the :ref:`automatedlegendcreation` example.
975982
@@ -1109,7 +1116,7 @@ def __init__(self, verts, sizes=None, closed=True, **kwargs):
11091116
Whether the polygon should be closed by adding a CLOSEPOLY
11101117
connection at the end.
11111118
**kwargs
1112-
Forwarded to `Collection`.
1119+
Forwarded to `.Collection`.
11131120
"""
11141121
Collection.__init__(self, **kwargs)
11151122
self.set_sizes(sizes)
@@ -1192,7 +1199,7 @@ def __init__(self, xranges, yrange, **kwargs):
11921199
yrange : (float, float)
11931200
The (lower-edge, height) common to all bars.
11941201
**kwargs
1195-
Forwarded to `Collection`.
1202+
Forwarded to `.Collection`.
11961203
"""
11971204
ymin, ywidth = yrange
11981205
ymax = ymin + ywidth
@@ -1206,7 +1213,7 @@ def __init__(self, xranges, yrange, **kwargs):
12061213
@classmethod
12071214
def span_where(cls, x, ymin, ymax, where, **kwargs):
12081215
"""
1209-
Return a `BrokenBarHCollection` that plots horizontal bars from
1216+
Return a `.BrokenBarHCollection` that plots horizontal bars from
12101217
over the regions in *x* where *where* is True. The bars range
12111218
on the y-axis from *ymin* to *ymax*
12121219
@@ -1242,7 +1249,7 @@ def __init__(self,
12421249
sizes : tuple of float
12431250
The area of the circle circumscribing the polygon in points^2.
12441251
**kwargs
1245-
Forwarded to `Collection`.
1252+
Forwarded to `.Collection`.
12461253
12471254
Examples
12481255
--------
@@ -1296,7 +1303,7 @@ class AsteriskPolygonCollection(RegularPolyCollection):
12961303

12971304
class LineCollection(Collection):
12981305
"""
1299-
A collection of 2D lines, where each line can be many segments long.
1306+
Collection of 2D lines. Each line may have an arbitrary number of segments.
13001307
13011308
Relevant parameters default to their ``'lines'`` values in
13021309
`~.matplotlib.rcParams` instead of their ``'patch'`` values.
@@ -1319,6 +1326,13 @@ def __init__(self, segments, # Can be None.
13191326
**kwargs
13201327
):
13211328
"""
1329+
Except for *segments*, the type listed below is the expected type when
1330+
passing a single value to be used for the whole collection. A Sequence
1331+
of the specified type is also allowed, in which case the property for
1332+
the ``i``th element of the collection will be::
1333+
1334+
prop[i % len(prop)]
1335+
13221336
Parameters
13231337
----------
13241338
segments
@@ -1328,9 +1342,9 @@ def __init__(self, segments, # Can be None.
13281342
13291343
or the equivalent numpy array with two columns. Each line
13301344
can have a different number of segments.
1331-
linewidths : default: :rc:`lines.linewidth`
1345+
linewidths : float, default: :rc:`lines.linewidth`
13321346
The width of each line in points.
1333-
colors : default: :rc:`lines.color`
1347+
colors : color, default: :rc:`lines.color`
13341348
A sequence of RGBA tuples (e.g., arbitrary color strings, etc, not
13351349
allowed).
13361350
antialiaseds : default: :rc:`lines.antialiased`
@@ -1343,10 +1357,11 @@ def __init__(self, segments, # Can be None.
13431357
"filled in" as if there was an implicit line segment joining the
13441358
last and first points of that line back around to each other. In
13451359
order to manually specify what should count as the "interior" of
1346-
each line, please use `PathCollection` instead, where the "interior"
1347-
can be specified by appropriate usage of `~.path.Path.CLOSEPOLY`.
1360+
each line, please use `.PathCollection` instead, where the
1361+
"interior" can be specified by appropriate usage of
1362+
`~.path.Path.CLOSEPOLY`.
13481363
**kwargs
1349-
Forwareded to `Collection`.
1364+
Forwareded to `.Collection`.
13501365
"""
13511366
if colors is None:
13521367
colors = mpl.rcParams['lines.color']
@@ -1492,7 +1507,7 @@ def __init__(self,
14921507
antialiased : bool, default: :rc:`lines.antialiased`
14931508
Whether to use antialiasing for drawing the lines.
14941509
**kwargs
1495-
Forwarded to :class:`~matplotlib.collections.LineCollection`.
1510+
Forwarded to `.LineCollection`.
14961511
14971512
Examples
14981513
--------
@@ -1650,10 +1665,10 @@ def __init__(self, sizes, **kwargs):
16501665
"""
16511666
Parameters
16521667
----------
1653-
sizes : float or Sequence[float]
1668+
sizes : float or array-like
16541669
Gives the area of each circle in points^2.
16551670
**kwargs
1656-
Forwarded to `Collection`.
1671+
Forwarded to `.Collection`.
16571672
"""
16581673
Collection.__init__(self, **kwargs)
16591674
self.set_sizes(sizes)
@@ -1687,7 +1702,7 @@ def __init__(self, widths, heights, angles, units='points', **kwargs):
16871702
it behaves the same as the `~matplotlib.patches.Ellipse` with
16881703
``axes.transData`` as its transform.
16891704
**kwargs
1690-
Forwarded to :class:`Collection`.
1705+
Forwarded to `Collection`.
16911706
"""
16921707
Collection.__init__(self, **kwargs)
16931708
self._widths = 0.5 * np.asarray(widths).ravel()

0 commit comments

Comments
 (0)