在 imshow 和 Legend 中调整散点大小

问题描述

所以我有这个问题,我想通过 ax.imshow 分散数据。由于大小和颜色对于图例很重要,我不想使用颜色条作为图例,而是使用带有标签标记

import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import numpy as np


fig,ax = plt.subplots()

ax.imshow(np.random.rand(100,100),cmap='gist_yarg')

npoints = 5
values = range(1,npoints+1)
sizes = [i**2 for i in values]
cmap = mpl.cm.get_cmap('jet')

ax.scatter(100*np.random.rand(npoints),100*np.random.rand(npoints),marker='.',color=cmap(values/np.max(values)),s=np.array(sizes)*50)

lmarkers = [Line2D([0],[0],color=cmap(values[i]/np.max(values)),ms=sizes[i],ls='',label='{:3.1f}'.format(values[i]))
            for i in range(len(values))]

ax.legend(handles=lmarkers,bBox_to_anchor=(1.3,0.72))

结果:

The size of the markers in the picture almost matches their respective size in the legend

我不得不猜测散点图中的 50,这不是一个完美的匹配,我必须手工完成。 有没有办法通过代码正确设置它? 我不在乎是将它设置在散点图中还是 lmarkers 中。

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)