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

Skip to content

Commit e24ec93

Browse files
committed
feat: Add histline/StepPatch demo and what's new
1 parent 48c2068 commit e24ec93

10 files changed

Lines changed: 112 additions & 586 deletions

File tree

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ per-file-ignores =
162162
examples/lines_bars_and_markers/fill.py: E402
163163
examples/lines_bars_and_markers/fill_between_demo.py: E402
164164
examples/lines_bars_and_markers/filled_step.py: E402
165+
examples/lines_bars_and_markers/histline_demo.py: E402
165166
examples/lines_bars_and_markers/horizontal_barchart_distribution.py: E402
166167
examples/lines_bars_and_markers/joinstyle.py: E402
167168
examples/lines_bars_and_markers/scatter_hist.py: E402
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
New `~.matplotlib.patches.StepPatch` artist and a `.pyplot.histline` method
2+
---------------------------------------------------------------------------
3+
These take inputs of asymmetric lengths with y-like values and
4+
x-like edges, between which the values lie.
5+
6+
.. plot::
7+
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
11+
np.random.seed(0)
12+
h, bins = np.histogram(np.random.normal(5, 2, 5000),
13+
bins=np.linspace(0,10,20))
14+
15+
fig, ax = plt.subplots(constrained_layout=True)
16+
17+
ax.histline(h, bins)
18+
19+
plt.show()
20+
21+
See :doc:`/gallery/lines_bars_and_markers/histline_demo`
22+
for examples.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
"""
2+
=============
3+
Histline Demo
4+
=============
5+
6+
This example demonstrates the use of `~.matplotlib.pyplot.histline`
7+
for histogram and histogram-like data visualization and an associated
8+
underlying `~.matplotlib.patches.StepPatch` artist, which is
9+
a contrained version of `.PathPatch` specified by its bins and edges.
10+
"""
11+
12+
import numpy as np
13+
import matplotlib.pyplot as plt
14+
from matplotlib.patches import StepPatch
15+
16+
np.random.seed(0)
17+
h, bins = np.histogram(np.random.normal(5, 3, 5000),
18+
bins=np.linspace(0, 10, 20))
19+
20+
fig, axs = plt.subplots(3, 1, figsize=(7, 15))
21+
axs[0].histline(h, bins, label='Simple histogram')
22+
axs[0].histline(h, bins+5, baseline=50, label='--//-- w/ modified baseline')
23+
axs[0].histline(h, bins+10, baseline=None, label='--//-- w/ no edges')
24+
axs[0].set_title("Step Histograms")
25+
26+
axs[1].histline(np.arange(1, 6, 1), fill=True,
27+
label='Filled histogram\nw/ automatatic edges')
28+
axs[1].histline(np.arange(1, 6, 1)*0.3, np.arange(2, 8, 1),
29+
orientation='horizontal', hatch='//',
30+
label='Hatched histogram\nw/ horizontal orientation')
31+
axs[1].set_title("Filled histogram")
32+
33+
patch = StepPatch(values=[1, 2, 3, 2, 1],
34+
edges=range(1, 7),
35+
label=('Patch derived underlying object\n'
36+
'with default edge/facecolor behaviour'))
37+
axs[2].add_patch(patch)
38+
axs[2].set_xlim(0, 7)
39+
axs[2].set_ylim(-1, 5)
40+
axs[2].set_title("StepPatch artist")
41+
42+
for ax in axs:
43+
ax.legend()
44+
plt.show()
45+
46+
47+
#############################################################################
48+
#
49+
# ------------
50+
#
51+
# References
52+
# """"""""""
53+
#
54+
# The use of the following functions, methods, classes and modules is shown
55+
# in this example:
56+
57+
import matplotlib
58+
matplotlib.axes.Axes.histline
59+
matplotlib.pyplot.histline
60+
matplotlib.patches.StepPatch

lib/matplotlib/axes/_axes.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6872,36 +6872,36 @@ def hist(self, x, bins=None, range=None, density=False, weights=None,
68726872
return tops, bins, cbook.silent_list(patch_type, patches)
68736873

68746874
@_preprocess_data()
6875-
def histline(self, vals, bins=None, *,
6875+
def histline(self, values, bins=None, *,
68766876
orientation='vertical', baseline=0, fill=False, **kwargs):
68776877
"""
68786878
A histogram-like line or filled plot.
68796879
68806880
Parameters
68816881
----------
6882-
vals : array
6882+
values : array-like
68836883
An array of y-values.
68846884
6885-
bins : array, default: ``range(len(vals)+1)``
6885+
bins : array-like, default: ``range(len(vals)+1)``
68866886
A array of x-values, with ``len(bins) == len(vals) + 1``,
68876887
between which the curve takes on vals values.
68886888
68896889
orientation : {'vertical', 'horizontal'}, default: 'vertical'
68906890
68916891
baseline : float or None, default: 0
68926892
Determines starting value of the bounding edges or when
6893-
"fill" == True, position of lower edge.
6893+
``fill=True``, position of lower edge.
68946894
68956895
fill : bool, default: False
68966896
68976897
Returns
68986898
-------
6899-
patch : `.StepPatch`
6899+
StepPatch : `.patches.StepPatch`
69006900
69016901
Other Parameters
69026902
----------------
69036903
**kwargs
6904-
`~matplotlib.patches.Patch` properties
6904+
`~.matplotlib.patches.StepPatch` properties
69056905
69066906
"""
69076907

@@ -6916,9 +6916,9 @@ def histline(self, vals, bins=None, *,
69166916
kwargs.setdefault('edgecolor', _color)
69176917

69186918
if bins is None:
6919-
bins = np.arange(len(vals) + 1)
6919+
bins = np.arange(len(values) + 1)
69206920

6921-
patch = mpatches.StepPatch(vals,
6921+
patch = mpatches.StepPatch(values,
69226922
bins,
69236923
baseline=baseline,
69246924
orientation=orientation,

lib/matplotlib/legend_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def create_artists(self, legend, orig_handle,
304304

305305
class HandlerStepPatch(HandlerBase):
306306
"""
307-
Handler for `.StepPatch` instances.
307+
Handler for `~.matplotlib.patches.StepPatch` instances.
308308
"""
309309
def __init__(self, **kw):
310310
"""

lib/matplotlib/patches.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -993,23 +993,23 @@ class StepPatch(PathPatch):
993993
"""An unclosed stepline path patch."""
994994

995995
@docstring.dedent_interpd
996-
def __init__(self, vals, edges, *,
997-
orientation='horizontal', baseline=0, **kwargs):
996+
def __init__(self, values, edges, *,
997+
orientation='vertical', baseline=0, **kwargs):
998998
"""
999999
Parameters
10001000
----------
1001-
vals : array
1001+
values : array-like
10021002
An array of y-values.
10031003
1004-
edges : array
1004+
edges : array-like
10051005
A array of x-value edges, with ``len(edges) == len(vals) + 1``,
10061006
between which the curve takes on vals values.
10071007
10081008
orientation : {'vertical', 'horizontal'}, default: 'vertical'
10091009
10101010
baseline : float or None, default: 0
10111011
Determines starting value of the bounding edges or when
1012-
"fill" == True, position of lower edge.
1012+
``fill=True``, position of lower edge.
10131013
10141014
Other valid keyword arguments are:
10151015
@@ -1018,20 +1018,24 @@ def __init__(self, vals, edges, *,
10181018
self.baseline = baseline
10191019
self.orientation = orientation
10201020
self._edges = np.asarray(edges)
1021-
self._vals = np.asarray(vals)
1021+
self._values = np.asarray(values)
10221022
verts, codes = self._update_data()
10231023
path = Path(verts, codes)
10241024
super().__init__(path, **kwargs)
10251025

10261026
def _update_data(self):
1027-
if self._edges.size - 1 != self._vals.size:
1028-
raise ValueError('Size mismatch between "vals" and "edges"')
1027+
if self._edges.size - 1 != self._values.size:
1028+
raise ValueError('Size mismatch between "values" and "edges". '
1029+
"Expected `len(values) + 1 == len(edges)`, but "
1030+
"they are or lengths {} and {}".format(
1031+
self._edges.size, self._values.size)
1032+
)
10291033
verts, codes = [], []
1030-
for idx0, idx1 in cbook.contiguous_regions(~np.isnan(self._vals)):
1034+
for idx0, idx1 in cbook.contiguous_regions(~np.isnan(self._values)):
10311035
x = np.vstack((self._edges[idx0:idx1+1],
10321036
self._edges[idx0:idx1+1])).T.flatten()
1033-
y = np.vstack((self._vals[idx0:idx1],
1034-
self._vals[idx0:idx1])).T.flatten()
1037+
y = np.vstack((self._values[idx0:idx1],
1038+
self._values[idx0:idx1])).T.flatten()
10351039
if self.baseline is not None:
10361040
y = np.hstack((self.baseline, y, self.baseline))
10371041
else:
@@ -1048,13 +1052,8 @@ def set_edges(self, edges):
10481052
self._edges = np.asarray(edges)
10491053
self._update_data()
10501054

1051-
def set_vals(self, vals):
1052-
self._vals = np.asarray(vals)
1053-
self._update_data()
1054-
1055-
def set_vals_edges(self, vals, edges):
1056-
self._vals = np.asarray(vals)
1057-
self._edegs = np.asarray(edges)
1055+
def set_values(self, values):
1056+
self._values = np.asarray(values)
10581057
self._update_data()
10591058

10601059

lib/matplotlib/pyplot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2780,10 +2780,10 @@ def hist(
27802780
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
27812781
@_copy_docstring_and_deprecators(Axes.histline)
27822782
def histline(
2783-
vals, bins=None, *, orientation='vertical', baseline=0,
2783+
values, bins=None, *, orientation='vertical', baseline=0,
27842784
fill=False, data=None, **kwargs):
27852785
return gca().histline(
2786-
vals, bins=bins, orientation=orientation, baseline=baseline,
2786+
values, bins=bins, orientation=orientation, baseline=baseline,
27872787
fill=fill, **({"data": data} if data is not None else {}),
27882788
**kwargs)
27892789

12.6 KB
Loading

0 commit comments

Comments
 (0)