matplotlib 的图例(散点图)

Legends with matplotlib (scatterplot)

看看第二个图 here,我目前正在使用 散点图 ,当我绘制图例时,我得到两个 "sample points"(在link 他们是两颗蓝色的星星)。我怎样才能只显示其中一个?为什么 matplotlib 绘制其中的 2 个?

我使用的代码类型是

plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), mode="expand", borderaxespad=0.)

我是新手,所以我还没有使用 'legend handles'。

提前致谢

对于散点图,您需要设置 scatterpoints

import matplotlib.lines as mlines
import matplotlib.pyplot as plt

plt.figure(figsize=(6, 2))

plt.subplot(1, 2, 1)
plt.scatter([1, 2, 3, 4, 5], [1, 2, 5, 3, 4])
plt.legend(['text'])

plt.subplot(1, 2, 2)
plt.scatter([1, 2, 3, 4, 5], [1, 2, 5, 3, 4])
plt.legend(['text'], scatterpoints=1)

plt.show()