Artturi Björk
Artturi Björk

Reputation: 3893

How to get rid of grid lines when plotting with Seaborn + Pandas with secondary_y

I'm plotting two data series with Pandas with seaborn imported. Ideally I would like the horizontal grid lines shared between both the left and the right y-axis, but I'm under the impression that this is hard to do.

As a compromise I would like to remove the grid lines all together. The following code however produces the horizontal gridlines for the secondary y-axis.

import pandas as pd
import numpy as np
import seaborn as sns


data = pd.DataFrame(np.cumsum(np.random.normal(size=(100,2)),axis=0),columns=['A','B'])
data.plot(secondary_y=['B'],grid=False)

gridlines that I want to get rid of

Upvotes: 53

Views: 118038

Answers (5)

Marjan
Marjan

Reputation: 301

You can just set:

sns.set_style("ticks")

It goes back to normal.

Upvotes: 5

Luan Nguyen
Luan Nguyen

Reputation: 1073

You can take the Axes object out after plotting and perform .grid(False) on both axes.

# Gets the axes object out after plotting
ax = data.plot(...)

# Turns off grid on the left Axis.
ax.grid(False)

# Turns off grid on the secondary (right) Axis.
ax.right_ax.grid(False)

Upvotes: 72

Doug W
Doug W

Reputation: 631

sns.set_style("whitegrid", {'axes.grid' : False})

Note that the style can be whichever valid one that you choose.

For a nice article on this, refer to this site.

Upvotes: 63

mwaskom
mwaskom

Reputation: 49002

This feels like buggy behavior in Pandas, with not all of the keyword arguments getting passed to both Axes. But if you want to have the grid off by default in seaborn, you just need to call sns.set_style("dark"). You can also use sns.axes_style in a with statement if you only want to change the default for one figure.

Upvotes: 6

DataSwede
DataSwede

Reputation: 5591

The problem is with using the default pandas formatting (or whatever formatting you chose). Not sure how things work behind the scenes, but these parameters are trumping the formatting that you pass as in the plot function. You can see a list of them here in the mpl_style dictionary

In order to get around it, you can do this:

import pandas as pd
pd.options.display.mpl_style = 'default'
new_style = {'grid': False}
matplotlib.rc('axes', **new_style)
data = pd.DataFrame(np.cumsum(np.random.normal(size=(100,2)),axis=0),columns=['A','B'])
data.plot(secondary_y=['B'])

enter image description here

Upvotes: 10

Related Questions