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

Skip to content

Commit d5e1218

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 49df465 commit d5e1218

File tree

11 files changed

+6139
-5290
lines changed

11 files changed

+6139
-5290
lines changed

lib/matplotlib/axes/_base.py

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

1313
import matplotlib as mpl
14+
import matplotlib.artist as martist
15+
import matplotlib.axis as maxis
1416
from matplotlib import cbook
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+
from matplotlib import docstring
21+
import matplotlib.font_manager as font_manager
22+
import matplotlib.image as mimage
1823
import matplotlib.lines as mlines
1924
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
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-
from matplotlib.rcsetup import cycler, validate_axisbelow
29+
import matplotlib.ticker as mticker
30+
import matplotlib.transforms as mtransforms
3031

3132
_log = logging.getLogger(__name__)
3233

@@ -1110,7 +1111,6 @@ def cla(self):
11101111
self.child_axes = []
11111112
self._current_image = None # strictly for pyplot via _sci, _gci
11121113
self.legend_ = None
1113-
self.collections = [] # collection.Collection instances
11141114
self.containers = []
11151115

11161116
self.grid(False) # Disable grid on init to use rcParameter
@@ -1178,6 +1178,11 @@ def cla(self):
11781178

11791179
self.stale = True
11801180

1181+
@property
1182+
def collections(self):
1183+
return tuple(a for a in self._children
1184+
if isinstance(a, mcoll.Collection))
1185+
11811186
@property
11821187
def images(self):
11831188
return tuple(a for a in self._children
@@ -1948,13 +1953,13 @@ def add_child_axes(self, ax):
19481953

19491954
def add_collection(self, collection, autolim=True):
19501955
"""
1951-
Add a `~.Collection` to the axes' collections; return the collection.
1956+
Add a `~.Collection` to the Axes; return the collection.
19521957
"""
19531958
label = collection.get_label()
19541959
if not label:
1955-
collection.set_label('_collection%d' % len(self.collections))
1956-
self.collections.append(collection)
1957-
collection._remove_method = self.collections.remove
1960+
collection.set_label(f'_collection{len(self._children)}')
1961+
self._children.append(collection)
1962+
collection._remove_method = self._children.remove
19581963
self._set_artist_props(collection)
19591964

19601965
if collection.get_clip_path() is None:
@@ -4077,7 +4082,6 @@ def format_deltas(key, dx, dy):
40774082
def get_children(self):
40784083
# docstring inherited.
40794084
return [
4080-
*self.collections,
40814085
*self._children,
40824086
*self.artists,
40834087
*self.spines.values(),

lib/matplotlib/streamplot.py

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

212212
p = patches.FancyArrowPatch(
213213
arrow_tail, arrow_head, transform=transform, **arrow_kw)
214-
axes.add_patch(p)
215214
arrows.append(p)
216215

217216
lc = mcollections.LineCollection(
@@ -223,9 +222,13 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
223222
lc.set_cmap(cmap)
224223
lc.set_norm(norm)
225224
axes.add_collection(lc)
226-
axes.autoscale_view()
227225

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

Binary file not shown.
Loading

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

Lines changed: 326 additions & 310 deletions
Loading
Binary file not shown.

0 commit comments

Comments
 (0)