微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 imshow 和 Legend 中调整散点大小

如何解决在 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 举报,一经查实,本站将立刻删除。