-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add Axes.ecdf() method. #24728
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
Merged
Merged
Add Axes.ecdf() method. #24728
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,6 +114,7 @@ Statistics | |
:template: autosummary.rst | ||
:nosignatures: | ||
|
||
ecdf | ||
boxplot | ||
violinplot | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
``Axes.ecdf`` | ||
~~~~~~~~~~~~~ | ||
A new Axes method, `~.Axes.ecdf`, allows plotting empirical cumulative | ||
distribution functions without any binning. | ||
|
||
.. plot:: | ||
tacaswell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
:include-source: | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
fig, ax = plt.subplots() | ||
ax.ecdf(np.random.randn(100)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
""" | ||
======= | ||
ecdf(x) | ||
======= | ||
|
||
See `~matplotlib.axes.Axes.ecdf`. | ||
""" | ||
|
||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
|
||
plt.style.use('_mpl-gallery') | ||
|
||
# make data | ||
np.random.seed(1) | ||
x = 4 + np.random.normal(0, 1.5, 200) | ||
|
||
# plot: | ||
fig, ax = plt.subplots() | ||
ax.ecdf(x) | ||
plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -7112,6 +7112,108 @@ def hist2d(self, x, y, bins=10, range=None, density=False, weights=None, | |||||||
|
||||||||
return h, xedges, yedges, pc | ||||||||
|
||||||||
@_preprocess_data(replace_names=["x", "weights"], label_namer="x") | ||||||||
@_docstring.dedent_interpd | ||||||||
def ecdf(self, x, weights=None, *, complementary=False, | ||||||||
orientation="vertical", compress=False, **kwargs): | ||||||||
""" | ||||||||
Compute and plot the empirical cumulative distribution function of *x*. | ||||||||
|
||||||||
.. versionadded:: 3.8 | ||||||||
|
||||||||
Parameters | ||||||||
---------- | ||||||||
x : 1d array-like | ||||||||
The input data. Infinite entries are kept (and move the relevant | ||||||||
end of the ecdf from 0/1), but NaNs and masked values are errors. | ||||||||
|
||||||||
weights : 1d array-like or None, default: None | ||||||||
The weights of the entries; must have the same shape as *x*. | ||||||||
Weights corresponding to NaN data points are dropped, and then the | ||||||||
remaining weights are normalized to sum to 1. If unset, all | ||||||||
entries have the same weight. | ||||||||
|
||||||||
complementary : bool, default: False | ||||||||
Whether to plot a cumulative distribution function, which increases | ||||||||
from 0 to 1 (the default), or a complementary cumulative | ||||||||
distribution function, which decreases from 1 to 0. | ||||||||
|
||||||||
orientation : {"vertical", "horizontal"}, default: "vertical" | ||||||||
Whether the entries are plotted along the x-axis ("vertical", the | ||||||||
default) or the y-axis ("horizontal"). This parameter takes the | ||||||||
same values as in `~.Axes.hist`. | ||||||||
|
||||||||
compress : bool, default: False | ||||||||
Whether multiple entries with the same values are grouped together | ||||||||
(with a summed weight) before plotting. This is mainly useful if | ||||||||
*x* contains many identical data points, to decrease the rendering | ||||||||
complexity of the plot. If *x* contains no duplicate points, this | ||||||||
has no effect and just uses some time and memory. | ||||||||
|
||||||||
Other Parameters | ||||||||
---------------- | ||||||||
data : indexable object, optional | ||||||||
DATA_PARAMETER_PLACEHOLDER | ||||||||
|
||||||||
**kwargs | ||||||||
Keyword arguments control the `.Line2D` properties: | ||||||||
|
||||||||
%(Line2D:kwdoc)s | ||||||||
oscargus marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
Returns | ||||||||
------- | ||||||||
`.Line2D` | ||||||||
|
||||||||
Notes | ||||||||
----- | ||||||||
The ecdf plot can be thought of as a cumulative histogram with one bin | ||||||||
per data entry; i.e. it reports on the entire dataset without any | ||||||||
arbitrary binning. | ||||||||
|
||||||||
If *x* contains NaNs or masked entries, either remove them first from | ||||||||
the array (if they should not taken into account), or replace them by | ||||||||
-inf or +inf (if they should be sorted at the beginning or the end of | ||||||||
the array). | ||||||||
""" | ||||||||
_api.check_in_list(["horizontal", "vertical"], orientation=orientation) | ||||||||
if "drawstyle" in kwargs or "ds" in kwargs: | ||||||||
raise TypeError("Cannot pass 'drawstyle' or 'ds' to ecdf()") | ||||||||
if np.ma.getmask(x).any(): | ||||||||
raise ValueError("ecdf() does not support masked entries") | ||||||||
x = np.asarray(x) | ||||||||
if np.isnan(x).any(): | ||||||||
raise ValueError("ecdf() does not support NaNs") | ||||||||
argsort = np.argsort(x) | ||||||||
x = x[argsort] | ||||||||
if weights is None: | ||||||||
# Ensure that we end at exactly 1, avoiding floating point errors. | ||||||||
cum_weights = (1 + np.arange(len(x))) / len(x) | ||||||||
else: | ||||||||
weights = np.take(weights, argsort) # Reorder weights like we reordered x. | ||||||||
cum_weights = np.cumsum(weights / np.sum(weights)) | ||||||||
if compress: | ||||||||
# Get indices of unique x values. | ||||||||
compress_idxs = [0, *(x[:-1] != x[1:]).nonzero()[0] + 1] | ||||||||
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
|
||||||||
x = x[compress_idxs] | ||||||||
cum_weights = cum_weights[compress_idxs] | ||||||||
if orientation == "vertical": | ||||||||
if not complementary: | ||||||||
line, = self.plot([x[0], *x], [0, *cum_weights], | ||||||||
drawstyle="steps-post", **kwargs) | ||||||||
else: | ||||||||
line, = self.plot([*x, x[-1]], [1, *1 - cum_weights], | ||||||||
drawstyle="steps-pre", **kwargs) | ||||||||
line.sticky_edges.y[:] = [0, 1] | ||||||||
else: # orientation == "horizontal": | ||||||||
if not complementary: | ||||||||
line, = self.plot([0, *cum_weights], [x[0], *x], | ||||||||
drawstyle="steps-pre", **kwargs) | ||||||||
else: | ||||||||
line, = self.plot([1, *1 - cum_weights], [*x, x[-1]], | ||||||||
drawstyle="steps-post", **kwargs) | ||||||||
line.sticky_edges.x[:] = [0, 1] | ||||||||
return line | ||||||||
|
||||||||
@_preprocess_data(replace_names=["x"]) | ||||||||
@_docstring.dedent_interpd | ||||||||
def psd(self, x, NFFT=None, Fs=None, Fc=None, detrend=None, | ||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -246,6 +246,7 @@ def boilerplate_gen(): | |
'contour', | ||
'contourf', | ||
'csd', | ||
'ecdf', | ||
'errorbar', | ||
'eventplot', | ||
'fill', | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.