Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control gridline spacing in seaborn

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!

like image 982
Dave Anderson Avatar asked Nov 05 '15 09:11

Dave Anderson


People also ask

How do I get rid of gridlines in seaborn?

To get rid of gridlines, use grid=False. To display the figure, use show() method.

How do you set up Darkgrid in seaborn plots?

Set the background to be darkgrid: Darkgrid appear on the sides of the plot on setting it as set_style('darkgrid'). palette attribute is used to set the color of the bars. It helps to distinguish between chunks of data.


2 Answers

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.

like image 146
David R Avatar answered Oct 27 '22 11:10

David R


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

like image 35
tmdavison Avatar answered Oct 27 '22 11:10

tmdavison