Seaborn FacetGrid PointPlot Add 1 Grid Line

2019-07-10 04:08发布

问题:

Given the following:

import seaborn as sns
attend = sns.load_dataset("attention")
sns.set_style("whitegrid", {'axes.grid' : False,'axes.edgecolor':'none'})
g = sns.FacetGrid(attend, col="subject", col_wrap=5,
size=1.5, ylim=(0, 10))
ax = g.map(sns.pointplot, "solutions", "score", scale=.7)

As you can see, I have removed all grid lines. I would now like to add just one horizontal grid line: at the value of 5 on the y-axis for each plot. Is this possible to do? I looked into the set_style dictionary options here but found nothing helpful.

Thanks in advance!

回答1:

The easiest way to get a grid line as an aide to the eye in the plot is to just draw a line onto every plot.

for a in g.axes:
    a.axhline(5, alpha=0.5, color='grey')

The upshot is that it's basically one line of code and the plot will have the feature you want. The downshot is that you have to manually specify where each line goes. (I assume that you want something a little more complicated in the production code). Something a little better would be

for a in g.axes:
    a.axhline(a.get_yticks()[1], alpha=0.5, color='grey')

which would grab a single tick and draw a line for it.

You could probably do something with the individual tick objects to give a similar effect---they can be accessed with a.yaxis.get_major_ticks()---but I wasn't able to use any of their methods to any effect.