Scatter series color3 does not match legend when using for-loop MATLAB

Problem :

The colors of the legend in my scatter plot 3 do not match the colors of the series after using the for loop.

Background :

I am writing a script to create a 3D graph based on experimental data. I process the data in R. Thus, the data is formatted as data.frame (i.e. CSV nx7 with headers - this is the cost of value, entropy, risk, confidence, algorithm, color, pch). I want the resulting three-dimensional scattering plot to add a point for each observation (that is, each element [x, y, z]) and colorize it according to its pch value (for example, all observations with pch == 1 must be one colors).

Code

%get & count unique pch values ommitting NANs
UniquePchVal =transpose(unique(pch));
numberOfUniquePchVals=length(UniquePchVal(~isnan(UniquePchVal)))
UniquePchVal = UniquePchVal(1:numberOfUniquePchVals);

% get boolean vector for each series to indicate which observations should
% are to be included
numberOfObservations=length(pch)
UniquePchValMatrix = repmat(UniquePchVal,numberOfObservations,1);
pchMatrix=repmat(pch,1,numberOfUniquePchVals);
rows=UniquePchValMatrix(1:numberOfObservations,1:numberOfUniquePchVals);
rows =(rows==pchMatrix);

%make the plot
hold on
for i=1:numberOfUniquePchVals
   x=ValueGain(rows(:,i));
   y=Cost(rows(:,i));
   z=Risk(rows(:,i));
   size=repmat(20,length(x),1);
   color=repmat(i,length(x),1);
   h(i)=scatter3(x,y,z,size,color,'filled');
end
xlabel('Value Gain')
ylabel('Cost')
zlabel('Risk')
legend(h,unique(algorithm))
hold off

Result

, . The result of the above code

:

Value Gain  Cost    Entropy Risk    Credibility algorithm   color   pch
-0.0    0.0 -0.0    0.0 -0.0    MOEAD   orange  3
-1.39838    1.44    -1.0    2.55555 -1.0    MOEAD   orange  3
-1.41319    4.15    -1.0    1.55555 -1.0    MOEAD   orange  3
-1.39593    3.84    -1.0    1.55555 -1.0    MOEAD   orange  3
-1.25189    1.11    -0.5    1.55555 -1.0    IBEA    cornflowerblue  4
-1.43183    2.87    -0.5    2.55555 -1.0    IBEA    cornflowerblue  4
-1.38953    1.43    -1.0    1.55555 -1.0    NSGA2   #fb8072 5
-1.39585    1.13    -0.33333    1.55555 -1.0    NSGA2   #fb8072 5
-1.1792 1.0 -0.2    2.55555 -1.0    NSGA2   #fb8072 5
-1.38244    1.14    -1.0    1.55555 -1.0    NSGA2   #fb8072 5
-0.05665    0.89    -1.0    2.55543 -1.0    NSGA2   #fb8072 5
-0.1175 0.9 -0.5    1.55556 -1.0    NSGA2   #fb8072 5
-0.06518    0.75    -0.33333    1.55555 -1.0    NSGA2   #fb8072 5
-0.51192    0.98    -0.5    1.55555 -1.0    NSGA2   #fb8072 5
-0.77432    0.98    -1.0    2.55555 -1.0    NSGA2   #fb8072 5
-0.82269    0.94    -0.25   2.55555 -1.0    NSGA2   #fb8072 5
-0.92626    1.0 -1.0    1.55555 -1.0    NSGA2   #fb8072 5
+4
1

, line scatter3; , line . scatter3 Matlab.

line , , :

colors = ['r','g','b'];
hold on
h = [];
for ii = 1:3
    x = rand(10,1);
    y = rand(10,1);
    z = rand(10,1);
    h(ii) = line(x,y,z);
    set(h(ii),...
        'Marker','.',...
        'MarkerEdgeColor',colors(ii),...
        'MarkerFaceColor',colors(ii),...
        'MarkerSize',10,...
        'LineStyle','none')
end
legend('a','b','c')
+1

Source: https://habr.com/ru/post/1615084/


All Articles