如何在Matplotlib中将Seaborn散点图的图例移动到外部? 要将图例移到Seaborn散点图外部,可以执行以下步骤 – 设置图形大小并调整子图之间和周围填充的间距。 使用三个列,即 column1 , column2 和 column3 ,创建Pandas数据框。 使用可能存在多个语义分组的散点图。 要将图例放置在图外,请在 legend() 方法中使用 bbox_to_anchor 。 使用 show() 方法显示图。 示例 import matplotlib.pyplot as plt import pandas as pd import seaborn as sns plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame(dict(col1=[2, 1, 4], col2=[5, 2, 1], col3=[4, 0, 1])) sns.scatterplot(data=df) plt.legend(bbox_to_anchor=(1.25, 1), borderaxespad=0) plt.show() PythonCopy