Skip to content

Commit 688ae3a

Browse files
author
Artemy Kolchinsky
committed
BUG: plot doesnt default to matplotlib axes.grid setting (pandas-dev#9792)
1 parent f306883 commit 688ae3a

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-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

+45
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,39 @@ 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+
#plt.figure()
471+
self.plt.subplot(1,4*len(kinds),spndx); spndx+=1
472+
mpl.rc('axes',grid=False)
473+
obj.plot(kind=kind, grid=True, **kws)
474+
self.assertTrue(is_grid_on())
442475

443476
@tm.mplskip
444477
class TestSeriesPlots(TestPlotBase):
@@ -1108,6 +1141,12 @@ def test_table(self):
11081141
_check_plot_works(self.series.plot, table=True)
11091142
_check_plot_works(self.series.plot, table=self.series)
11101143

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

11121151
@tm.mplskip
11131152
class TestDataFramePlots(TestPlotBase):
@@ -3426,6 +3465,12 @@ def test_sharey_and_ax(self):
34263465
"y label is invisible but shouldn't")
34273466

34283467

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

34303475
@tm.mplskip
34313476
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)