Skip to content

Commit 5df497e

Browse files
author
Artemy Kolchinsky
committed
BUG: plot doesnt default to matplotlib axes.grid setting (#9792)
Cleanup
1 parent f306883 commit 5df497e

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

doc/source/whatsnew/v0.17.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,6 @@ Bug Fixes
7676
- Bug in ``DatetimeIndex`` and ``TimedeltaIndex`` names are lost after timedelta arithmetics ( :issue:`9926`)
7777

7878
- Bug in `Series.plot(label="LABEL")` not correctly setting the label (:issue:`10119`)
79+
- Bug in `plot` not defaulting to matplotlib `axes.grid` setting (:issue:`9792`)
7980

8081

pandas/tests/test_graphics.py

+44
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,38 @@ def _check_box_return_type(self, returned, return_type, expected_keys=None,
439439
else:
440440
raise AssertionError
441441

442+
def _check_grid_settings(self, obj, kinds, kws={}):
443+
# Make sure plot defaults to rcParams['axes.grid'] setting, GH 9792
444+
445+
import matplotlib as mpl
446+
447+
def is_grid_on():
448+
xoff = all(not g.gridOn for g in self.plt.gca().xaxis.get_major_ticks())
449+
yoff = all(not g.gridOn for g in self.plt.gca().yaxis.get_major_ticks())
450+
return not(xoff and yoff)
451+
452+
spndx=1
453+
for kind in kinds:
454+
self.plt.subplot(1,4*len(kinds),spndx); spndx+=1
455+
mpl.rc('axes',grid=False)
456+
obj.plot(kind=kind, **kws)
457+
self.assertFalse(is_grid_on())
458+
459+
self.plt.subplot(1,4*len(kinds),spndx); spndx+=1
460+
mpl.rc('axes',grid=True)
461+
obj.plot(kind=kind, grid=False, **kws)
462+
self.assertFalse(is_grid_on())
463+
464+
if kind != 'pie':
465+
self.plt.subplot(1,4*len(kinds),spndx); spndx+=1
466+
mpl.rc('axes',grid=True)
467+
obj.plot(kind=kind, **kws)
468+
self.assertTrue(is_grid_on())
469+
470+
self.plt.subplot(1,4*len(kinds),spndx); spndx+=1
471+
mpl.rc('axes',grid=False)
472+
obj.plot(kind=kind, grid=True, **kws)
473+
self.assertTrue(is_grid_on())
442474

443475
@tm.mplskip
444476
class TestSeriesPlots(TestPlotBase):
@@ -1108,6 +1140,12 @@ def test_table(self):
11081140
_check_plot_works(self.series.plot, table=True)
11091141
_check_plot_works(self.series.plot, table=self.series)
11101142

1143+
@slow
1144+
def test_series_grid_settings(self):
1145+
# Make sure plot defaults to rcParams['axes.grid'] setting, GH 9792
1146+
self._check_grid_settings(Series([1,2,3]),
1147+
plotting._series_kinds + plotting._common_kinds)
1148+
11111149

11121150
@tm.mplskip
11131151
class TestDataFramePlots(TestPlotBase):
@@ -3426,6 +3464,12 @@ def test_sharey_and_ax(self):
34263464
"y label is invisible but shouldn't")
34273465

34283466

3467+
@slow
3468+
def test_df_grid_settings(self):
3469+
# Make sure plot defaults to rcParams['axes.grid'] setting, GH 9792
3470+
self._check_grid_settings(DataFrame({'a':[1,2,3],'b':[2,3,4]}),
3471+
plotting._dataframe_kinds, kws={'x':'a','y':'b'})
3472+
34293473

34303474
@tm.mplskip
34313475
class TestDataFrameGroupByPlots(TestPlotBase):

pandas/tools/plotting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ def __init__(self, data, kind=None, by=None, subplots=False, sharex=None,
810810
self.rot = self._default_rot
811811

812812
if grid is None:
813-
grid = False if secondary_y else True
813+
grid = False if secondary_y else self.plt.rcParams['axes.grid']
814814

815815
self.grid = grid
816816
self.legend = legend

0 commit comments

Comments
 (0)