Plot Matlab colours as vertical bars

故事扮演 提交于 2019-12-11 10:25:52

问题


I stumbled upon this file exchange submission, which, given a positive integer, generates that many "maximally distinguishable" colours. The tool is working great, but I would like to visualize the colours it generates with coloured vertical bands. An example, taken from the linked blog article:

For the choice of colours:

ans =
         0         0    1.0000
    1.0000         0         0
         0    1.0000         0
         0         0    0.1724
    1.0000    0.1034    0.7241
    1.0000    0.8276         0
         0    0.3448         0

We obtain the vertical bands on the left that show these colours.


回答1:


A fairly simple way would be as follows:

a = [     0         0    1.0000 ;
     1.0000         0         0 ;
          0    1.0000         0 ;
          0         0    0.1724 ;
     1.0000    0.1034    0.7241 ;
     1.0000    0.8276         0 ;
          0    0.3448         0 ]

figure
imagesc(1:size(a, 1));
colormap(a);
% Optional, but neatens things up a bit
set(gca, 'clim', [0.5 (size(a, 1) + 0.5)]);

% Also optional, removes the ticks from the axes
set(gca, 'xtick', [], 'ytick', []);

output:




回答2:


Rectangles can easily be drawn with the command rectangle():

z = [      0         0    1.0000
    1.0000         0         0
         0    1.0000         0
         0         0    0.1724
    1.0000    0.1034    0.7241
    1.0000    0.8276         0
         0    0.3448         0];

     h = 6; % Heigth rectangle
     w = 1  % Width rectangle

     n = size(z,1); % Colours in z

     x = 1:w:w*n;

     for ii = 1:n
     rectangle('Position',[x(ii),0,w,h],'FaceColor',z(ii,:))
     end
     axis off;



回答3:


Here's one way, using the low-level patch function to create the color strips:

c = [     0         0    1.0000
     1.0000         0         0
          0    1.0000         0
          0         0    0.1724
     1.0000    0.1034    0.7241
     1.0000    0.8276         0
          0    0.3448         0];

n = size(c,1);

figure;
x = [0:n-1; 1:n; ...
     1:n;   0:n-1];
y = [zeros(2, n); ones(2, n)];
patch('XData', x, 'YData', y, ...
      'EdgeColor', 'none', ...
      'FaceColor', 'flat', ...
      'FaceVertexCData', c);
axis off;

which yields a plot like this

You can play with the x and y values to scale the width an height of the strips if you want to change the aspect ratio.



来源:https://stackoverflow.com/questions/33600134/plot-matlab-colours-as-vertical-bars

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