Control seaborn gridline spacing
Asked Answered
C

2

12

I'd like to change the spacing of the horizontal grid lines on a seaborn chart, I've tried setting the style with no luck:

seaborn.set_style("whitegrid", {
    "ytick.major.size": 0.1,
    "ytick.minor.size": 0.05,
    'grid.linestyle': '--'
 })

bar(range(len(data)),data,alpha=0.5)
plot(avg_line)

The gridlines are set automatically desipite me trying to overide the tick size

enter image description here

Any suggestions? Thanks!

Cowherb answered 5/11, 2015 at 9:40 Comment(1)
It's not quite clear what you mean by "gridline spacing". Do you mean the length of the dashes? Or the frequency of the grid lines (i.e. here it's every 10 on the x axis)? Or something else?Airtoair
W
13

you can set the tick locations explicitly later, and it will draw the grid at those locations.

The neatest way to do this is to use a MultpleLocator from the matplotlib.ticker module.

For example:

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

sns.set_style("whitegrid", {'grid.linestyle': '--'})

fig,ax = plt.subplots()
ax.bar(np.arange(0,50,1),np.random.rand(50)*0.016-0.004,alpha=0.5)

ax.yaxis.set_major_locator(ticker.MultipleLocator(0.005))

plt.show()

enter image description here

Wacke answered 5/11, 2015 at 10:58 Comment(0)
U
10

The OP asked about modifying tick distances in Seaborn.

If you are working in Seaborn and you use a plotting feature that returns an Axes object, then you can work with that just like any other Axes object in matplotlib. For example:

import matplotlib.pyplot as plt
import seaborn as sns
import statsmodels.api as sm
from matplotlib.ticker import MultipleLocator

df = sm.datasets.get_rdataset("Guerry", "HistData").data

ax = sns.scatterplot('Literacy', 'Lottery', data=df)

ax.yaxis.set_major_locator(MultipleLocator(10))
ax.xaxis.set_major_locator(MultipleLocator(10))

plt.show()

Put if you are working with one of the Seaborn processes that involve FacetGrid objects, you will see precious little help on how to modify the tick marks without manually setting them. You have dig out the Axes object from the numpy array inside FacetGrid.axes .

import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.ticker import MultipleLocator

tips = sns.load_dataset("tips")
g = sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips, )

g.axes[0][0].yaxis.set_major_locator(MultipleLocator(3))

Note the double subscript required. g is a FacetGrid object, which holds a two-dimensional numpy array of dtype=object, whose entries are matplotlib AxesSubplot objects.

If you are working with a FacetGrid that has multiple axes, then each one will have to be extracted and modified.

Understate answered 5/9, 2019 at 20:45 Comment(1)
This is such a gem i don't even know. To add to the comment, Seaborn's catplot functions have this behavior and do not lend their axes easily to the usual plt customization given that they return a FacetGridPittance

© 2022 - 2024 — McMap. All rights reserved.