Changing alpha of a line in seaborn factorplot

狂风中的少年 提交于 2020-12-11 09:07:12

问题


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

In the code above, how can I change the rest line to an alpha of 0.5?


回答1:


Find proper objects of FacetGrid axes with get_children() method and set alpha for lines and markers. To change marker property in legend (g._legend object) find suitable element of legendHandles and apply set_alpha() method:

import seaborn as sns
import matplotlib.pylab as plt

sns.set(style="ticks")
exercise = sns.load_dataset("exercise")
g = sns.factorplot(x="time", y="pulse", hue="kind", data=exercise)

# set alpha for marker (index 0) and for the rest line (indeces 3-6) 
plt.setp([g.ax.get_children()[0],g.ax.get_children()[3:7]],alpha=.5)

# modify marker in legend box
g._legend.legendHandles[0].set_alpha(.5)

plt.show()



来源:https://stackoverflow.com/questions/45556499/changing-alpha-of-a-line-in-seaborn-factorplot

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