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

Skip to content

Commit 468038b

Browse files
committed
feat: StepPolyCollection
1 parent 786d0db commit 468038b

File tree

5 files changed

+102
-9
lines changed

5 files changed

+102
-9
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6914,6 +6914,34 @@ def histline(self, vals, bins=None, *,
69146914
return patches
69156915

69166916

6917+
def histlinealt(self, vals, bins=None, *,
6918+
orientation='horizontal', baseline=0, fill=False, **kwargs):
6919+
6920+
if 'color' not in kwargs:
6921+
kwargs['color'] = self._get_lines.get_next_color()
6922+
6923+
if bins is None:
6924+
bins = np.arange(len(vals)+1)
6925+
6926+
pcoll = mcoll.StepPolyCollection(vals,
6927+
bins,
6928+
baseline=baseline,
6929+
orientation=orientation,
6930+
fill=fill,
6931+
**kwargs)
6932+
6933+
self.add_collection(pcoll)
6934+
6935+
if baseline is None:
6936+
baseline = 0
6937+
if orientation == 'horizontal':
6938+
pcoll.sticky_edges.y.append(baseline)
6939+
else:
6940+
pcoll.sticky_edges.x.append(baseline)
6941+
6942+
self._request_autoscale_view()
6943+
return pcoll
6944+
69176945
@_preprocess_data(replace_names=["x", "y", "weights"])
69186946
@docstring.dedent_interpd
69196947
def hist2d(self, x, y, bins=10, range=None, density=False, weights=None,

lib/matplotlib/collections.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,58 @@ def set_verts_and_codes(self, verts, codes):
11911191
self.stale = True
11921192

11931193

1194+
class StepPolyCollection(PolyCollection):
1195+
def __init__(self, vals, bins=None, *, fill=False,
1196+
orientation='horizontal', baseline=0, **kwargs):
1197+
self.fill = fill
1198+
self._facecolor = None
1199+
self._edgecolor = None
1200+
if not fill:
1201+
self._facecolor = "none"
1202+
self._edgecolor = kwargs.pop('color', None)
1203+
self.baseline = baseline
1204+
self.orientation = orientation
1205+
self._bins = bins
1206+
self._vals = vals
1207+
verts = self._update_data()
1208+
super().__init__(verts,
1209+
facecolor=self._facecolor,
1210+
edgecolor=self._edgecolor,
1211+
closed=False,
1212+
**kwargs)
1213+
1214+
def _update_data(self):
1215+
if self._bins.size - 1 != self._vals.size:
1216+
raise ValueError('the length of the bins is wrong')
1217+
verts = []
1218+
for idx0, idx1 in cbook.contiguous_regions(~np.isnan(self._vals)):
1219+
x = np.vstack((self._bins[idx0:idx1+1], self._bins[idx0:idx1+1])).T.flatten()
1220+
y = np.vstack((self._vals[idx0:idx1], self._vals[idx0:idx1])).T.flatten()
1221+
if self.baseline is not None:
1222+
y = np.hstack((self.baseline, y, self.baseline))
1223+
else:
1224+
y = np.hstack((y[0], y, y[-1]))
1225+
if self.orientation == 'horizontal':
1226+
xy = np.vstack([x, y]).T
1227+
else:
1228+
xy = np.vstack([y, x]).T
1229+
verts.append(xy)
1230+
return verts
1231+
1232+
def set_bins(self, bins):
1233+
self._bins = bins
1234+
self._update_data()
1235+
1236+
def set_vals(self, vals):
1237+
self._vals = vals
1238+
self._update_data()
1239+
1240+
def set_vals_bins(self, vals, bins):
1241+
self._vals = vals
1242+
self._bins = bins
1243+
self._update_data()
1244+
1245+
11941246
class BrokenBarHCollection(PolyCollection):
11951247
"""
11961248
A collection of horizontal bars spanning *yrange* with a sequence of

lib/matplotlib/legend.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from matplotlib.patches import Patch, Rectangle, Shadow, FancyBboxPatch, HistLine
3737
from matplotlib.collections import (LineCollection, RegularPolyCollection,
3838
CircleCollection, PathCollection,
39-
PolyCollection)
39+
PolyCollection, StepPolyCollection)
4040
from matplotlib.transforms import Bbox, BboxBase, TransformedBbox
4141
from matplotlib.transforms import BboxTransformTo, BboxTransformFrom
4242

@@ -624,6 +624,7 @@ def draw(self, renderer):
624624
Line2D: legend_handler.HandlerLine2D(),
625625
Patch: legend_handler.HandlerPatch(),
626626
HistLine: legend_handler.HandlerLinePatch(),
627+
StepPolyCollection: legend_handler.HandlerLinePatch(),
627628
LineCollection: legend_handler.HandlerLineCollection(),
628629
RegularPolyCollection: legend_handler.HandlerRegularPolyCollection(),
629630
CircleCollection: legend_handler.HandlerCircleCollection(),

lib/matplotlib/legend_handler.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,22 @@ def patch_func(legend=legend, orig_handle=orig_handle,
328328
super().__init__(**kw)
329329
self._patch_func = patch_func
330330

331+
def ded(self, prop):
332+
print(prop)
333+
if isinstance(prop, list):
334+
ret = prop[0]
335+
elif hasattr(prop, "shape"):
336+
if prop.shape[0] == 1:
337+
ret = prop.flatten()
338+
else:
339+
ret = prop
340+
print(ret)
341+
return ret
342+
331343
def _create_patch(self, legend, orig_handle,
332344
xdescent, ydescent, width, height, fontsize):
333345
if self._patch_func is None:
334-
p = Rectangle(xy=(-xdescent, -ydescent),
346+
p = Rectangle(xy=(-xdescent, -ydescent), color=self.ded(orig_handle.get_facecolor()),
335347
width=width, height=height)
336348
else:
337349
p = self._patch_func(legend=legend, orig_handle=orig_handle,
@@ -344,9 +356,9 @@ def _create_line(self, legend, orig_handle,
344356

345357
# Overwrite manually because patch and line properties don't mix
346358
legline = Line2D([0, width], [height/2, height/2],
347-
color=orig_handle.get_edgecolor(),
348-
linestyle=orig_handle.get_linestyle(),
349-
linewidth=orig_handle.get_linewidth(),
359+
color=self.ded(orig_handle.get_edgecolor()),
360+
linestyle=self.ded(orig_handle.get_linestyle()),
361+
linewidth=self.ded(orig_handle.get_linewidth()),
350362
)
351363

352364
legline.set_drawstyle('default')
@@ -358,7 +370,7 @@ def create_artists(self, legend, orig_handle,
358370
if orig_handle.get_fill():
359371
p = self._create_patch(legend, orig_handle,
360372
xdescent, ydescent, width, height, fontsize)
361-
self.update_prop(p, orig_handle, legend)
373+
#self.update_prop(p, orig_handle, legend)
362374
else:
363375
p = self._create_line(legend, orig_handle,
364376
xdescent, ydescent, width, height, fontsize)

lib/matplotlib/patches.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,16 +1111,16 @@ def _update_data(self):
11111111

11121112
def set_bins(self, bins):
11131113
self._bins = bins
1114-
self.set_data(self._update_data())
1114+
self._update_data()
11151115

11161116
def set_vals(self, vals):
11171117
self._vals = vals
1118-
self.set_data(self._update_data())
1118+
self._update_data()
11191119

11201120
def set_vals_bins(self, vals, bins):
11211121
self._vals = vals
11221122
self._bins = bins
1123-
self.set_data(self._update_data())
1123+
self._update_data()
11241124

11251125

11261126
class Wedge(Patch):

0 commit comments

Comments
 (0)