我正在用图例在matplotlib中绘制几个散点图。标记的尺寸很小,因此很难在图例手柄中绘制一些示例点。相反,我想将图例句柄设置为看起来像微小的散点图(即,点状的圆形圆点云)。
我知道可以在调用图例时更改scatterpoints关键字,如图所示(如下图和代码所示),这种方式可以满足我的要求,但是手柄似乎是沿着半水平线分组的,我希望他们看起来比这更随机。
我一直在寻找另一个涵盖该主题的主题,但是运气不太好。我知道这将涉及创建自定义艺术家,并且该线程对此提供了一些见解:如何在matplotlib中制作自定义图例。
在此先感谢您的帮助。
import matplotlib.pyplot as mp
import numpy
a = numpy.random.rand(1000)
b = numpy.random.rand(1000)
c = numpy.random.rand(1000)
d = numpy.random.rand(1000)
fontsize=12
fig = mp.figure(figsize=(3,3))
ax = fig.add_subplot(111)
ax.scatter(a, b, color='0.25', s=1, label='label1')
ax.scatter(c, d, color='firebrick', s=1, label='label2')
ax.tick_params(labelsize=fontsize)
handles, labels = ax.get_legend_handles_labels()
leg = ax.legend(handles, labels, fontsize=fontsize, scatterpoints=10, bbox_to_anchor=(1.03,1.0), bbox_transform=ax.transAxes, loc='upper left', borderaxespad=0, labelspacing=0.25, fancybox=False, edgecolor='0', framealpha=0, borderpad=0.25, handletextpad=0.5, markerscale=1, handlelength=0)
传说有一个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()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句