showing empty plot in nexttile or subplot

I have 4 classes:
Class A: x=[0.1 0.2 0.3 0.6 0.24 0.67],y=[2 1 4 7 4 5],type=[a b a c d b]
Class B: empty
Class C: x=[0.24 0.4 0.32 0.5 0.1],y=[3 1 4 2 6], type=[c a d c b]
Class D: x=[0.5 0.2 0.4 0.1 0.2],y=[4 1 5 2 6], type=[d d b a c]
I want to generate a 2rows 2 columns group plot (nexttile or subplot or any other possible way) such that I can have plot of xy values based upon the color from type variable (the color of a particular type should be same for all classes, for eg. if color of a is red in Class A, then it should be red in Class B,C and D). I also want to include empty Class B to show that there is no data distribution in Class B. Can anyone please help me with this. Thank you

 Accepted Answer

a = "r"; b="g"; c="b"; d="m";
x{1}=[0.1 0.2 0.3 0.6 0.24 0.67];
y{1}=[2 1 4 7 4 5];
t{1}=[a b a c d b]
t = 1×1 cell array
{["r" "g" "r" "b" "m" "g"]}
x{2} = [];
y{2} = [];
t{2} = [];
x{3}=[0.24 0.4 0.32 0.5 0.1];
y{3}=[3 1 4 2 6];
t{3}=[c a d c b];
x{4}=[0.5 0.2 0.4 0.1 0.2];
y{4}=[4 1 5 2 6];
t{4}=[d d b a c];
tiledlayout(2, 2);
for i=1:4
nexttile
hold on
for j=1:length(x{i})
plot(x{i}(j), y{i}(j), "Marker", 'o', "MarkerEdgeColor", t{i}(j), "MarkerFaceColor", t{i}(j))
end
hold off
end

9 Comments

Thank you. I wanted to add some more data to this as my previous question with 16 names. But it is giving me invalid RGB triplets error while using the above function. I have generated x, y and t variables for the above function. t contains a 16 different names distributed among the 6 sections. I couldn't assign the usual rgb values as there are only 7. So i used hexadecimal values for the 16 names and tried the above code but it gave me error. I am still learning so couldn't figure out what might have gone wrong. I just replaced the x, y and t values with my values. I have attached the variables with this comment.
L ooks like that last cell of t is not correct.
load('t.mat'); load('x.mat'); load('y.mat');
%whos
x, y, t
x = 1×6 cell array
{0×0 double} {25×1 double} {0×0 double} {94×1 double} {0×0 double} {37×1 double}
y = 1×6 cell array
{0×0 double} {25×1 double} {0×0 double} {94×1 double} {0×0 double} {37×1 double}
t = 1×6 cell array
{3×1 cell} {37×1 cell} {0×0 double} {94×1 cell} {0×0 double} {25×1 cell}
% concatenate t to get all types available
tall =[];
for i=1:length(t)
if ~isempty(t{i})
tall = [tall; string(t{i})];
end
end
ut=unique(tall); % unique types
ntypes = length(ut)
ntypes = 16
c = jet(ntypes); % different colors for different types
tiledlayout(2, 3);
for i=1:6
nexttile
hold on
% i, size(x{i}), size(y{i}), size(t{i})
for j=1:length(x{i})
if j>=length(t{i}) % this is not necessary if t{i} has enough entries
mc = ntypes; % use last color
else
mc = find(t{i}(j)==ut);
end
%mc = find(t{i}(j)==ut);
plot(x{i}(j), y{i}(j), "Marker", 'o', "MarkerEdgeColor", c(mc,:), "MarkerFaceColor", c(mc,:))
end
hold off
end
l=nexttile('south');
for i=1:ntypes
plot(nan(2,1), 'o', 'MarkerEdgeColor', c(i,:), 'MarkerFaceColor', c(i,:)); hold on
end
l.Visible = 'off';
lgd = legend(l, ut);
lgd.Location = 'east';
lgd.NumColumns = 4;
Thanks a lot. It does plot the data but the legend color doesn't seem to follow the respective name type. And when i added the legend, it shows different values. i tried to check the values and in the (3,1) box, all three values have same color while there are 2 name types here in the data. I have also added the corrected x,y, t here. But the legend colors and the data values seem to mismatch here.
See the update above.
Thank a lot. But the marker color and the legend color are not matching in above figure. You can see that there is dark blue marker in the plot (second and third) but not in the legend. Similarly, there is dark brown marker in the forth and sixth plots but not in the legend.
Chunru
Chunru on 28 Apr 2022
Edited: Chunru on 28 Apr 2022
I don't understand what you mean. All colors in plot are shown in the legend in the program above.
Show what problem you face with the result and code.
Like in my previous comment, I am trying to say that the plot markers and the legend elements are not matching as in the figure below. This is the figure from your updated comment. It is plotting the respective points but somehow the markercolor in the plot are not matching with the legend. For example, I have put red circle in the data points which have unique color but are not present in the legend.
Need some tricks to get the legend correct. See above
Thanks a lot. Yeah, it does seems trickier. I am sorry to say but I think it is still not giving the correct plot. The names seem to get mixed up during concatenation while generating the tall variable. And the x,y value seems to be linked with the random names within tall variable. I have attached the edited x, y and t variables here again after correcting from your first comment. To verify the code, I ran it in these variables but in the first plot with single point, the x and y values are correctly plotted but the name is not matching (It is showing the color of Wairau instead of Conway).

Sign in to comment.

More Answers (1)

Thanks a lot. I finally got the plot. I removed the = sign from if j>=length(t{i}) line to if j>length(t{i}) and it worked.
for j=1:length(x{i})
if j>length(t{i}) % this is not necessary if t{i} has enough entries
mc = ntypes; % use last color
else
mc = find(t{i}(j)==ut);
end
%mc = find(t{i}(j)==ut);

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!