使用seaborn将次要网格线添加到matplotlib绘图

2024-04-28 10:37:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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

有什么想法吗?还有任何关于如何调整海生网格线的风格的想法…特别是,我想把它们缩小。


Tags: importnumpytruewhichmatplotlibasnpplt
2条回答

这是因为小记号尚未定义,因此我们需要添加例如:

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

最后结合朱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

相关问题 更多 >