Skip to content

Commit 7fa3739

Browse files
committed
Move Axes.collections into hidden children attribute.
The collections can still be accessed via a read-only property, but now are combined with images, lines, patches, tables and texts for sorting and drawing purposes. There was a quite a bit more fallout from this one: * `streamplot` had to add the arrows after the line collection, so they would remain on top. Because `FancyArrowPatch` is created in display coordinates, this meant that their limits changed slightly, as adding the line collection would have re-adjusted the `Axes.transData`. * The `test_pre_transform_plotting` image needed to be regenerated because of the above limit change. I took this opportunity to clean up some of the previous keep-image-the-same hacks. * The `test_arrow_contains_point` test image now has 'contained' scatter points correctly over the arrows. * Tests in `test_patches.py` needed to be re-ordered slightly as they were checking alpha between collections and non-collection artists. * The `test_fancy` legend images needed to be regenerated because errorbars are correctly drawn as a group now.
1 parent 278de8f commit 7fa3739

File tree

12 files changed

+6142
-5294
lines changed

12 files changed

+6142
-5294
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@
1111
import numpy as np
1212

1313
import matplotlib as mpl
14-
from matplotlib import _api, cbook
14+
from matplotlib import _api, cbook, docstring
15+
import matplotlib.artist as martist
16+
import matplotlib.axis as maxis
1517
from matplotlib.cbook import _OrderedSet, _check_1d, index_of
16-
from matplotlib import docstring
18+
import matplotlib.collections as mcoll
1719
import matplotlib.colors as mcolors
20+
import matplotlib.font_manager as font_manager
21+
import matplotlib.image as mimage
1822
import matplotlib.lines as mlines
1923
import matplotlib.patches as mpatches
20-
import matplotlib.artist as martist
21-
import matplotlib.transforms as mtransforms
22-
import matplotlib.ticker as mticker
23-
import matplotlib.axis as maxis
24+
import matplotlib.path as mpath
25+
from matplotlib.rcsetup import cycler, validate_axisbelow
2426
import matplotlib.spines as mspines
25-
import matplotlib.font_manager as font_manager
2627
import matplotlib.table as mtable
2728
import matplotlib.text as mtext
28-
import matplotlib.image as mimage
29-
import matplotlib.path as mpath
30-
from matplotlib.rcsetup import cycler, validate_axisbelow
29+
import matplotlib.ticker as mticker
30+
import matplotlib.transforms as mtransforms
3131

3232
_log = logging.getLogger(__name__)
3333

@@ -1238,7 +1238,6 @@ def cla(self):
12381238
self._current_image = None # strictly for pyplot via _sci, _gci
12391239
self._projection_init = None # strictly for pyplot.subplot
12401240
self.legend_ = None
1241-
self.collections = [] # collection.Collection instances
12421241
self.containers = []
12431242

12441243
self.grid(False) # Disable grid on init to use rcParameter
@@ -1306,6 +1305,11 @@ def cla(self):
13061305

13071306
self.stale = True
13081307

1308+
@property
1309+
def collections(self):
1310+
return tuple(a for a in self._children
1311+
if isinstance(a, mcoll.Collection))
1312+
13091313
@property
13101314
def images(self):
13111315
return tuple(a for a in self._children
@@ -2069,13 +2073,13 @@ def add_child_axes(self, ax):
20692073

20702074
def add_collection(self, collection, autolim=True):
20712075
"""
2072-
Add a `~.Collection` to the axes' collections; return the collection.
2076+
Add a `~.Collection` to the Axes; return the collection.
20732077
"""
20742078
label = collection.get_label()
20752079
if not label:
2076-
collection.set_label('_collection%d' % len(self.collections))
2077-
self.collections.append(collection)
2078-
collection._remove_method = self.collections.remove
2080+
collection.set_label(f'_child{len(self._children)}')
2081+
self._children.append(collection)
2082+
collection._remove_method = self._children.remove
20792083
self._set_artist_props(collection)
20802084

20812085
if collection.get_clip_path() is None:
@@ -4346,7 +4350,6 @@ def format_deltas(key, dx, dy):
43464350
def get_children(self):
43474351
# docstring inherited.
43484352
return [
4349-
*self.collections,
43504353
*self._children,
43514354
*self.artists,
43524355
*self.spines.values(),

lib/matplotlib/streamplot.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
210210

211211
p = patches.FancyArrowPatch(
212212
arrow_tail, arrow_head, transform=transform, **arrow_kw)
213-
axes.add_patch(p)
214213
arrows.append(p)
215214

216215
lc = mcollections.LineCollection(
@@ -222,9 +221,13 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
222221
lc.set_cmap(cmap)
223222
lc.set_norm(norm)
224223
axes.add_collection(lc)
225-
axes.autoscale_view()
226224

227225
ac = matplotlib.collections.PatchCollection(arrows)
226+
# Adding the collection itself is broken; see #2341.
227+
for p in arrows:
228+
axes.add_patch(p)
229+
230+
axes.autoscale_view()
228231
stream_container = StreamplotSet(lc, ac)
229232
return stream_container
230233

54 Bytes
Binary file not shown.
52 Bytes
Loading

lib/matplotlib/tests/baseline_images/test_legend/fancy.svg

Lines changed: 326 additions & 310 deletions
Loading
103 Bytes
Loading
Binary file not shown.
6.14 KB
Loading

0 commit comments

Comments
 (0)