Add minor gridlines
Asked Answered
D

2

35

But I can't seem to figure out how to show minor gridlines in my plots with a seaborn style.

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

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.

Dulce answered 21/2, 2014 at 1:35 Comment(0)
D
46

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)

enter image description here

Dulce answered 21/2, 2014 at 3:17 Comment(2)
Can you open an issue on the seaborn github about this? You shouldn't need to style the minor gridlines yourself, but I think that's an oversight in the current code.Hapte
breadcrumb: github.com/mwaskom/seaborn/issues/110 (more or less unresolved)Cardin
S
11

That's because the minor ticks are not yet defined, so we need to add for example:

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

Sandrocottus answered 21/2, 2014 at 2:16 Comment(1)
It is better to ax.get_xaxis().set_minor_locator(...)Merci

© 2022 - 2024 — McMap. All rights reserved.