How to add legend in imagesc plot in matlab

﹥>﹥吖頭↗ 提交于 2019-12-21 11:45:23

问题


I have the following code, creating figures which I print to files:

f=figure;
set(gcf,'Visible','off');
imagesc (exp_genes_sorted_cut);
h=colorbar;
set(gcf,'Colormap',mycmap);

set(gca, 'xtick', 1:num_tissues_displayed);
set(gca, 'xticklabel', tissues, 'fontsize', 14);
ylabel('Genes', 'Fontsize', 18);
xlabel('Tissues', 'Fontsize', 18);

I want to add legend to the right of the colorbar, and I tried doing so using the legend function, but it is not shown... using the text function places it outside the printanle area. Can anyone help ? Thanks,,,


回答1:


A possible workaround (if I understood you correctly):

 N=4;                                                    %  # of data types, hence legend entries
 Data = randi(N,30,30);                                  % generate fake data
 imagesc(Data)                                           % image it
 cmap = jet(N);                                          % assigen colormap
 colormap(cmap)
 hold on
 L = line(ones(N),ones(N), 'LineWidth',2);               % generate line 
 set(L,{'color'},mat2cell(cmap,ones(1,N),3));            % set the colors according to cmap
 legend('A','B','C','D')                                 % add as many legend entries as data



来源:https://stackoverflow.com/questions/12902709/how-to-add-legend-in-imagesc-plot-in-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!