问题
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