Python 使用带有secondary_y的Seaborn + Pandas绘图时如何摆脱网格线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26868304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:05:52  来源:igfitidea点击:

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

pythonmatplotlibpandasplotseaborn

提问by Artturi Bj?rk

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.

我正在用导入 seaborn 的 Pandas 绘制两个数据系列。理想情况下,我希望左右 y 轴共享水平网格线,但我觉得这很难做到。

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.

作为妥协,我想一起删除网格线。但是,以下代码为辅助 y 轴生成水平网格线。

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

我想摆脱的网格线

回答by DataSwede

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

问题在于使用默认的 Pandas 格式(或您选择的任何格式)。不确定幕后的工作原理,但这些参数胜过您在 plot 函数中传递的格式。您可以在 mpl_style 字典中看到它们的列表

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

在此处输入图片说明

回答by mwaskom

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_stylein a withstatement if you only want to change the default for one figure.

这感觉就像 Pandas 中的错误行为,并不是所有的关键字参数都传递给了两个 Axes。但是如果你想在 seaborn 中默认关闭网格,你只需要调用sns.set_style("dark"). 如果您只想更改一个数字的默认值,也可以sns.axes_stylewith语句中使用。

回答by Doug W

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.

有关这方面的一篇不错的文章,请参阅此站点

回答by Luan Nguyen

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

您可以在绘图后取出 Axes 对象并.grid(False)在两个轴上执行。

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