python - seaborn中的图例重叠情节区域
问题描述
我使用 seaborn 制作了上面的情节,但我无法将图例正确放置在情节之外。请注意,图例在图像的右侧被切断。这是它的真实外观,我没有手动切割它。这是我正在使用的代码:
sns.lineplot(x="Time", y='Anomaly', style='country', hue='region', size='area', sizes=(1., 4), data=df)
# Put the legend out of the figure
plt.subplots_adjust(right=0.2)
plt.legend(bbox_to_anchor=(.95, 1), loc=2, borderaxespad=0.)
plt.tight_layout()
plt.show()
- 编辑:
这是复制此问题的数据: https ://www.dropbox.com/s/w4gd447e22zb5yk/subset.csv?dl=0
解决方案
您没有为我们指定一个样本集来测试实现并生成绘图,但是通过玩具初始化,修改bbox_to_anchor
似乎可以解决问题。请参阅matplotlib 的图例指南。
bbox_to_anchor
控制手动图例放置。将其设置为将(1,1)
其放置在右上角。
plt.legend(bbox_to_anchor=(1, 1), loc=2, borderaxespad=0.)