传说中有一个scatteryoffsets
争论。您可以提供 y 坐标列表。这些应该在 0 和 1 之间。
yoffsets = [.1,.7,.3,.1,.8,.4,.2,.6,.7,.5]
plt.legend(scatteryoffsets=yoffsets, scatterpoints=len(yoffsets) )
import matplotlib.pyplot as plt
import numpy
import matplotlib.legend_handler
import matplotlib.collections
a = numpy.random.rand(1000)
b = numpy.random.rand(1000)
c = numpy.random.rand(1000)
d = numpy.random.rand(1000)
fontsize=12
fig = plt.figure(figsize=(3,3))
ax = fig.add_subplot(111)
sc = ax.scatter(a, b, color='0.25', s=1, label='label1')
sc2 = ax.scatter(c, d, color='firebrick', s=1, label='label2')
ax.tick_params(labelsize=fontsize)
yoffsets = [.1,.7,.3,.1,.8,.4,.2,.6,.7,.5]
plt.legend(scatteryoffsets=yoffsets, scatterpoints=len(yoffsets),
framealpha=1)
plt.show()