Produce a 3D stem plot with a custom colormap in MATLAB

后端 未结 2 673
逝去的感伤
逝去的感伤 2020-12-20 06:32

I have a matrix (200 x 4) where first 3 values are X, Y and Z data. I want use the fourth column to display each (X,Y,Z)

2条回答
  •  一个人的身影
    2020-12-20 07:11

    You can use HSV as well. The Z values would correspond to your fourth column. Low Z values are blue and high Z values are red.

    enter image description here

    I used the site http://colorizer.org/ to work out that blue is H=0.65 and red is H=1. S and V stay the same.

    From http://colorizer.org/, I got that a blue colour is H=236, S=100, V=100. Then the H value for blue is H = 235/360 = 0.65 and H=1, S=1, V=1 for red.


    num_elem = 200;
    c = linspace(0,1,num_elem)'; % // Replace this with the values from your fourth column
    
    
    % // The equation gives blue (H=0.65) for c=0 and red (H=1) for c = 1
    H = 0.65 + ((1-0.65).* c); 
    S = ones(size(c,1),1);
    V = ones(size(c,1),1);
    
    % // You have to convert it to RGB to be compatible with stem3
    
    colourmap = hsv2rgb([H,S,V]);
    
    
    % // Generate some sample data
    theta = linspace(0,2*pi,num_elem)';
    X = cos(theta);
    Y = sin(theta);
    Z = theta;
    
    % // Plot the sample data with the colourmap
    figure;
    hold on;
    for idx=1:num_elem
      stem3(X(idx),Y(idx),Z(idx),':*','Color',colourmap(idx,:) ...
      ,'MarkerEdgeColor',colourmap(idx,:) ...
      ,'MarkerFaceColor',colourmap(idx,:) ...
      ,'LineWidth',4 ...
      );
    end
    
    hold off;
    set(gca,'FontSize',36');
    

提交回复
热议问题