-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
feat: StepPatch #18275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: StepPatch #18275
Changes from all commits
157aecf
467683d
67c15ec
48c2068
e24ec93
4a111ce
aa905ac
f2499b9
e5af734
6d17761
4018a62
ecc54cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -137,6 +137,7 @@ Binned | |
Axes.hexbin | ||
Axes.hist | ||
Axes.hist2d | ||
Axes.stairs | ||
|
||
Contours | ||
-------- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ Classes | |
FancyBboxPatch | ||
Patch | ||
PathPatch | ||
StepPatch | ||
Polygon | ||
Rectangle | ||
RegularPolygon | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
New StepPatch artist and a stairs method | ||
---------------------------------------- | ||
New `~.matplotlib.patches.StepPatch` artist and `.pyplot.stairs` method. | ||
For both the artist and the function, the x-like edges input is one | ||
longer than the y-like values input | ||
|
||
.. plot:: | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
np.random.seed(0) | ||
h, bins = np.histogram(np.random.normal(5, 2, 5000), | ||
bins=np.linspace(0,10,20)) | ||
|
||
fig, ax = plt.subplots(constrained_layout=True) | ||
|
||
ax.stairs(h, bins) | ||
|
||
plt.show() | ||
|
||
See :doc:`/gallery/lines_bars_and_markers/stairs_demo` | ||
for examples. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
""" | ||
=========== | ||
Stairs Demo | ||
=========== | ||
|
||
This example demonstrates the use of `~.matplotlib.pyplot.stairs` | ||
for histogram and histogram-like data visualization and an associated | ||
underlying `.StepPatch` artist, which is a specialized version of | ||
`.PathPatch` specified by its bins and edges. | ||
|
||
The primary difference to `~.matplotlib.pyplot.step` is that ``stairs`` | ||
x-like edges input is one longer than its y-like values input. | ||
""" | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from matplotlib.patches import StepPatch | ||
|
||
np.random.seed(0) | ||
h, bins = np.histogram(np.random.normal(5, 3, 5000), | ||
bins=np.linspace(0, 10, 20)) | ||
|
||
fig, axs = plt.subplots(3, 1, figsize=(7, 15)) | ||
axs[0].stairs(h, bins, label='Simple histogram') | ||
axs[0].stairs(h, bins+5, baseline=50, label='Modified baseline') | ||
axs[0].stairs(h, bins+10, baseline=None, label='No edges') | ||
axs[0].set_title("Step Histograms") | ||
|
||
axs[1].stairs(np.arange(1, 6, 1), fill=True, | ||
label='Filled histogram\nw/ automatic edges') | ||
axs[1].stairs(np.arange(1, 6, 1)*0.3, np.arange(2, 8, 1), | ||
orientation='horizontal', hatch='//', | ||
label='Hatched histogram\nw/ horizontal orientation') | ||
axs[1].set_title("Filled histogram") | ||
|
||
patch = StepPatch(values=[1, 2, 3, 2, 1], | ||
edges=range(1, 7), | ||
label=('Patch derived underlying object\n' | ||
'with default edge/facecolor behaviour')) | ||
axs[2].add_patch(patch) | ||
axs[2].set_xlim(0, 7) | ||
axs[2].set_ylim(-1, 5) | ||
axs[2].set_title("StepPatch artist") | ||
|
||
for ax in axs: | ||
ax.legend() | ||
plt.show() | ||
|
||
############################################################################# | ||
# Comparison of `.pyplot.step` and `.pyplot.stairs`. | ||
|
||
bins = np.arange(14) | ||
centers = bins[:-1] + np.diff(bins)/2 | ||
y = np.sin(centers / 2) | ||
|
||
plt.step(bins[:-1], y, where='post', label='Step(where="post")') | ||
plt.plot(bins[:-1], y, 'o--', color='grey', alpha=0.3) | ||
|
||
plt.stairs(y - 1, bins, baseline=None, label='Stairs') | ||
plt.plot(centers, y - 1, 'o--', color='grey', alpha=0.3) | ||
plt.plot(np.repeat(bins, 2), np.hstack([y[0], np.repeat(y, 2), y[-1]]) - 1, | ||
'o', color='red', alpha=0.2) | ||
|
||
plt.legend() | ||
plt.title('plt.step vs plt.stairs') | ||
plt.show() | ||
|
||
############################################################################# | ||
# | ||
# ------------ | ||
# | ||
# References | ||
# """""""""" | ||
# | ||
# The use of the following functions, methods, classes and modules is shown | ||
# in this example: | ||
|
||
import matplotlib | ||
matplotlib.axes.Axes.stairs | ||
matplotlib.pyplot.stairs | ||
matplotlib.patches.StepPatch |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6871,6 +6871,73 @@ def hist(self, x, bins=None, range=None, density=False, weights=None, | |||||
else "List[Polygon]") | ||||||
return tops, bins, cbook.silent_list(patch_type, patches) | ||||||
|
||||||
@_preprocess_data() | ||||||
def stairs(self, values, edges=None, *, | ||||||
orientation='vertical', baseline=0, fill=False, **kwargs): | ||||||
""" | ||||||
A histogram-like line or filled plot. | ||||||
|
||||||
Parameters | ||||||
---------- | ||||||
values : array-like | ||||||
An array of y-values. | ||||||
|
||||||
edges : array-like, default: ``range(len(vals)+1)`` | ||||||
A array of x-values, with ``len(edges) == len(vals) + 1``, | ||||||
between which the curve takes on vals values. | ||||||
|
||||||
orientation : {'vertical', 'horizontal'}, default: 'vertical' | ||||||
|
||||||
baseline : float or None, default: 0 | ||||||
Determines starting value of the bounding edges or when | ||||||
``fill=True``, position of lower edge. | ||||||
|
||||||
fill : bool, default: False | ||||||
|
||||||
Returns | ||||||
------- | ||||||
StepPatch : `matplotlib.patches.StepPatch` | ||||||
|
||||||
Other Parameters | ||||||
---------------- | ||||||
**kwargs | ||||||
`~matplotlib.patches.StepPatch` properties | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
""" | ||||||
|
||||||
if 'color' in kwargs: | ||||||
_color = kwargs.pop('color') | ||||||
else: | ||||||
_color = self._get_lines.get_next_color() | ||||||
if fill: | ||||||
kwargs.setdefault('edgecolor', 'none') | ||||||
kwargs.setdefault('facecolor', _color) | ||||||
else: | ||||||
kwargs.setdefault('edgecolor', _color) | ||||||
|
||||||
if edges is None: | ||||||
edges = np.arange(len(values) + 1) | ||||||
|
||||||
self._process_unit_info(xdata=edges, ydata=values, kwargs=kwargs) | ||||||
edges = self.convert_xunits(edges) | ||||||
values = self.convert_yunits(values) | ||||||
|
||||||
patch = mpatches.StepPatch(values, | ||||||
edges, | ||||||
baseline=baseline, | ||||||
orientation=orientation, | ||||||
fill=fill, | ||||||
**kwargs) | ||||||
self.add_patch(patch) | ||||||
if baseline is None: | ||||||
baseline = 0 | ||||||
if orientation == 'vertical': | ||||||
patch.sticky_edges.y.append(baseline) | ||||||
else: | ||||||
patch.sticky_edges.x.append(baseline) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just looking from the peanut gallery...
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1 is intended. I think the implied consensus was that the naming would be general but some histogram like assumptions could be made in the plotting function. That's why sticky is in stairs and not in StepPatch. I suppose that means the update of StepPatch is not nicely behaved wrt baseline sticky, but then again neither are lims |
||||||
self._request_autoscale_view() | ||||||
return patch | ||||||
|
||||||
@_preprocess_data(replace_names=["x", "y", "weights"]) | ||||||
@docstring.dedent_interpd | ||||||
def hist2d(self, x, y, bins=10, range=None, density=False, weights=None, | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.