改变seaborn factorplot中一条线的alpha

use*_*827 3 python matplotlib pandas 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)
Run Code Online (Sandbox Code Playgroud)

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

Ser*_*ity 5

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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明