Skip to content

Commit

Permalink
DOC: Run doctests for pandas/plotting (#45019)
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke authored Dec 24, 2021
1 parent 149e193 commit 9598f77
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
1 change: 1 addition & 0 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ if [[ -z "$CHECK" || "$CHECK" == "doctests" ]]; then
pandas/core \
pandas/errors/ \
pandas/io/ \
pandas/plotting/ \
pandas/tseries/ \
pandas/util/ \
pandas/_typing.py \
Expand Down
9 changes: 5 additions & 4 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def hist_frame(
>>> np.random.seed(1234)
>>> df = pd.DataFrame(np.random.randn(10, 4),
... columns=['Col1', 'Col2', 'Col3', 'Col4'])
>>> boxplot = df.boxplot(column=['Col1', 'Col2', 'Col3'])
>>> boxplot = df.boxplot(column=['Col1', 'Col2', 'Col3']) # doctest: +SKIP
Boxplots of variables distributions grouped by the values of a third
variable can be created using the option ``by``. For instance:
Expand Down Expand Up @@ -381,7 +381,7 @@ def hist_frame(
.. plot::
:context: close-figs
>>> boxplot = df.boxplot(grid=False, rot=45, fontsize=15)
>>> boxplot = df.boxplot(grid=False, rot=45, fontsize=15) # doctest: +SKIP
The parameter ``return_type`` can be used to select the type of element
returned by `boxplot`. When ``return_type='axes'`` is selected,
Expand Down Expand Up @@ -591,14 +591,14 @@ def boxplot_frame_groupby(
>>> data = np.random.randn(len(index),4)
>>> df = pd.DataFrame(data, columns=list('ABCD'), index=index)
>>> grouped = df.groupby(level='lvl1')
>>> grouped.boxplot(rot=45, fontsize=12, figsize=(8,10))
>>> grouped.boxplot(rot=45, fontsize=12, figsize=(8,10)) # doctest: +SKIP
The ``subplots=False`` option shows the boxplots in a single figure.
.. plot::
:context: close-figs
>>> grouped.boxplot(subplots=False, rot=45, fontsize=12)
>>> grouped.boxplot(subplots=False, rot=45, fontsize=12) # doctest: +SKIP
"""
plot_backend = _get_plot_backend(backend)
return plot_backend.boxplot_frame_groupby(
Expand Down Expand Up @@ -987,6 +987,7 @@ def __call__(self, *args, **kwargs):
>>> s = pd.Series([1, 3, 2])
>>> s.plot.line()
<AxesSubplot:ylabel='Density'>
.. plot::
:context: close-figs
Expand Down
19 changes: 13 additions & 6 deletions pandas/plotting/_matplotlib/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,15 @@ def create_iter_data_given_by(
... [3, 4, np.nan, np.nan], [np.nan, np.nan, 5, 6]]
>>> data = DataFrame(value, columns=mi)
>>> create_iter_data_given_by(data)
{'h1': DataFrame({'a': [1, 3, np.nan], 'b': [3, 4, np.nan]}),
'h2': DataFrame({'a': [np.nan, np.nan, 5], 'b': [np.nan, np.nan, 6]})}
{'h1': h1
a b
0 1.0 3.0
1 3.0 4.0
2 NaN NaN, 'h2': h2
a b
0 NaN NaN
1 NaN NaN
2 5.0 6.0}
"""

# For `hist` plot, before transformation, the values in level 0 are values
Expand Down Expand Up @@ -96,10 +103,10 @@ def reconstruct_data_with_by(
>>> df = DataFrame(d)
>>> reconstruct_data_with_by(df, by='h', cols=['a', 'b'])
h1 h2
a b a b
0 1 3 NaN NaN
1 3 4 NaN NaN
2 NaN NaN 5 6
a b a b
0 1.0 3.0 NaN NaN
1 3.0 4.0 NaN NaN
2 NaN NaN 5.0 6.0
"""
grouped = data.groupby(by)

Expand Down
23 changes: 23 additions & 0 deletions pandas/plotting/_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ def scatter_matrix(
>>> df = pd.DataFrame(np.random.randn(1000, 4), columns=['A','B','C','D'])
>>> pd.plotting.scatter_matrix(df, alpha=0.2)
array([[<AxesSubplot:xlabel='A', ylabel='A'>,
<AxesSubplot:xlabel='B', ylabel='A'>,
<AxesSubplot:xlabel='C', ylabel='A'>,
<AxesSubplot:xlabel='D', ylabel='A'>],
[<AxesSubplot:xlabel='A', ylabel='B'>,
<AxesSubplot:xlabel='B', ylabel='B'>,
<AxesSubplot:xlabel='C', ylabel='B'>,
<AxesSubplot:xlabel='D', ylabel='B'>],
[<AxesSubplot:xlabel='A', ylabel='C'>,
<AxesSubplot:xlabel='B', ylabel='C'>,
<AxesSubplot:xlabel='C', ylabel='C'>,
<AxesSubplot:xlabel='D', ylabel='C'>],
[<AxesSubplot:xlabel='A', ylabel='D'>,
<AxesSubplot:xlabel='B', ylabel='D'>,
<AxesSubplot:xlabel='C', ylabel='D'>,
<AxesSubplot:xlabel='D', ylabel='D'>]], dtype=object)
"""
plot_backend = _get_plot_backend("matplotlib")
return plot_backend.scatter_matrix(
Expand Down Expand Up @@ -208,6 +224,7 @@ def radviz(frame, class_column, ax=None, color=None, colormap=None, **kwds):
... }
... )
>>> pd.plotting.radviz(df, 'Category')
<AxesSubplot:xlabel='y(t)', ylabel='y(t + 1)'>
"""
plot_backend = _get_plot_backend("matplotlib")
return plot_backend.radviz(
Expand Down Expand Up @@ -266,6 +283,7 @@ def andrews_curves(
... 'pandas/master/pandas/tests/io/data/csv/iris.csv'
... )
>>> pd.plotting.andrews_curves(df, 'Name')
<AxesSubplot:title={'center':'width'}>
"""
plot_backend = _get_plot_backend("matplotlib")
return plot_backend.andrews_curves(
Expand Down Expand Up @@ -325,6 +343,7 @@ def bootstrap_plot(series, fig=None, size=50, samples=500, **kwds):
>>> s = pd.Series(np.random.uniform(size=100))
>>> pd.plotting.bootstrap_plot(s)
<Figure size 640x480 with 6 Axes>
"""
plot_backend = _get_plot_backend("matplotlib")
return plot_backend.bootstrap_plot(
Expand Down Expand Up @@ -392,6 +411,7 @@ def parallel_coordinates(
>>> pd.plotting.parallel_coordinates(
... df, 'Name', color=('#556270', '#4ECDC4', '#C7F464')
... )
<AxesSubplot:xlabel='y(t)', ylabel='y(t + 1)'>
"""
plot_backend = _get_plot_backend("matplotlib")
return plot_backend.parallel_coordinates(
Expand Down Expand Up @@ -440,13 +460,15 @@ def lag_plot(series, lag=1, ax=None, **kwds):
>>> x = np.cumsum(np.random.normal(loc=1, scale=5, size=50))
>>> s = pd.Series(x)
>>> s.plot()
<AxesSubplot:xlabel='Midrange'>
A lag plot with ``lag=1`` returns
.. plot::
:context: close-figs
>>> pd.plotting.lag_plot(s, lag=1)
<AxesSubplot:xlabel='y(t)', ylabel='y(t + 1)'>
"""
plot_backend = _get_plot_backend("matplotlib")
return plot_backend.lag_plot(series=series, lag=lag, ax=ax, **kwds)
Expand Down Expand Up @@ -480,6 +502,7 @@ def autocorrelation_plot(series, ax=None, **kwargs):
>>> 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)
<AxesSubplot:title={'center':'width'}, xlabel='Lag', ylabel='Autocorrelation'>
"""
plot_backend = _get_plot_backend("matplotlib")
return plot_backend.autocorrelation_plot(series=series, ax=ax, **kwargs)
Expand Down

0 comments on commit 9598f77

Please sign in to comment.