【问题标题】:Changing alpha of a line in seaborn factorplot改变seaborn factorplot中一条线的alpha
【发布时间】:2018-01-15 07:49:13
【问题描述】:
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?
【问题讨论】:
标签:
python
pandas
matplotlib
seaborn
【解决方案1】:
使用get_children() 方法找到FacetGrid 轴的正确对象,并为线条和标记设置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()