pythonmatplotlibseaborngridlines

Control seaborn gridline spacing


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!


Solution

  • 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