改变seaborn factorplot中一条线的alpha

时间:2017-08-07 22:16:19

标签: python pandas matplotlib seaborn

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

在上面的代码中,如何将rest行更改为0.5的alpha?

1 个答案:

答案 0 :(得分:3)

使用FacetGrid方法查找get_children()轴的正确对象,并为线条和标记设置alpha。要更改图例(g._legend对象)中的标记属性,请找到legendHandles的合适元素并应用set_alpha()方法:

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()

enter image description here