18

How can I add grid lines (vertically and horizontally) to a seaborn catplot? I found a possibility to do that on a boxplot, but I have multiple facets and therefore need a catplot instead. And in contrast to this other answer, catplot does not allow an ax argument.

This code is borrowed from here.

import seaborn as sns
sns.set(style="ticks")
exercise = sns.load_dataset("exercise")
g = sns.catplot(x="time", y="pulse", hue="kind", data=exercise)
plt.show()

Any ideas? Thank you!

EDIT: The provided answer is working, but for faceted plots, only the last plot inherits the grid.

import seaborn as sns
sns.set(style="ticks")
exercise = sns.load_dataset("exercise")
g = sns.catplot(x="time", y="pulse", hue="kind", col="diet", data=exercise)
plt.grid()
plt.show()

Can someone explain to me why and how to fix it?

Revan
  • 2,072
  • 4
  • 26
  • 42

3 Answers3

33

You can set the grid over seaborn plots in two ways:

1. plt.grid() method:

You need to use the grid method inside matplotlib.pyplot. You can do that like so:

import seaborn as sns
import matplotlib.pyplot as plt

sns.set(style="ticks")
exercise = sns.load_dataset("exercise")
g = sns.catplot(x="time", y="pulse", hue="kind", data=exercise)
plt.grid()  #just add this
plt.show()

Which results in this graph: enter image description here

2. sns.set_style() method

You can also use sns.set_style which will enable grid over all subplots in any given FacetGrid. You can do that like so:

import seaborn as sns
import matplotlib.pyplot as plt


sns.set(style="ticks")
exercise = sns.load_dataset("exercise")
sns.set_style("darkgrid")
g = sns.catplot(x="time", y="pulse", hue="kind", col="diet", data=exercise)
plt.show()

Which returns this graph: enter image description here

Anwarvic
  • 12,156
  • 4
  • 49
  • 69
  • Thank you very much. Can you explain to me why this is faiilng for faceted plots? See updated question. – Revan Apr 29 '20 at 15:27
  • 2
    I edited my answer, see the second method. Hope this is helpful! – Anwarvic Apr 29 '20 at 17:49
  • @Johannes The first solution to this question will give you vertical grid lines on your faceted plots: https://stackoverflow.com/questions/65029406/how-to-add-vertical-gridlines-in-seaborn-catplot-with-multiple-column-plots – cjstevens Jul 23 '21 at 17:48
12

Came into this question looking for a way of adding grids to a FacetGrid plot, without using the "whitegrid" style. After trying many solutions, I found that in faceted plots one has to add {'axes.grid' : True} to the set_style function:

import seaborn as sns
sns.set_style("ticks",{'axes.grid' : True})
g = sns.FacetGrid(df, col="column_variable",col_wrap=4, height=2.3)
  • 1
    This worked great, and is even simpler as it directly uses a seaborn function. It is working for me in with `relplot` – Salvatore Cosentino Jun 18 '21 at 05:27
  • This is very easy, but similar to @Anwarvic sns.set_style() method above, it only gives horizontal grid lines. – cjstevens Jul 23 '21 at 17:35
  • I found this solution here: https://stackoverflow.com/questions/65029406/how-to-add-vertical-gridlines-in-seaborn-catplot-with-multiple-column-plots – cjstevens Jul 23 '21 at 17:49
1

This is the correct way to add grids for all axes:

g = sns.catplot(x="time", y="pulse", hue="kind", col="diet", data=exercise)

for ax in g.axes.flatten():
    ax.grid()  
Soerendip
  • 7,684
  • 15
  • 61
  • 128