How to assign colors to each value in scatter function - GNU Octave

末鹿安然 提交于 2019-11-28 10:08:52

问题


How can I use the scatter function in GNU Octave in order to assign colors to each plotted value ?


回答1:


You have to change colormap and 4th parameter of octave's scatter. The parameter is vector 1xn indexing colormap. You can have maximally 255^3 vectors. And how to do that?

Example solution:

  1. Set colormap (matrix 3 x n), which will contain each color exactly ones
  2. Use as 4th parameter vector, which is containing each number exactly ones, and has same size like x and y

clf;
x = randn (100, 1);
y = randn (100, 1);
cmap=[];
for R = 1:255
  for G = 1:255
    for B = 1:255
      if (size(cmap) ./ [1,3] == size(x))
        break;
      endif
      cmap=[cmap;R/255,G/255,B/255];

    endfor
    if (size(cmap) ./ [1,3] == size(x))
        break;
    endif
  endfor 
 if (size(cmap) ./ [1,3] == size(x))
        break;
 endif 
endfor

colormap(cmap);

scatter(x,y,20, 1:100);



回答2:


Here's a nice illustrative example:

X = linspace(0, 4 * pi, 100);                       % Create points (100 elements)
Y = 100 * sin(X); 

S = Y + 100;                                        % Sizes array. All values need 
                                                    % to be larger than 0

C = [linspace(0,1,100); ones(1,100); ones(1,100)]'; % create hsv triplets at full 
                                                    % saturation and value that
                                                    % cover the whole colour 
                                                    % (i.e. hues) spectrum

C = hsv2rgb(C);                                     % convert to rgb triplets; to
                                                    % be used as a 'colour array'

scatter(X,Y,S,C,'filled','MarkerEdgeColor','k');    % fill bubbles, black border



来源:https://stackoverflow.com/questions/38550379/how-to-assign-colors-to-each-value-in-scatter-function-gnu-octave

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