如何在Matplotlib中将Seaborn散点图的图例移动到外部?

如何在Matplotlib中将Seaborn散点图的图例移动到外部?

要将图例移到Seaborn散点图外部,可以执行以下步骤 –

  • 设置图形大小并调整子图之间和周围填充的间距。
  • 使用三个列,即 column1column2column3 ,创建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()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程