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

后端 未结 2 490
说谎
说谎 2020-12-21 18:05

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

相关标签:
2条回答
  • 2020-12-21 18:37

    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);
    
    0 讨论(0)
  • 2020-12-21 18:46

    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
    

    0 讨论(0)
提交回复
热议问题