Python 如何在matplotlib点大小图例中指定点的大小

Python 如何在matplotlib点大小图例中指定点的大小,python,matplotlib,Python,Matplotlib,我的目标是(尽可能优雅地)为散点图制作一个图例,显示散点大小的最小、中等和最大值 我正在尝试使用legend\u elements函数。我已经浏览了和文档,但似乎没有一种方法可以实现我想要的功能 接下来,我试着召唤和我一样多的分数。理由是我可以选择第一个、中间和最后一个点来近似最小值、平均值和最大值。但是,图例显示的最小点和最大点不是数据中的最小点和最大点。为什么调用与采样点相同数量的点图例不能产生最小值和最大值?是否可以使用像legend\u elements这样的辅助函数来构建一个图例,以显

我的目标是(尽可能优雅地)为散点图制作一个图例,显示散点大小的最小、中等和最大值

我正在尝试使用
legend\u elements
函数。我已经浏览了和文档,但似乎没有一种方法可以实现我想要的功能

接下来,我试着召唤和我一样多的分数。理由是我可以选择第一个、中间和最后一个点来近似最小值、平均值和最大值。但是,图例显示的最小点和最大点不是数据中的最小点和最大点。为什么调用与采样点相同数量的点图例不能产生最小值和最大值?是否可以使用像
legend\u elements
这样的辅助函数来构建一个图例,以显示散点图中的最小、中等和最大点尺寸

import matplotlib.pyplot as plt
import numpy as np

# ensure repeatability
np.random.seed(123456)

NSAMPS = 100 # Number of scattered points

idx = [0, NSAMPS // 2, -1] # min-medium-max coordinates

# randomly sample x, y, and size from 0 to 100
RandomMatrix = 100 * np.random.random((NSAMPS, 3))

# make scatter plot
sc = plt.scatter(RandomMatrix[:, 0], RandomMatrix[:, 1], s=RandomMatrix[:, 2])

# recover the same number of element sizes
sizes = sc.legend_elements("sizes", num=NSAMPS)

# parse the sizes for the first, middle, and last entries
args = (list(np.array(sizes[0])[idx]), list(np.array(sizes[1])[idx])) 

# show that the estimated min and max sizes are 1.6 and 98.4, respectively
plt.legend(*args, **{'title': "I want the true min-med-max here! [thanks for taking a look:)]", 'bbox_to_anchor': (1, 1)})

# save figure
plt.savefig('mybigprobelem')

# show that the true minimum and maximum sizes are 1.06 and 98.9
# (I wish for these numbers/sizes to be in the first and third legend positions)
print('True min-max: ', RandomMatrix[:, 2].min(), RandomMatrix[:, 2].max())


args
替换为:

args = (list(np.array(sizes[0])[idx]), np.sort(RandomMatrix[:,2])[idx]) 
输出:


args
替换为:

args = (list(np.array(sizes[0])[idx]), np.sort(RandomMatrix[:,2])[idx]) 
输出:


每个点的大小是否与图例中它旁边的数字完全对应?看起来这只是更改了图例文本,而不是相应的点大小…每个点的大小是否与图例中它旁边的数字完全对应?看起来这只是更改了图例文本,而不是相应的点大小。。。