如何设置seaborn lineplot图例标记应用fillstyle='none'无填充
seaborn折线图图例标记无填充设置方法
绘制折线图时通过fillstyle='none'设置绘图区域标记无填充后,图例标记默认不会同步该样式,可通过手动修改图例句柄属性解决。
问题复现代码
import seaborn as sns tips = sns.load_dataset('tips') ax = sns.lineplot( data=tips, x='size', y='tip', hue='sex', ci=None, style='sex', markers=True, fillstyle='none', markeredgecolor=None, markersize=10, )

修复代码
在原有绘图代码后追加图例样式修改逻辑即可:
import seaborn as sns import matplotlib.pyplot as plt tips = sns.load_dataset('tips') ax = sns.lineplot( data=tips, x='size', y='tip', hue='sex', ci=None, style='sex', markers=True, fillstyle='none', markeredgecolor=None, markersize=10, ) # 同步图例标记的无填充设置 leg = ax.legend() for marker in leg.legendHandles: marker.set_fillstyle('none') plt.show()
- 核心逻辑:seaborn生成图例时不会自动继承plot阶段传入的marker填充属性,需要遍历图例内所有标记对象,手动设置
fillstyle='none'匹配图内样式。
内容的提问来源于stack exchange,提问作者Probability is wonderful
相关产品推荐
相关产品推荐

