How to get rid of grid lines when plotting with Se

2020-05-19 21:44发布

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

4条回答
Root(大扎)
2楼-- · 2020-05-19 22:04

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.

查看更多
smile是对你的礼貌
3楼-- · 2020-05-19 22:07
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.

查看更多
forever°为你锁心
4楼-- · 2020-05-19 22:09

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(False)
查看更多
祖国的老花朵
5楼-- · 2020-05-19 22:29

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

查看更多
登录 后发表回答