Repository: pandas-dev/pandas

Issue:
Pandas DataFrame.plot() does not default to matplotlib rcparams grid setting
When using df.plot() to creat a simple line chart from a dataframe, the expected behavior is that, if no 'grid' keyword is passed, the plot will default to the matplotlib 'axes.grid' rcParam setting to determine whether to show a grid or not.  However (in 0.16), the grid is always being shown, regardless of the matplotlib setting, unless I explicitly pass grid=False.

In general, there are several plot types where grid=True is forced, which basically breaks the consistent styling that you get from matplotlib rcparams or another package like Seaborn.  I can understand forcing a default grid=False for the type of plots where a grid doesn't make sense, but forcing grid=True, overriding the configured defaults, for things like boxplots seems a bit counter intuitive.



Discussion:
Agreed for the most part, but where do we draw the line (sorry for the bad pun). I'm not sure which plots grids makes sense on. I guess have grids for line, scatter, density. No grid for box. On barplots I usually only have gridlines along the `y` axis, but I'm not sure that's the best default. And which ones do we fall back to rcParams on?

my opinion is that they should fall back on rcparams for almost everything, except those where grid lines make no sense at all.  The problem with overriding rcparams is that it 'breaks' the default matplotlib behavior for people who are used to using matplotlib.

I also ran into this issue and found it very confusing and --- worst of all --- in conflict with the documentation (which says that not passing in grid will result in matplotlib default)

Should be a pretty simple fix if you're up to submitting a PR.

The signature should still be `df.plot(..., grid=None, ...)`.

If the user specifies `True` or `False`, we use that.

If they leave the default of `None`, we either
- override `plt.RcParams` for `box` and `pie` (setting `grid=False`)
- fall back to rcParams otherwise


Resolving patch:
diff --git a/pandas/tools/plotting.py b/pandas/tools/plotting.py
--- a/pandas/tools/plotting.py
+++ b/pandas/tools/plotting.py
@@ -810,7 +810,7 @@ def __init__(self, data, kind=None, by=None, subplots=False, sharex=None,
                 self.rot = self._default_rot
 
         if grid is None:
-            grid = False if secondary_y else True
+            grid = False if secondary_y else self.plt.rcParams['axes.grid']
 
         self.grid = grid
         self.legend = legend


Test patch:
diff --git a/pandas/tests/test_graphics.py b/pandas/tests/test_graphics.py
--- a/pandas/tests/test_graphics.py
+++ b/pandas/tests/test_graphics.py
@@ -439,6 +439,38 @@ def _check_box_return_type(self, returned, return_type, expected_keys=None,
                 else:
                     raise AssertionError
 
+    def _check_grid_settings(self, obj, kinds, kws={}):
+        # Make sure plot defaults to rcParams['axes.grid'] setting, GH 9792
+
+        import matplotlib as mpl
+
+        def is_grid_on():
+            xoff = all(not g.gridOn for g in self.plt.gca().xaxis.get_major_ticks())
+            yoff = all(not g.gridOn for g in self.plt.gca().yaxis.get_major_ticks())
+            return not(xoff and yoff)
+
+        spndx=1
+        for kind in kinds:
+            self.plt.subplot(1,4*len(kinds),spndx); spndx+=1
+            mpl.rc('axes',grid=False)
+            obj.plot(kind=kind, **kws)
+            self.assertFalse(is_grid_on())    
+
+            self.plt.subplot(1,4*len(kinds),spndx); spndx+=1
+            mpl.rc('axes',grid=True)
+            obj.plot(kind=kind, grid=False, **kws)
+            self.assertFalse(is_grid_on())    
+
+            if kind != 'pie':
+                self.plt.subplot(1,4*len(kinds),spndx); spndx+=1
+                mpl.rc('axes',grid=True)
+                obj.plot(kind=kind, **kws)
+                self.assertTrue(is_grid_on())
+
+                self.plt.subplot(1,4*len(kinds),spndx); spndx+=1
+                mpl.rc('axes',grid=False)
+                obj.plot(kind=kind, grid=True, **kws)
+                self.assertTrue(is_grid_on())
 
 @tm.mplskip
 class TestSeriesPlots(TestPlotBase):
@@ -1108,6 +1140,12 @@ def test_table(self):
         _check_plot_works(self.series.plot, table=True)
         _check_plot_works(self.series.plot, table=self.series)
 
+    @slow
+    def test_series_grid_settings(self):
+        # Make sure plot defaults to rcParams['axes.grid'] setting, GH 9792
+        self._check_grid_settings(Series([1,2,3]), 
+            plotting._series_kinds + plotting._common_kinds)
+
 
 @tm.mplskip
 class TestDataFramePlots(TestPlotBase):
@@ -3426,6 +3464,12 @@ def test_sharey_and_ax(self):
                              "y label is invisible but shouldn't")
 
 
+    @slow
+    def test_df_grid_settings(self):
+        # Make sure plot defaults to rcParams['axes.grid'] setting, GH 9792
+        self._check_grid_settings(DataFrame({'a':[1,2,3],'b':[2,3,4]}), 
+            plotting._dataframe_kinds, kws={'x':'a','y':'b'})
+        
 
 @tm.mplskip
 class TestDataFrameGroupByPlots(TestPlotBase):