绘制2D numpy数组的图例 [英] Plotting legend for 2D numpy array

查看:70
本文介绍了绘制2D numpy数组的图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢使用Numpy和Matplotlib基于某些数字创建图例,但无济于事.因此,在将其转移到我的主脚本之前,我开始尝试使用测试功能以使其正确.

 %matplotlib内联导入matplotlib.pyplot作为plt将numpy导入为npmin_xyz = np.random.randint(5,大小=(50,1,50))无花果= plt.figure(figsize =(7,7))斧= fig.add_subplot(111)ax.set_title('test')plt.imshow(min_xyz [:,0 ,:])ax.set_aspect('等于')ax.set_xlabel('Distance')ax.set_ylabel('深度')ax.legend() 

所以这会创建类似的东西

我想创建显示以下内容的图例:

 矿物1 = colour_1矿物2 = colour_2矿物3 = colour_3矿物4 = colour_4矿物5 = colour_5 

我尝试使用ax.legend(),但似乎无法正确处理.有什么想法吗?

带有垂直色条的解决方案

我从j08lue输入的解决方案-垂直彩条

 %matplotlib内联导入matplotlib.pyplot作为plt将numpy导入为np导入matplotlib.patches作为mpatchesmin_xyz = np.random.randint(5,大小=(50,1,50))无花果,ax = plt.subplots(figsize =(7,40))ax.set_title('test')cax = ax.imshow(min_xyz [:,0 ,:],cmap = plt.cm.Accent)""min_xyz的句柄""cbar = plt.colorbar(cax,ticks = [0,1,2,3,4],方向=垂直",分数= 0.045,填充= 0.05)cbar.ax.set_yticklabels(['矿物1','矿物2','矿物3','矿物4','矿物5'])ax.set_xlabel('Distance')ax.set_ylabel('深度') 

创建自定义图例

我放置了j08lue建议的解决方案,并设法使第一个图例正确.但是,我相信这与色条的规范化有关,以使图例反映正确的颜色.我知道我错过了一些东西,但是我不确定应该寻找什么.任何输入,我们将不胜感激.

 %matplotlib内联导入matplotlib.pyplot作为plt将numpy导入为np导入matplotlib.patches作为mpatchesmin_xyz = np.random.randint(5,大小=(50,50))无花果,ax2 = plt.subplots(figsize =(7,40))斧= fig.add_subplot(111)ax2.set_title('test')cax2 = ax2.imshow(min_xyz,cmap = plt.cm.Accent,vmin = 0,vmax = 4)ax2.set_aspect('等于')""min_xyz的句柄""my_colors = {'矿物1':0.,'Mineral 2':1.,#归一化为0到1'矿物3':2.,'矿物4':3.,'矿物5':4.,}补丁= [mpatches.Patch(color = cmap(v),label = k)for k,v in sorted(my_colors.items(),key = lambda t:t [0])]plt.legend(handles = patches,loc = 2,bbox_to_anchor =(1.01,1))ax2.set_xlabel('Distance')ax2.set_ylabel('深度') 

解决方案

代理艺术家

这可以通过

但是您需要弄清楚哪种颜色对应哪些值.例如

  cmap = plt.cm.viridismy_colors = {'矿物质1':0.1,``矿物质2'':0.2,}补丁= [my_colors.items()中k,v的mpatches.Patch(color = cmap(v),label = k)]plt.legend(handles = patches) 

字典中的数字对应于归一化为[0,1]的数据,当然,您需要使用相同的 cmap 绘制数据.

替代:颜色栏

或者,您可以添加 colorbar (等同于 imshow 图等中的图例)和

Edit: Creating customised legend

I have placed the solution suggested by j08lue and managed to get the first legend correct. However, I believe it got to do with the normalising of the colour bar to get the legend reflect the right colour. I know I am missing something but I am not sure what I should be searching for. Any input is greatly appreciated.

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches

min_xyz = np.random.randint(5, size=(50,50))

fig2, ax2 = plt.subplots(figsize = (7,40))
ax = fig.add_subplot(111)
ax2.set_title('test')
cax2 = ax2.imshow(min_xyz, cmap = plt.cm.Accent, vmin=0, vmax=4)
ax2.set_aspect('equal')

"""Handles for min_xyz"""
 my_colors = {
    'Mineral 1' : 0.,
    'Mineral 2' : 1., # It is normalised to 0 to 1
    'Mineral 3' : 2.,
    'Mineral 4' : 3.,
    'Mineral 5' : 4.,
}
patches = [mpatches.Patch(color=cmap(v), label=k) for k,v in sorted(my_colors.items(), key=lambda t: t[0])]
plt.legend(handles=patches, loc=2, bbox_to_anchor=(1.01,1))

ax2.set_xlabel('Distance')
ax2.set_ylabel('Depth')

解决方案

Proxy artists

This can be done via proxy artists. Example from the docs:

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt

red_patch = mpatches.Patch(color='red', label='The red data')
plt.legend(handles=[red_patch])

plt.show()

But you need to figure out which colours correspond to which values. E.g.

cmap = plt.cm.viridis
my_colors = {
    'Mineral 1' : 0.1,
    'Mineral 2' : 0.2,
    }

patches = [mpatches.Patch(color=cmap(v), label=k) for k,v in my_colors.items()]

plt.legend(handles=patches)

The numbers in the dictionary correspond to the data normalized to [0,1] and you need to plot your data with the same cmap, of course.

Alternative: Colorbar

Alternatively, you can add a colorbar (the equivalent to the legend in imshow plots and the like) and place your labels on the ticks.

cbar = plt.colorbar(cax, ticks=list(my_colors.values()), orientation='horizontal')
cbar.ax.set_xticklabels(list(my_colors.keys()))

这篇关于绘制2D numpy数组的图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆