Skip to content

Commit 18a7e97

Browse files
authored
DOC: Add pd.plotting examples and fix broken examples (#33989)
1 parent 8771c25 commit 18a7e97

File tree

1 file changed

+90
-27
lines changed

1 file changed

+90
-27
lines changed

pandas/plotting/_misc.py

+90-27
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,12 @@ def scatter_matrix(
117117
118118
Examples
119119
--------
120-
>>> df = pd.DataFrame(np.random.randn(1000, 4), columns=['A','B','C','D'])
121-
>>> scatter_matrix(df, alpha=0.2)
120+
121+
.. plot::
122+
:context: close-figs
123+
124+
>>> df = pd.DataFrame(np.random.randn(1000, 4), columns=['A','B','C','D'])
125+
>>> pd.plotting.scatter_matrix(df, alpha=0.2)
122126
"""
123127
plot_backend = _get_plot_backend("matplotlib")
124128
return plot_backend.scatter_matrix(
@@ -179,24 +183,31 @@ def radviz(frame, class_column, ax=None, color=None, colormap=None, **kwds):
179183
180184
Examples
181185
--------
186+
182187
.. plot::
183188
:context: close-figs
184189
185-
>>> df = pd.DataFrame({
186-
... 'SepalLength': [6.5, 7.7, 5.1, 5.8, 7.6, 5.0, 5.4, 4.6,
187-
... 6.7, 4.6],
188-
... 'SepalWidth': [3.0, 3.8, 3.8, 2.7, 3.0, 2.3, 3.0, 3.2,
189-
... 3.3, 3.6],
190-
... 'PetalLength': [5.5, 6.7, 1.9, 5.1, 6.6, 3.3, 4.5, 1.4,
191-
... 5.7, 1.0],
192-
... 'PetalWidth': [1.8, 2.2, 0.4, 1.9, 2.1, 1.0, 1.5, 0.2,
193-
... 2.1, 0.2],
194-
... 'Category': ['virginica', 'virginica', 'setosa',
195-
... 'virginica', 'virginica', 'versicolor',
196-
... 'versicolor', 'setosa', 'virginica',
197-
... 'setosa']
198-
... })
199-
>>> rad_viz = pd.plotting.radviz(df, 'Category') # doctest: +SKIP
190+
>>> df = pd.DataFrame(
191+
... {
192+
... 'SepalLength': [6.5, 7.7, 5.1, 5.8, 7.6, 5.0, 5.4, 4.6, 6.7, 4.6],
193+
... 'SepalWidth': [3.0, 3.8, 3.8, 2.7, 3.0, 2.3, 3.0, 3.2, 3.3, 3.6],
194+
... 'PetalLength': [5.5, 6.7, 1.9, 5.1, 6.6, 3.3, 4.5, 1.4, 5.7, 1.0],
195+
... 'PetalWidth': [1.8, 2.2, 0.4, 1.9, 2.1, 1.0, 1.5, 0.2, 2.1, 0.2],
196+
... 'Category': [
197+
... 'virginica',
198+
... 'virginica',
199+
... 'setosa',
200+
... 'virginica',
201+
... 'virginica',
202+
... 'versicolor',
203+
... 'versicolor',
204+
... 'setosa',
205+
... 'virginica',
206+
... 'setosa'
207+
... ]
208+
... }
209+
... )
210+
>>> pd.plotting.radviz(df, 'Category')
200211
"""
201212
plot_backend = _get_plot_backend("matplotlib")
202213
return plot_backend.radviz(
@@ -243,6 +254,18 @@ def andrews_curves(
243254
Returns
244255
-------
245256
class:`matplotlip.axis.Axes`
257+
258+
Examples
259+
--------
260+
261+
.. plot::
262+
:context: close-figs
263+
264+
>>> df = pd.read_csv(
265+
... 'https://raw.github.com/pandas-dev/'
266+
... 'pandas/master/pandas/tests/data/iris.csv'
267+
... )
268+
>>> pd.plotting.andrews_curves(df, 'Name')
246269
"""
247270
plot_backend = _get_plot_backend("matplotlib")
248271
return plot_backend.andrews_curves(
@@ -298,10 +321,10 @@ def bootstrap_plot(series, fig=None, size=50, samples=500, **kwds):
298321
This example draws a basic bootstap plot for a Series.
299322
300323
.. plot::
301-
:context: close-figs
324+
:context: close-figs
302325
303-
>>> s = pd.Series(np.random.uniform(size=100))
304-
>>> fig = pd.plotting.bootstrap_plot(s) # doctest: +SKIP
326+
>>> s = pd.Series(np.random.uniform(size=100))
327+
>>> pd.plotting.bootstrap_plot(s)
305328
"""
306329
plot_backend = _get_plot_backend("matplotlib")
307330
return plot_backend.bootstrap_plot(
@@ -358,13 +381,17 @@ def parallel_coordinates(
358381
359382
Examples
360383
--------
361-
>>> from matplotlib import pyplot as plt
362-
>>> df = pd.read_csv('https://raw.github.com/pandas-dev/pandas/master'
363-
'/pandas/tests/data/iris.csv')
364-
>>> pd.plotting.parallel_coordinates(
365-
df, 'Name',
366-
color=('#556270', '#4ECDC4', '#C7F464'))
367-
>>> plt.show()
384+
385+
.. plot::
386+
:context: close-figs
387+
388+
>>> df = pd.read_csv(
389+
... 'https://raw.github.com/pandas-dev/'
390+
... 'pandas/master/pandas/tests/data/iris.csv'
391+
... )
392+
>>> pd.plotting.parallel_coordinates(
393+
... df, 'Name', color=('#556270', '#4ECDC4', '#C7F464')
394+
... )
368395
"""
369396
plot_backend = _get_plot_backend("matplotlib")
370397
return plot_backend.parallel_coordinates(
@@ -398,6 +425,28 @@ def lag_plot(series, lag=1, ax=None, **kwds):
398425
Returns
399426
-------
400427
class:`matplotlib.axis.Axes`
428+
429+
Examples
430+
--------
431+
432+
Lag plots are most commonly used to look for patterns in time series data.
433+
434+
Given the following time series
435+
436+
.. plot::
437+
:context: close-figs
438+
439+
>>> np.random.seed(5)
440+
>>> x = np.cumsum(np.random.normal(loc=1, scale=5, size=50))
441+
>>> s = pd.Series(x)
442+
>>> s.plot()
443+
444+
A lag plot with ``lag=1`` returns
445+
446+
.. plot::
447+
:context: close-figs
448+
449+
>>> pd.plotting.lag_plot(s, lag=1)
401450
"""
402451
plot_backend = _get_plot_backend("matplotlib")
403452
return plot_backend.lag_plot(series=series, lag=lag, ax=ax, **kwds)
@@ -417,6 +466,20 @@ def autocorrelation_plot(series, ax=None, **kwargs):
417466
Returns
418467
-------
419468
class:`matplotlib.axis.Axes`
469+
470+
Examples
471+
--------
472+
473+
The horizontal lines in the plot correspond to 95% and 99% confidence bands.
474+
475+
The dashed line is 99% confidence band.
476+
477+
.. plot::
478+
:context: close-figs
479+
480+
>>> spacing = np.linspace(-9 * np.pi, 9 * np.pi, num=1000)
481+
>>> s = pd.Series(0.7 * np.random.rand(1000) + 0.3 * np.sin(spacing))
482+
>>> pd.plotting.autocorrelation_plot(s)
420483
"""
421484
plot_backend = _get_plot_backend("matplotlib")
422485
return plot_backend.autocorrelation_plot(series=series, ax=ax, **kwargs)

0 commit comments

Comments
 (0)