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

Skip to content

Commit 1403a30

Browse files
committed
Deprecate BrokenBarHCollection.
It doesn't warrant a separate class; also, directly doing the unit conversion in broken_barh is actually easier (one doesn't need to go through _convert_dx, instead we directly move to using xmin/xmax).
1 parent ba7f3f0 commit 1403a30

File tree

4 files changed

+57
-67
lines changed

4 files changed

+57
-67
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
``BrokenBarHCollection`` is deprecated
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
It was just a thin wrapper inheriting from `.PolyCollection`;
4+
`~.Axes.broken_barh` has now been changed to return a `.PolyCollection`
5+
instead.
6+
7+
The ``BrokenBarHCollection.span_where`` helper is likewise deprecated; for the
8+
duration of the deprecation it has been moved to the parent `.PolyCollection`
9+
class. Use `~.Axes.fill_between` as a replacement; see
10+
:doc:`/gallery/lines_bars_and_markers/span_regions` for an example.
Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
11
"""
2-
========================================================================
3-
Shade regions defined by a logical mask using fill_between or span_where
4-
========================================================================
5-
6-
Shade regions where a logical mask is True with `.Axes.fill_between` or with
7-
`matplotlib.collections.BrokenBarHCollection.span_where`.
2+
==========================================================
3+
Shade regions defined by a logical mask using fill_between
4+
==========================================================
85
"""
96

107
import numpy as np
118
import matplotlib.pyplot as plt
12-
import matplotlib.collections as collections
139

1410

1511
t = np.arange(0.0, 2, 0.01)
1612
s = np.sin(2*np.pi*t)
1713

18-
fig, axs = plt.subplots(2, sharex=True, sharey=True)
19-
for ax in axs:
20-
ax.plot(t, s, color='black')
21-
ax.axhline(0, color='black')
14+
fig, ax = plt.subplots()
2215

23-
axs[0].set_title('using fill_between')
24-
axs[0].fill_between(t, 1, where=s > 0, facecolor='green', alpha=.5)
25-
axs[0].fill_between(t, -1, where=s < 0, facecolor='red', alpha=.5)
16+
ax.plot(t, s, color='black')
17+
ax.axhline(0, color='black')
2618

27-
axs[1].set_title('using span_where')
28-
axs[1].add_collection(collections.BrokenBarHCollection.span_where(
29-
t, ymin=0, ymax=1, where=s > 0, facecolor='green', alpha=0.5))
30-
axs[1].add_collection(collections.BrokenBarHCollection.span_where(
31-
t, ymin=-1, ymax=0, where=s < 0, facecolor='red', alpha=0.5))
19+
ax.fill_between(t, 1, where=s > 0, facecolor='green', alpha=.5)
20+
ax.fill_between(t, -1, where=s < 0, facecolor='red', alpha=.5)
3221

3322
plt.show()
3423

@@ -41,6 +30,3 @@
4130
# in this example:
4231
#
4332
# - `matplotlib.axes.Axes.fill_between`
44-
# - `matplotlib.collections.BrokenBarHCollection`
45-
# - `matplotlib.collections.BrokenBarHCollection.span_where`
46-
# - `matplotlib.axes.Axes.add_collection`

lib/matplotlib/axes/_axes.py

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2826,10 +2826,6 @@ def broken_barh(self, xranges, yrange, **kwargs):
28262826
A rectangle is drawn for each element of *xranges*. All rectangles
28272827
have the same vertical position and size defined by *yrange*.
28282828
2829-
This is a convenience function for instantiating a
2830-
`.BrokenBarHCollection`, adding it to the Axes and autoscaling the
2831-
view.
2832-
28332829
Parameters
28342830
----------
28352831
xranges : sequence of tuples (*xmin*, *xwidth*)
@@ -2841,13 +2837,13 @@ def broken_barh(self, xranges, yrange, **kwargs):
28412837
28422838
Returns
28432839
-------
2844-
`~.collections.BrokenBarHCollection`
2840+
`~.collections.PolyCollection`
28452841
28462842
Other Parameters
28472843
----------------
28482844
data : indexable object, optional
28492845
DATA_PARAMETER_PLACEHOLDER
2850-
**kwargs : `.BrokenBarHCollection` properties
2846+
**kwargs : `.PolyCollection` properties
28512847
28522848
Each *kwarg* can be either a single argument applying to all
28532849
rectangles, e.g.::
@@ -2862,32 +2858,28 @@ def broken_barh(self, xranges, yrange, **kwargs):
28622858
28632859
Supported keywords:
28642860
2865-
%(BrokenBarHCollection:kwdoc)s
2861+
%(PolyCollection:kwdoc)s
28662862
"""
28672863
# process the unit information
2868-
if len(xranges):
2869-
xdata = cbook._safe_first_finite(xranges)
2870-
else:
2871-
xdata = None
2872-
if len(yrange):
2873-
ydata = cbook._safe_first_finite(yrange)
2874-
else:
2875-
ydata = None
2864+
xdata = cbook._safe_first_finite(xranges) if len(xranges) else None
2865+
ydata = cbook._safe_first_finite(yrange) if len(yrange) else None
28762866
self._process_unit_info(
28772867
[("x", xdata), ("y", ydata)], kwargs, convert=False)
2878-
xranges_conv = []
2879-
for xr in xranges:
2880-
if len(xr) != 2:
2881-
raise ValueError('each range in xrange must be a sequence '
2882-
'with two elements (i.e. an Nx2 array)')
2883-
# convert the absolute values, not the x and dx...
2884-
x_conv = np.asarray(self.convert_xunits(xr[0]))
2885-
x1 = self._convert_dx(xr[1], xr[0], x_conv, self.convert_xunits)
2886-
xranges_conv.append((x_conv, x1))
2887-
2888-
yrange_conv = self.convert_yunits(yrange)
2889-
2890-
col = mcoll.BrokenBarHCollection(xranges_conv, yrange_conv, **kwargs)
2868+
2869+
vertices = []
2870+
y0, dy = yrange
2871+
y0, y1 = self.convert_yunits((y0, y0 + dy))
2872+
for xr in xranges: # convert the absolute values, not the x and dx
2873+
try:
2874+
x0, dx = xr
2875+
except Exception:
2876+
raise ValueError(
2877+
"each range in xrange must be a sequence with two "
2878+
"elements (i.e. xrange must be an (N, 2) array)") from None
2879+
x0, x1 = self.convert_xunits((x0, x0 + dx))
2880+
vertices.append([(x0, y0), (x0, y1), (x1, y1), (x1, y0)])
2881+
2882+
col = mcoll.PolyCollection(np.array(vertices), **kwargs)
28912883
self.add_collection(col, autolim=True)
28922884
self._request_autoscale_view()
28932885

lib/matplotlib/collections.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,26 @@ def set_verts_and_codes(self, verts, codes):
12251225
for xy, cds in zip(verts, codes)]
12261226
self.stale = True
12271227

1228+
@classmethod
1229+
@_api.deprecated("3.7", alternative="fill_between")
1230+
def span_where(cls, x, ymin, ymax, where, **kwargs):
1231+
"""
1232+
Return a `.BrokenBarHCollection` that plots horizontal bars from
1233+
over the regions in *x* where *where* is True. The bars range
1234+
on the y-axis from *ymin* to *ymax*
1235+
1236+
*kwargs* are passed on to the collection.
1237+
"""
1238+
xranges = []
1239+
for ind0, ind1 in cbook.contiguous_regions(where):
1240+
xslice = x[ind0:ind1]
1241+
if not len(xslice):
1242+
continue
1243+
xranges.append((xslice[0], xslice[-1] - xslice[0]))
1244+
return BrokenBarHCollection(xranges, [ymin, ymax - ymin], **kwargs)
1245+
12281246

1247+
@_api.deprecated("3.7")
12291248
class BrokenBarHCollection(PolyCollection):
12301249
"""
12311250
A collection of horizontal bars spanning *yrange* with a sequence of
@@ -1251,23 +1270,6 @@ def __init__(self, xranges, yrange, **kwargs):
12511270
(xmin, ymin)] for xmin, xwidth in xranges]
12521271
super().__init__(verts, **kwargs)
12531272

1254-
@classmethod
1255-
def span_where(cls, x, ymin, ymax, where, **kwargs):
1256-
"""
1257-
Return a `.BrokenBarHCollection` that plots horizontal bars from
1258-
over the regions in *x* where *where* is True. The bars range
1259-
on the y-axis from *ymin* to *ymax*
1260-
1261-
*kwargs* are passed on to the collection.
1262-
"""
1263-
xranges = []
1264-
for ind0, ind1 in cbook.contiguous_regions(where):
1265-
xslice = x[ind0:ind1]
1266-
if not len(xslice):
1267-
continue
1268-
xranges.append((xslice[0], xslice[-1] - xslice[0]))
1269-
return cls(xranges, [ymin, ymax - ymin], **kwargs)
1270-
12711273

12721274
class RegularPolyCollection(_CollectionWithSizes):
12731275
"""A collection of n-sided regular polygons."""

0 commit comments

Comments
 (0)