Seaborn FacetGrid PointPlot Add 1 Grid Line

醉酒当歌 提交于 2020-01-03 19:15:29

问题


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.



来源:https://stackoverflow.com/questions/45848281/seaborn-facetgrid-pointplot-add-1-grid-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!