使用 seaborn 将次要网格线添加到 matplotlib 图 [英] Add minor gridlines to matplotlib plot using seaborn

查看:73
本文介绍了使用 seaborn 将次要网格线添加到 matplotlib 图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Seaborn软件包的粉丝,因为他使用Matplotlib制作漂亮的图.但是我似乎无法弄清楚如何在绘图中显示次要网格线.

I'm a fan of the Seaborn package for making nice-looking plots using Matplotlib. But I can't seem to figure out how to show minor gridlines in my plots.

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

给予:

这里有什么想法吗?还有关于如何调整出现的 Seaborn 网格线样式的任何想法......特别是,我很想使它们更窄.

Any thoughts here? Also any thoughts on how to adjust the style of the Seaborn gridlines that do show up...in particular, I'd love to make them narrower.

推荐答案

结合朱卓恩的答案和tcaswell的提示:

Wound up combining CT Zhu's answer with tcaswell's hint:

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)

这篇关于使用 seaborn 将次要网格线添加到 matplotlib 图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆