Skip to content

DOC: Add plotting examples and fix broken examples #33989

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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 90 additions & 27 deletions pandas/plotting/_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,12 @@ def scatter_matrix(

Examples
--------
>>> df = pd.DataFrame(np.random.randn(1000, 4), columns=['A','B','C','D'])
>>> scatter_matrix(df, alpha=0.2)

.. plot::
:context: close-figs

>>> df = pd.DataFrame(np.random.randn(1000, 4), columns=['A','B','C','D'])
>>> pd.plotting.scatter_matrix(df, alpha=0.2)
"""
plot_backend = _get_plot_backend("matplotlib")
return plot_backend.scatter_matrix(
Expand Down Expand Up @@ -179,24 +183,31 @@ def radviz(frame, class_column, ax=None, color=None, colormap=None, **kwds):

Examples
--------

.. plot::
:context: close-figs

>>> df = pd.DataFrame({
... 'SepalLength': [6.5, 7.7, 5.1, 5.8, 7.6, 5.0, 5.4, 4.6,
... 6.7, 4.6],
... 'SepalWidth': [3.0, 3.8, 3.8, 2.7, 3.0, 2.3, 3.0, 3.2,
... 3.3, 3.6],
... 'PetalLength': [5.5, 6.7, 1.9, 5.1, 6.6, 3.3, 4.5, 1.4,
... 5.7, 1.0],
... 'PetalWidth': [1.8, 2.2, 0.4, 1.9, 2.1, 1.0, 1.5, 0.2,
... 2.1, 0.2],
... 'Category': ['virginica', 'virginica', 'setosa',
... 'virginica', 'virginica', 'versicolor',
... 'versicolor', 'setosa', 'virginica',
... 'setosa']
... })
>>> rad_viz = pd.plotting.radviz(df, 'Category') # doctest: +SKIP
>>> df = pd.DataFrame(
... {
... 'SepalLength': [6.5, 7.7, 5.1, 5.8, 7.6, 5.0, 5.4, 4.6, 6.7, 4.6],
... 'SepalWidth': [3.0, 3.8, 3.8, 2.7, 3.0, 2.3, 3.0, 3.2, 3.3, 3.6],
... 'PetalLength': [5.5, 6.7, 1.9, 5.1, 6.6, 3.3, 4.5, 1.4, 5.7, 1.0],
... 'PetalWidth': [1.8, 2.2, 0.4, 1.9, 2.1, 1.0, 1.5, 0.2, 2.1, 0.2],
... 'Category': [
... 'virginica',
... 'virginica',
... 'setosa',
... 'virginica',
... 'virginica',
... 'versicolor',
... 'versicolor',
... 'setosa',
... 'virginica',
... 'setosa'
... ]
... }
... )
>>> pd.plotting.radviz(df, 'Category')
"""
plot_backend = _get_plot_backend("matplotlib")
return plot_backend.radviz(
Expand Down Expand Up @@ -243,6 +254,18 @@ def andrews_curves(
Returns
-------
class:`matplotlip.axis.Axes`

Examples
--------

.. plot::
:context: close-figs

>>> df = pd.read_csv(
... 'https://raw.github.com/pandas-dev/'
... 'pandas/master/pandas/tests/data/iris.csv'
... )
>>> pd.plotting.andrews_curves(df, 'Name')
"""
plot_backend = _get_plot_backend("matplotlib")
return plot_backend.andrews_curves(
Expand Down Expand Up @@ -298,10 +321,10 @@ def bootstrap_plot(series, fig=None, size=50, samples=500, **kwds):
This example draws a basic bootstap plot for a Series.

.. plot::
:context: close-figs
:context: close-figs

>>> s = pd.Series(np.random.uniform(size=100))
>>> fig = pd.plotting.bootstrap_plot(s) # doctest: +SKIP
>>> s = pd.Series(np.random.uniform(size=100))
>>> pd.plotting.bootstrap_plot(s)
"""
plot_backend = _get_plot_backend("matplotlib")
return plot_backend.bootstrap_plot(
Expand Down Expand Up @@ -358,13 +381,17 @@ def parallel_coordinates(

Examples
--------
>>> from matplotlib import pyplot as plt
>>> df = pd.read_csv('https://raw.github.com/pandas-dev/pandas/master'
'/pandas/tests/data/iris.csv')
>>> pd.plotting.parallel_coordinates(
df, 'Name',
color=('#556270', '#4ECDC4', '#C7F464'))
>>> plt.show()

.. plot::
:context: close-figs

>>> df = pd.read_csv(
... 'https://raw.github.com/pandas-dev/'
... 'pandas/master/pandas/tests/data/iris.csv'
... )
>>> pd.plotting.parallel_coordinates(
... df, 'Name', color=('#556270', '#4ECDC4', '#C7F464')
... )
"""
plot_backend = _get_plot_backend("matplotlib")
return plot_backend.parallel_coordinates(
Expand Down Expand Up @@ -398,6 +425,28 @@ def lag_plot(series, lag=1, ax=None, **kwds):
Returns
-------
class:`matplotlib.axis.Axes`

Examples
--------

Lag plots are most commonly used to look for patterns in time series data.

Given the following time series

.. plot::
:context: close-figs

>>> np.random.seed(5)
>>> x = np.cumsum(np.random.normal(loc=1, scale=5, size=50))
>>> s = pd.Series(x)
>>> s.plot()

A lag plot with ``lag=1`` returns

.. plot::
:context: close-figs

>>> pd.plotting.lag_plot(s, lag=1)
"""
plot_backend = _get_plot_backend("matplotlib")
return plot_backend.lag_plot(series=series, lag=lag, ax=ax, **kwds)
Expand All @@ -417,6 +466,20 @@ def autocorrelation_plot(series, ax=None, **kwargs):
Returns
-------
class:`matplotlib.axis.Axes`

Examples
--------

The horizontal lines in the plot correspond to 95% and 99% confidence bands.

The dashed line is 99% confidence band.

.. plot::
:context: close-figs

>>> spacing = np.linspace(-9 * np.pi, 9 * np.pi, num=1000)
>>> s = pd.Series(0.7 * np.random.rand(1000) + 0.3 * np.sin(spacing))
>>> pd.plotting.autocorrelation_plot(s)
"""
plot_backend = _get_plot_backend("matplotlib")
return plot_backend.autocorrelation_plot(series=series, ax=ax, **kwargs)
Expand Down