简体   繁体   中英

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)

我想摆脱的网格线

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)
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 .

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'])

在此处输入图片说明

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.

You can just set:

sns.set_style("ticks")

It goes back to normal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM