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

Skip to content

Commit d2a12cb

Browse files
authored
Merge pull request #13656 from anntzer/hist-return
For single datasets, don't wrap artist added by Axes.hist in silent_list
2 parents cb34934 + 8995e41 commit d2a12cb

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

doc/api/api_changes_3.3/behaviour.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,36 @@ also be 'x' or 'y'.
241241
This newly allowed value for :rc:`savefig.facecolor` and :rc:`savefig.edgecolor`,
242242
as well as the *facecolor* and *edgecolor* parameters to `.Figure.savefig`, means
243243
"use whatever facecolor and edgecolor the figure current has".
244+
245+
When using a single dataset, `.Axes.hist` no longer wraps the added artist in a `.silent_list`
246+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
247+
248+
When `.Axes.hist` is called with a single dataset, it adds to the axes either
249+
a `.BarContainer` object (when ``histtype="bar"`` or ``"barstacked"``), or a
250+
`.Polygon` object (when ``histype="step"`` or ``"stepfilled"``) -- the latter
251+
being wrapped in a list-of-one-element. Previously, either artist would be
252+
wrapped in a `.silent_list`. This is no longer the case: the `.BarContainer` is
253+
now returned as is (this is an API breaking change if you were directly relying
254+
on the concrete `list` API; however, `.BarContainer` inherits from `tuple` so
255+
most common operations remain available), and the list-of-one `.Polygon` is
256+
returned as is. This makes the `repr` of the returned artist more accurate: it
257+
is now ::
258+
259+
<BarContainer object of of 10 artists> # "bar", "barstacked"
260+
[<matplotlib.patches.Polygon object at 0xdeadbeef>] # "step", "stepfilled"
261+
262+
instead of ::
263+
264+
<a list of 10 Patch objects> # "bar", "barstacked"
265+
<a list of 1 Patch objects> # "step", "stepfilled"
266+
267+
When `.Axes.hist` is called with multiple artists, it still wraps its return
268+
value in a `.silent_list`, but uses more accurate type information ::
269+
270+
<a list of 3 BarContainer objects> # "bar", "barstacked"
271+
<a list of 3 List[Polygon] objects> # "step", "stepfilled"
272+
273+
instead of ::
274+
275+
<a list of 3 Lists of Patches objects> # "bar", "barstacked"
276+
<a list of 3 Lists of Patches objects> # "step", "stepfilled"

lib/matplotlib/axes/_axes.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6514,9 +6514,10 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
65146514
edge of last bin). Always a single array even when multiple data
65156515
sets are passed in.
65166516
6517-
patches : list or list of lists
6518-
Silent list of individual patches used to create the histogram
6519-
or list of such list if multiple input datasets.
6517+
patches : `.BarContainer` or list of a single `.Polygon` or list of \
6518+
such objects
6519+
Container of individual artists used to create the histogram
6520+
or list of such containers if there are multiple input datasets.
65206521
65216522
Other Parameters
65226523
----------------
@@ -6798,9 +6799,11 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
67986799
p.set_label('_nolegend_')
67996800

68006801
if nx == 1:
6801-
return tops[0], bins, cbook.silent_list('Patch', patches[0])
6802+
return tops[0], bins, patches[0]
68026803
else:
6803-
return tops, bins, cbook.silent_list('Lists of Patches', patches)
6804+
patch_type = ("BarContainer" if histtype.startswith("bar")
6805+
else "List[Polygon]")
6806+
return tops, bins, cbook.silent_list(patch_type, patches)
68046807

68056808
@_preprocess_data(replace_names=["x", "y", "weights"])
68066809
@docstring.dedent_interpd

0 commit comments

Comments
 (0)