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

Skip to content

Commit 4a111ce

Browse files
committed
feat: rename histline
1 parent e24ec93 commit 4a111ce

14 files changed

Lines changed: 122 additions & 121 deletions

File tree

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +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
165+
examples/lines_bars_and_markers/levels_demo.py: E402
166166
examples/lines_bars_and_markers/horizontal_barchart_distribution.py: E402
167167
examples/lines_bars_and_markers/joinstyle.py: E402
168168
examples/lines_bars_and_markers/scatter_hist.py: E402

doc/api/artist_api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
``matplotlib.artist``
55
*********************
66

7-
.. inheritance-diagram:: matplotlib.axes._axes.Axes matplotlib.axes._base._AxesBase matplotlib.axis.Axis matplotlib.axis.Tick matplotlib.axis.XAxis matplotlib.axis.XTick matplotlib.axis.YAxis matplotlib.axis.YTick matplotlib.collections.AsteriskPolygonCollection matplotlib.collections.BrokenBarHCollection matplotlib.collections.CircleCollection matplotlib.collections.Collection matplotlib.collections.EllipseCollection matplotlib.collections.EventCollection matplotlib.collections.LineCollection matplotlib.collections.PatchCollection matplotlib.collections.PathCollection matplotlib.collections.PolyCollection matplotlib.collections.QuadMesh matplotlib.collections.RegularPolyCollection matplotlib.collections.StarPolygonCollection matplotlib.collections.TriMesh matplotlib.collections._CollectionWithSizes matplotlib.contour.ClabelText matplotlib.figure.Figure matplotlib.image.AxesImage matplotlib.image.BboxImage matplotlib.image.FigureImage matplotlib.image.NonUniformImage matplotlib.image.PcolorImage matplotlib.image._ImageBase matplotlib.legend.Legend matplotlib.lines.Line2D matplotlib.offsetbox.AnchoredOffsetbox matplotlib.offsetbox.AnchoredText matplotlib.offsetbox.AnnotationBbox matplotlib.offsetbox.AuxTransformBox matplotlib.offsetbox.DrawingArea matplotlib.offsetbox.HPacker matplotlib.offsetbox.OffsetBox matplotlib.offsetbox.OffsetImage matplotlib.offsetbox.PackerBase matplotlib.offsetbox.PaddedBox matplotlib.offsetbox.TextArea matplotlib.offsetbox.VPacker matplotlib.patches.Arc matplotlib.patches.Arrow matplotlib.patches.Circle matplotlib.patches.CirclePolygon matplotlib.patches.ConnectionPatch matplotlib.patches.Ellipse matplotlib.patches.FancyArrow matplotlib.patches.FancyArrowPatch matplotlib.patches.FancyBboxPatch matplotlib.patches.Patch matplotlib.patches.PathPatch matplotlib.patches.Polygon matplotlib.patches.Rectangle matplotlib.patches.RegularPolygon matplotlib.patches.Shadow matplotlib.patches.Wedge matplotlib.projections.geo.AitoffAxes matplotlib.projections.geo.GeoAxes matplotlib.projections.geo.HammerAxes matplotlib.projections.geo.LambertAxes matplotlib.projections.geo.MollweideAxes matplotlib.projections.polar.PolarAxes matplotlib.quiver.Barbs matplotlib.quiver.Quiver matplotlib.quiver.QuiverKey matplotlib.spines.Spine matplotlib.table.Cell matplotlib.table.CustomCell matplotlib.table.Table matplotlib.text.Annotation matplotlib.text.Text
7+
.. inheritance-diagram:: matplotlib.axes._axes.Axes matplotlib.axes._base._AxesBase matplotlib.axis.Axis matplotlib.axis.Tick matplotlib.axis.XAxis matplotlib.axis.XTick matplotlib.axis.YAxis matplotlib.axis.YTick matplotlib.collections.AsteriskPolygonCollection matplotlib.collections.BrokenBarHCollection matplotlib.collections.CircleCollection matplotlib.collections.Collection matplotlib.collections.EllipseCollection matplotlib.collections.EventCollection matplotlib.collections.LineCollection matplotlib.collections.PatchCollection matplotlib.collections.PathCollection matplotlib.collections.PolyCollection matplotlib.collections.QuadMesh matplotlib.collections.RegularPolyCollection matplotlib.collections.StarPolygonCollection matplotlib.collections.TriMesh matplotlib.collections._CollectionWithSizes matplotlib.contour.ClabelText matplotlib.figure.Figure matplotlib.image.AxesImage matplotlib.image.BboxImage matplotlib.image.FigureImage matplotlib.image.NonUniformImage matplotlib.image.PcolorImage matplotlib.image._ImageBase matplotlib.legend.Legend matplotlib.lines.Line2D matplotlib.offsetbox.AnchoredOffsetbox matplotlib.offsetbox.AnchoredText matplotlib.offsetbox.AnnotationBbox matplotlib.offsetbox.AuxTransformBox matplotlib.offsetbox.DrawingArea matplotlib.offsetbox.HPacker matplotlib.offsetbox.OffsetBox matplotlib.offsetbox.OffsetImage matplotlib.offsetbox.PackerBase matplotlib.offsetbox.PaddedBox matplotlib.offsetbox.TextArea matplotlib.offsetbox.VPacker matplotlib.patches.Arc matplotlib.patches.Arrow matplotlib.patches.Circle matplotlib.patches.CirclePolygon matplotlib.patches.ConnectionPatch matplotlib.patches.Ellipse matplotlib.patches.FancyArrow matplotlib.patches.FancyArrowPatch matplotlib.patches.FancyBboxPatch matplotlib.patches.Patch matplotlib.patches.PathPatch matplotlib.patches.LevelsPatch matplotlib.patches.Polygon matplotlib.patches.Rectangle matplotlib.patches.RegularPolygon matplotlib.patches.Shadow matplotlib.patches.Wedge matplotlib.projections.geo.AitoffAxes matplotlib.projections.geo.GeoAxes matplotlib.projections.geo.HammerAxes matplotlib.projections.geo.LambertAxes matplotlib.projections.geo.MollweideAxes matplotlib.projections.polar.PolarAxes matplotlib.quiver.Barbs matplotlib.quiver.Quiver matplotlib.quiver.QuiverKey matplotlib.spines.Spine matplotlib.table.Cell matplotlib.table.CustomCell matplotlib.table.Table matplotlib.text.Annotation matplotlib.text.Text
88
:parts: 1
99
:private-bases:
1010

doc/api/patches_api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Classes
2929
FancyBboxPatch
3030
Patch
3131
PathPatch
32+
LevelsPatch
3233
Polygon
3334
Rectangle
3435
RegularPolygon

doc/users/next_whats_new/steppatch_and_histline.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
New `~.matplotlib.patches.StepPatch` artist and a `.pyplot.histline` method
1+
New `~.matplotlib.patches.LevelsPatch` artist and a `.pyplot.levels` method
22
---------------------------------------------------------------------------
33
These take inputs of asymmetric lengths with y-like values and
44
x-like edges, between which the values lie.
@@ -14,9 +14,9 @@ x-like edges, between which the values lie.
1414

1515
fig, ax = plt.subplots(constrained_layout=True)
1616

17-
ax.histline(h, bins)
17+
ax.levels(h, bins)
1818

1919
plt.show()
2020

21-
See :doc:`/gallery/lines_bars_and_markers/histline_demo`
21+
See :doc:`/gallery/lines_bars_and_markers/levels_demo`
2222
for examples.

examples/lines_bars_and_markers/histline_demo.py

Lines changed: 0 additions & 60 deletions
This file was deleted.
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.levels`
7+
for histogram and histogram-like data visualization and an associated
8+
underlying `.LevelsPatch` 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 LevelsPatch
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].levels(h, bins, label='Simple histogram')
22+
axs[0].levels(h, bins+5, baseline=50, label='--//-- w/ modified baseline')
23+
axs[0].levels(h, bins+10, baseline=None, label='--//-- w/ no edges')
24+
axs[0].set_title("Step Histograms")
25+
26+
axs[1].levels(np.arange(1, 6, 1), fill=True,
27+
label='Filled histogram\nw/ automatatic edges')
28+
axs[1].levels(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 = LevelsPatch(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("LevelsPatch 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.levels
59+
matplotlib.pyplot.levels
60+
matplotlib.patches.LevelsPatch

lib/matplotlib/axes/_axes.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6872,8 +6872,8 @@ 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, values, bins=None, *,
6876-
orientation='vertical', baseline=0, fill=False, **kwargs):
6875+
def levels(self, values, edges=None, *,
6876+
orientation='vertical', baseline=0, fill=False, **kwargs):
68776877
"""
68786878
A histogram-like line or filled plot.
68796879
@@ -6882,8 +6882,8 @@ def histline(self, values, bins=None, *,
68826882
values : array-like
68836883
An array of y-values.
68846884
6885-
bins : array-like, default: ``range(len(vals)+1)``
6886-
A array of x-values, with ``len(bins) == len(vals) + 1``,
6885+
edges : array-like, default: ``range(len(vals)+1)``
6886+
A array of x-values, with ``len(edges) == len(vals) + 1``,
68876887
between which the curve takes on vals values.
68886888
68896889
orientation : {'vertical', 'horizontal'}, default: 'vertical'
@@ -6896,12 +6896,12 @@ def histline(self, values, bins=None, *,
68966896
68976897
Returns
68986898
-------
6899-
StepPatch : `.patches.StepPatch`
6899+
LevelsPatch : `matplotlib.patches.LevelsPatch`
69006900
69016901
Other Parameters
69026902
----------------
69036903
**kwargs
6904-
`~.matplotlib.patches.StepPatch` properties
6904+
`~matplotlib.patches.LevelsPatch` properties
69056905
69066906
"""
69076907

@@ -6915,15 +6915,15 @@ def histline(self, values, bins=None, *,
69156915
else:
69166916
kwargs.setdefault('edgecolor', _color)
69176917

6918-
if bins is None:
6919-
bins = np.arange(len(values) + 1)
6920-
6921-
patch = mpatches.StepPatch(values,
6922-
bins,
6923-
baseline=baseline,
6924-
orientation=orientation,
6925-
fill=fill,
6926-
**kwargs)
6918+
if edges is None:
6919+
edges = np.arange(len(values) + 1)
6920+
6921+
patch = mpatches.LevelsPatch(values,
6922+
edges,
6923+
baseline=baseline,
6924+
orientation=orientation,
6925+
fill=fill,
6926+
**kwargs)
69276927
self.add_patch(patch)
69286928
if baseline is None:
69296929
baseline = 0

lib/matplotlib/legend.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from matplotlib.font_manager import FontProperties
3535
from matplotlib.lines import Line2D
3636
from matplotlib.patches import (Patch, Rectangle, Shadow, FancyBboxPatch,
37-
StepPatch)
37+
LevelsPatch)
3838
from matplotlib.collections import (LineCollection, RegularPolyCollection,
3939
CircleCollection, PathCollection,
4040
PolyCollection)
@@ -624,7 +624,7 @@ def draw(self, renderer):
624624
ErrorbarContainer: legend_handler.HandlerErrorbar(),
625625
Line2D: legend_handler.HandlerLine2D(),
626626
Patch: legend_handler.HandlerPatch(),
627-
StepPatch: legend_handler.HandlerStepPatch(),
627+
LevelsPatch: legend_handler.HandlerLevelsPatch(),
628628
LineCollection: legend_handler.HandlerLineCollection(),
629629
RegularPolyCollection: legend_handler.HandlerRegularPolyCollection(),
630630
CircleCollection: legend_handler.HandlerCircleCollection(),

lib/matplotlib/legend_handler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,9 @@ def create_artists(self, legend, orig_handle,
302302
return [p]
303303

304304

305-
class HandlerStepPatch(HandlerBase):
305+
class HandlerLevelsPatch(HandlerBase):
306306
"""
307-
Handler for `~.matplotlib.patches.StepPatch` instances.
307+
Handler for `~.matplotlib.patches.LevelsPatch` instances.
308308
"""
309309
def __init__(self, **kw):
310310
"""
@@ -319,7 +319,7 @@ def _create_patch(self, legend, orig_handle,
319319
width=width, height=height)
320320
return p
321321

322-
# Unfilled StepPatch should show as a line
322+
# Unfilled LevelsPatch should show as a line
323323
def _create_line(self, legend, orig_handle,
324324
xdescent, ydescent, width, height, fontsize):
325325

lib/matplotlib/patches.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -989,8 +989,8 @@ def set_path(self, path):
989989
self._path = path
990990

991991

992-
class StepPatch(PathPatch):
993-
"""An unclosed stepline path patch."""
992+
class LevelsPatch(PathPatch):
993+
"""An unclosed levels path patch."""
994994

995995
@docstring.dedent_interpd
996996
def __init__(self, values, edges, *,
@@ -1024,6 +1024,8 @@ def __init__(self, values, edges, *,
10241024
super().__init__(path, **kwargs)
10251025

10261026
def _update_data(self):
1027+
if np.isnan(np.sum(self._edges)):
1028+
raise ValueError('Nan values in "edges" are disallowed')
10271029
if self._edges.size - 1 != self._values.size:
10281030
raise ValueError('Size mismatch between "values" and "edges". '
10291031
"Expected `len(values) + 1 == len(edges)`, but "
@@ -1032,18 +1034,16 @@ def _update_data(self):
10321034
)
10331035
verts, codes = [], []
10341036
for idx0, idx1 in cbook.contiguous_regions(~np.isnan(self._values)):
1035-
x = np.vstack((self._edges[idx0:idx1+1],
1036-
self._edges[idx0:idx1+1])).T.flatten()
1037-
y = np.vstack((self._values[idx0:idx1],
1038-
self._values[idx0:idx1])).T.flatten()
1037+
x = np.repeat(self._edges[idx0:idx1+1], 2)
1038+
y = np.repeat(self._values[idx0:idx1], 2)
10391039
if self.baseline is not None:
10401040
y = np.hstack((self.baseline, y, self.baseline))
10411041
else:
10421042
y = np.hstack((y[0], y, y[-1]))
10431043
if self.orientation == 'vertical':
1044-
xy = np.vstack([x, y]).T
1044+
xy = np.column_stack([x, y])
10451045
else:
1046-
xy = np.vstack([y, x]).T
1046+
xy = np.column_stack([y, x])
10471047
verts.append(xy)
10481048
codes.append(np.array([Path.MOVETO] + [Path.LINETO]*(len(xy)-1)))
10491049
return np.vstack(verts), np.hstack(codes)

0 commit comments

Comments
 (0)