gpt4 book ai didi

Matplotlib 散点图图例 : Customize handles to look like tiny scatter plots

转载 作者:行者123 更新时间:2023-12-03 17:53:03 25 4
gpt4 key购买 nike

我正在用图例在 matplotlib 中制作几个散点图。标记尺寸很小,因此在图例 handle 中仅绘制几个示例点将很难看到。相反,我想将图例 handle 格式化为看起来像微小的散点图(即点的小圆形点云)。

我知道在调用图例时可以更改 scatterpoints 关键字,如图(下图和代码)所示,这种方式可以满足我的要求,但是 handle 似乎沿着半水平线分组,我希望它们看起来比这更随机。

HERE

我已经寻找了涵盖该主题的另一个线程,但运气不佳。我知道这将涉及创建自定义艺术家,此线程对此提供了一些见解:How to make custom legend in 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) )

enter image description here
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()

关于Matplotlib 散点图图例 : Customize handles to look like tiny scatter plots,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45041149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com