Skip to content

Commit 2f41991

Browse files
Merge pull request #8877 from sinhrks/plot_doc
DOC: Suppress warnings in visualization.rst
2 parents 726e892 + 82744b9 commit 2f41991

File tree

3 files changed

+109
-117
lines changed

3 files changed

+109
-117
lines changed

doc/source/visualization.rst

+9-9
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,6 @@ The existing interface ``DataFrame.boxplot`` to plot boxplot still can be used.
387387
np.random.seed(123456)
388388
389389
.. ipython:: python
390-
:okwarning:
391390
392391
df = DataFrame(rand(10,5))
393392
plt.figure();
@@ -1271,7 +1270,7 @@ or columns needed, given the other.
12711270
.. ipython:: python
12721271
12731272
@savefig frame_plot_subplots_layout.png
1274-
df.plot(subplots=True, layout=(3, 2), figsize=(6, 6), sharex=False);
1273+
df.plot(subplots=True, layout=(2, 3), figsize=(6, 6), sharex=False);
12751274
12761275
.. ipython:: python
12771276
:suppress:
@@ -1282,22 +1281,23 @@ The above example is identical to using
12821281

12831282
.. ipython:: python
12841283
1285-
df.plot(subplots=True, layout=(3, -1), figsize=(6, 6), sharex=False);
1284+
df.plot(subplots=True, layout=(2, -1), figsize=(6, 6), sharex=False);
12861285
12871286
.. ipython:: python
12881287
:suppress:
12891288
12901289
plt.close('all')
12911290
1292-
The required number of columns (2) is inferred from the number of series to plot
1293-
and the given number of rows (3).
1291+
The required number of columns (3) is inferred from the number of series to plot
1292+
and the given number of rows (2).
12941293

12951294
Also, you can pass multiple axes created beforehand as list-like via ``ax`` keyword.
12961295
This allows to use more complicated layout.
12971296
The passed axes must be the same number as the subplots being drawn.
12981297

1299-
When multiple axes are passed via ``ax`` keyword, ``layout``, ``sharex`` and ``sharey`` keywords are ignored.
1300-
These must be configured when creating axes.
1298+
When multiple axes are passed via ``ax`` keyword, ``layout``, ``sharex`` and ``sharey`` keywords
1299+
don't affect to the output. You should explicitly pass ``sharex=False`` and ``sharey=False``,
1300+
otherwise you will see a warning.
13011301

13021302
.. ipython:: python
13031303
@@ -1306,9 +1306,9 @@ These must be configured when creating axes.
13061306
target1 = [axes[0][0], axes[1][1], axes[2][2], axes[3][3]]
13071307
target2 = [axes[3][0], axes[2][1], axes[1][2], axes[0][3]]
13081308
1309-
df.plot(subplots=True, ax=target1, legend=False);
1309+
df.plot(subplots=True, ax=target1, legend=False, sharex=False, sharey=False);
13101310
@savefig frame_plot_subplots_multi_ax.png
1311-
(-df).plot(subplots=True, ax=target2, legend=False);
1311+
(-df).plot(subplots=True, ax=target2, legend=False, sharex=False, sharey=False);
13121312
13131313
.. ipython:: python
13141314
:suppress:

pandas/tests/test_graphics.py

+100-21
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import itertools
66
import os
77
import string
8+
import warnings
89
from distutils.version import LooseVersion
910

1011
from datetime import datetime, date
@@ -1297,12 +1298,12 @@ def test_subplots_multiple_axes(self):
12971298
df = DataFrame(np.random.rand(10, 3),
12981299
index=list(string.ascii_letters[:10]))
12991300

1300-
returned = df.plot(subplots=True, ax=axes[0])
1301+
returned = df.plot(subplots=True, ax=axes[0], sharex=False, sharey=False)
13011302
self._check_axes_shape(returned, axes_num=3, layout=(1, 3))
13021303
self.assertEqual(returned.shape, (3, ))
13031304
self.assertIs(returned[0].figure, fig)
13041305
# draw on second row
1305-
returned = df.plot(subplots=True, ax=axes[1])
1306+
returned = df.plot(subplots=True, ax=axes[1], sharex=False, sharey=False)
13061307
self._check_axes_shape(returned, axes_num=3, layout=(1, 3))
13071308
self.assertEqual(returned.shape, (3, ))
13081309
self.assertIs(returned[0].figure, fig)
@@ -1319,26 +1320,32 @@ def test_subplots_multiple_axes(self):
13191320
# (show warning is tested in
13201321
# TestDataFrameGroupByPlots.test_grouped_box_multiple_axes
13211322
fig, axes = self.plt.subplots(2, 2)
1322-
df = DataFrame(np.random.rand(10, 4),
1323-
index=list(string.ascii_letters[:10]))
1324-
1325-
returned = df.plot(subplots=True, ax=axes, layout=(2, 1))
1326-
self._check_axes_shape(returned, axes_num=4, layout=(2, 2))
1327-
self.assertEqual(returned.shape, (4, ))
1328-
1329-
returned = df.plot(subplots=True, ax=axes, layout=(2, -1))
1330-
self._check_axes_shape(returned, axes_num=4, layout=(2, 2))
1331-
self.assertEqual(returned.shape, (4, ))
1332-
1333-
returned = df.plot(subplots=True, ax=axes, layout=(-1, 2))
1323+
with warnings.catch_warnings():
1324+
warnings.simplefilter('ignore')
1325+
df = DataFrame(np.random.rand(10, 4),
1326+
index=list(string.ascii_letters[:10]))
1327+
1328+
returned = df.plot(subplots=True, ax=axes, layout=(2, 1),
1329+
sharex=False, sharey=False)
1330+
self._check_axes_shape(returned, axes_num=4, layout=(2, 2))
1331+
self.assertEqual(returned.shape, (4, ))
1332+
1333+
returned = df.plot(subplots=True, ax=axes, layout=(2, -1),
1334+
sharex=False, sharey=False)
1335+
self._check_axes_shape(returned, axes_num=4, layout=(2, 2))
1336+
self.assertEqual(returned.shape, (4, ))
1337+
1338+
returned = df.plot(subplots=True, ax=axes, layout=(-1, 2),
1339+
sharex=False, sharey=False)
13341340
self._check_axes_shape(returned, axes_num=4, layout=(2, 2))
13351341
self.assertEqual(returned.shape, (4, ))
13361342

13371343
# single column
13381344
fig, axes = self.plt.subplots(1, 1)
13391345
df = DataFrame(np.random.rand(10, 1),
13401346
index=list(string.ascii_letters[:10]))
1341-
axes = df.plot(subplots=True, ax=[axes])
1347+
1348+
axes = df.plot(subplots=True, ax=[axes], sharex=False, sharey=False)
13421349
self._check_axes_shape(axes, axes_num=1, layout=(1, 1))
13431350
self.assertEqual(axes.shape, (1, ))
13441351

@@ -3122,13 +3129,14 @@ class TestDataFrameGroupByPlots(TestPlotBase):
31223129
@slow
31233130
def test_boxplot(self):
31243131
grouped = self.hist_df.groupby(by='gender')
3125-
axes = _check_plot_works(grouped.boxplot, return_type='axes')
3132+
with warnings.catch_warnings():
3133+
warnings.simplefilter('ignore')
3134+
axes = _check_plot_works(grouped.boxplot, return_type='axes')
31263135
self._check_axes_shape(list(axes.values()), axes_num=2, layout=(1, 2))
31273136

31283137
axes = _check_plot_works(grouped.boxplot, subplots=False,
31293138
return_type='axes')
31303139
self._check_axes_shape(axes, axes_num=1, layout=(1, 1))
3131-
31323140
tuples = lzip(string.ascii_letters[:10], range(10))
31333141
df = DataFrame(np.random.rand(10, 3),
31343142
index=MultiIndex.from_tuples(tuples))
@@ -3149,6 +3157,30 @@ def test_boxplot(self):
31493157
return_type='axes')
31503158
self._check_axes_shape(axes, axes_num=1, layout=(1, 1))
31513159

3160+
@slow
3161+
def test_grouped_plot_fignums(self):
3162+
n = 10
3163+
weight = Series(np.random.normal(166, 20, size=n))
3164+
height = Series(np.random.normal(60, 10, size=n))
3165+
with tm.RNGContext(42):
3166+
gender = tm.choice(['male', 'female'], size=n)
3167+
df = DataFrame({'height': height, 'weight': weight, 'gender': gender})
3168+
gb = df.groupby('gender')
3169+
3170+
res = gb.plot()
3171+
self.assertEqual(len(self.plt.get_fignums()), 2)
3172+
self.assertEqual(len(res), 2)
3173+
tm.close()
3174+
3175+
res = gb.boxplot(return_type='axes')
3176+
self.assertEqual(len(self.plt.get_fignums()), 1)
3177+
self.assertEqual(len(res), 2)
3178+
tm.close()
3179+
3180+
# now works with GH 5610 as gender is excluded
3181+
res = df.groupby('gender').hist()
3182+
tm.close()
3183+
31523184
def test_series_plot_color_kwargs(self):
31533185
# GH1890
31543186
ax = Series(np.arange(12) + 1).plot(color='green')
@@ -3219,6 +3251,21 @@ def test_grouped_hist(self):
32193251
with tm.assert_produces_warning(FutureWarning):
32203252
df.hist(by='C', figsize='default')
32213253

3254+
@slow
3255+
def test_grouped_hist2(self):
3256+
n = 10
3257+
weight = Series(np.random.normal(166, 20, size=n))
3258+
height = Series(np.random.normal(60, 10, size=n))
3259+
with tm.RNGContext(42):
3260+
gender_int = tm.choice([0, 1], size=n)
3261+
df_int = DataFrame({'height': height, 'weight': weight,
3262+
'gender': gender_int})
3263+
gb = df_int.groupby('gender')
3264+
axes = gb.hist()
3265+
self.assertEqual(len(axes), 2)
3266+
self.assertEqual(len(self.plt.get_fignums()), 2)
3267+
tm.close()
3268+
32223269
@slow
32233270
def test_grouped_box_return_type(self):
32243271
df = self.hist_df
@@ -3334,15 +3381,21 @@ def test_grouped_box_multiple_axes(self):
33343381
self._check_axes_shape(self.plt.gcf().axes, axes_num=4, layout=(2, 2))
33353382

33363383
fig, axes = self.plt.subplots(2, 3)
3337-
returned = df.boxplot(column=['height', 'weight', 'category'], by='gender',
3338-
return_type='axes', ax=axes[0])
3384+
with warnings.catch_warnings():
3385+
warnings.simplefilter('ignore')
3386+
returned = df.boxplot(column=['height', 'weight', 'category'],
3387+
by='gender', return_type='axes', ax=axes[0])
33393388
returned = np.array(list(returned.values()))
33403389
self._check_axes_shape(returned, axes_num=3, layout=(1, 3))
33413390
self.assert_numpy_array_equal(returned, axes[0])
33423391
self.assertIs(returned[0].figure, fig)
3392+
33433393
# draw on second row
3344-
returned = df.groupby('classroom').boxplot(column=['height', 'weight', 'category'],
3345-
return_type='axes', ax=axes[1])
3394+
with warnings.catch_warnings():
3395+
warnings.simplefilter('ignore')
3396+
returned = df.groupby('classroom').boxplot(
3397+
column=['height', 'weight', 'category'],
3398+
return_type='axes', ax=axes[1])
33463399
returned = np.array(list(returned.values()))
33473400
self._check_axes_shape(returned, axes_num=3, layout=(1, 3))
33483401
self.assert_numpy_array_equal(returned, axes[1])
@@ -3469,6 +3522,32 @@ def test_invalid_colormap(self):
34693522
with tm.assertRaises(ValueError):
34703523
df.plot(colormap='invalid_colormap')
34713524

3525+
def test_series_groupby_plotting_nominally_works(self):
3526+
n = 10
3527+
weight = Series(np.random.normal(166, 20, size=n))
3528+
height = Series(np.random.normal(60, 10, size=n))
3529+
with tm.RNGContext(42):
3530+
gender = tm.choice(['male', 'female'], size=n)
3531+
3532+
weight.groupby(gender).plot()
3533+
tm.close()
3534+
height.groupby(gender).hist()
3535+
tm.close()
3536+
#Regression test for GH8733
3537+
height.groupby(gender).plot(alpha=0.5)
3538+
tm.close()
3539+
3540+
def test_plotting_with_float_index_works(self):
3541+
# GH 7025
3542+
df = DataFrame({'def': [1,1,1,2,2,2,3,3,3],
3543+
'val': np.random.randn(9)},
3544+
index=[1.0,2.0,3.0,1.0,2.0,3.0,1.0,2.0,3.0])
3545+
3546+
df.groupby('def')['val'].plot()
3547+
tm.close()
3548+
df.groupby('def')['val'].apply(lambda x: x.plot())
3549+
tm.close()
3550+
34723551

34733552
def assert_is_valid_plot_return_object(objs):
34743553
import matplotlib.pyplot as plt

pandas/tests/test_groupby.py

-87
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@
3535
import pandas as pd
3636
from numpy.testing import assert_equal
3737

38-
def _skip_if_mpl_not_installed():
39-
try:
40-
import matplotlib.pyplot as plt
41-
except ImportError:
42-
raise nose.SkipTest("matplotlib not installed")
4338

4439
def commonSetUp(self):
4540
self.dateRange = bdate_range('1/1/2005', periods=250)
@@ -4670,88 +4665,6 @@ def test_groupby_blacklist(self):
46704665
with tm.assertRaisesRegexp(AttributeError, msg):
46714666
getattr(gb, bl)
46724667

4673-
def test_series_groupby_plotting_nominally_works(self):
4674-
_skip_if_mpl_not_installed()
4675-
4676-
n = 10
4677-
weight = Series(np.random.normal(166, 20, size=n))
4678-
height = Series(np.random.normal(60, 10, size=n))
4679-
with tm.RNGContext(42):
4680-
gender = tm.choice(['male', 'female'], size=n)
4681-
4682-
weight.groupby(gender).plot()
4683-
tm.close()
4684-
height.groupby(gender).hist()
4685-
tm.close()
4686-
#Regression test for GH8733
4687-
height.groupby(gender).plot(alpha=0.5)
4688-
tm.close()
4689-
4690-
def test_plotting_with_float_index_works(self):
4691-
_skip_if_mpl_not_installed()
4692-
4693-
# GH 7025
4694-
df = DataFrame({'def': [1,1,1,2,2,2,3,3,3],
4695-
'val': np.random.randn(9)},
4696-
index=[1.0,2.0,3.0,1.0,2.0,3.0,1.0,2.0,3.0])
4697-
4698-
df.groupby('def')['val'].plot()
4699-
tm.close()
4700-
df.groupby('def')['val'].apply(lambda x: x.plot())
4701-
tm.close()
4702-
4703-
@slow
4704-
def test_frame_groupby_plot_boxplot(self):
4705-
_skip_if_mpl_not_installed()
4706-
4707-
import matplotlib.pyplot as plt
4708-
import matplotlib as mpl
4709-
mpl.use('Agg')
4710-
tm.close()
4711-
4712-
n = 10
4713-
weight = Series(np.random.normal(166, 20, size=n))
4714-
height = Series(np.random.normal(60, 10, size=n))
4715-
with tm.RNGContext(42):
4716-
gender = tm.choice(['male', 'female'], size=n)
4717-
df = DataFrame({'height': height, 'weight': weight, 'gender': gender})
4718-
gb = df.groupby('gender')
4719-
4720-
res = gb.plot()
4721-
self.assertEqual(len(plt.get_fignums()), 2)
4722-
self.assertEqual(len(res), 2)
4723-
tm.close()
4724-
4725-
res = gb.boxplot()
4726-
self.assertEqual(len(plt.get_fignums()), 1)
4727-
self.assertEqual(len(res), 2)
4728-
tm.close()
4729-
4730-
# now works with GH 5610 as gender is excluded
4731-
res = df.groupby('gender').hist()
4732-
tm.close()
4733-
4734-
@slow
4735-
def test_frame_groupby_hist(self):
4736-
_skip_if_mpl_not_installed()
4737-
import matplotlib.pyplot as plt
4738-
import matplotlib as mpl
4739-
mpl.use('Agg')
4740-
tm.close()
4741-
4742-
n = 10
4743-
weight = Series(np.random.normal(166, 20, size=n))
4744-
height = Series(np.random.normal(60, 10, size=n))
4745-
with tm.RNGContext(42):
4746-
gender_int = tm.choice([0, 1], size=n)
4747-
df_int = DataFrame({'height': height, 'weight': weight,
4748-
'gender': gender_int})
4749-
gb = df_int.groupby('gender')
4750-
axes = gb.hist()
4751-
self.assertEqual(len(axes), 2)
4752-
self.assertEqual(len(plt.get_fignums()), 2)
4753-
tm.close()
4754-
47554668
def test_tab_completion(self):
47564669
grp = self.mframe.groupby(level='second')
47574670
results = set([v for v in dir(grp) if not v.startswith('_')])

0 commit comments

Comments
 (0)