使用seaborn将小网格线添加到matplotlib图中

时间:2014-02-21 01:35:51

标签: python matplotlib seaborn

我是Seaborn软件包的粉丝,使用Matplotlib制作漂亮的图。但我似乎无法弄清楚如何在我的情节中显示小网格线。

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sbn

x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)

fig, ax = plt.subplots(1, 1)
ax.scatter(x, y)

ax.grid(b=True, which='major')
ax.grid(b=True, which='minor')

给出:

enter image description here

有什么想法吗?还有任何关于如何调整Seaborn网格线的风格的想法......特别是,我希望能让它们更窄。

2 个答案:

答案 0 :(得分:26)

将CT朱的回答与tcaswell的提示结合起来:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sbn

x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)

fig, ax = plt.subplots(1, 1)

ax.scatter(x, y)
ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator())
ax.grid(b=True, which='major', color='w', linewidth=1.0)
ax.grid(b=True, which='minor', color='w', linewidth=0.5)

enter image description here

答案 1 :(得分:7)

那是因为尚未定义次要刻度,所以我们需要添加例如:

ax.set_xticks(np.arange(0,8)-0.5, minor=True)
ax.set_yticks([-1.25, -0.75, -0.25,0.24,0.75,1.25], minor=True)

enter image description here