You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Plot data in matlab
8 views (last 30 days)
Show older comments
Hi, I have 3 variables which are basically in form of % and I would like to build a trent plot for these
Example:
03/06/2023 - A - 5%
03/06/2023 - B - 50%
03/06/2023 - C - 55%
I want to plot them in the same window. Basically do not want to open them in new windows each
9 Comments
Dyuman Joshi
on 6 Mar 2023
Use hold on
MattC
on 6 Mar 2023
Thanks for getting back on this @Dyuman Joshi, can you please help me what should be the plot type for this?
Dyuman Joshi
on 6 Mar 2023
A line plot should work well. What are the x and y values?
Star Strider
on 6 Mar 2023
I thought we already did that yesterday.
What do you want to do?
Image Analyst
on 7 Mar 2023
What about my answer to your other question: https://www.mathworks.com/matlabcentral/answers/1924235-axeshandle-with-matlab-code#answer_1186870
MattC
on 7 Mar 2023
Thanks for getting back @Image Analyst. I have the data now in three separate plots and but I am not able to plot the subplots for each one of them using the subplot. There is plot1,plot2,plot3 but it only plots plot3 for me. Here is my code:
Days = datetime(['03/06/2023';'03/07/2023'], 'InputFormat','MM/dd/yyyy', 'Format','MM/dd/yyyy');
Data = array2table([5 50; 50 40; 55 65], 'VariableNames',string(Days), 'RowNames',{'A','B','C'})
subplot(4,1,2)
grid on
box on
VN = Data.Properties.VariableNames;
Rows = Data.Properties.RowNames;
for k = 1:3
plot(categorical(VN), table2array(Data(k,:)), 'p-')
title(Rows{k})
ylim([0 100])
end
Can you please help what's wrong? I am thinking since I have (4,1,2) it plots the last plot which is table3 plot and overwrites the previous 2 plots
Accepted Answer
Star Strider
on 6 Mar 2023
Isn’t that just this —
Scaled = @(data,Th) [data(:) ones(size(data(:)))] * ([Th 1; 0 1] \ [1; 0]); % Scale Data By Threshold
Day{1} = [10;
30;
15];
Day{2} = [20;
40;
10];
A_Threshold = 70;
B_Threshold = 80;
C_Threshold = 85;
Thrshld = [A_Threshold; B_Threshold; C_Threshold];
for k1 = 1:numel(Day)
for k2 = 1:size(Thrshld,1)
DayScaled{k1}(k2,:) = Scaled(Day{k1}(k2,:),Thrshld(k2));
end
end
% Day1 = array2table([DayScaled{1}].', 'VariableNames',{'A','B','C'});
% Day2 = array2table([DayScaled{2}].', 'VariableNames',{'A','B','C'});
% ScaledResult = table(Day1,Day2, 'VariableNames',{'Day 1','Day 2'})
Days = datetime(['03/06/2023';'03/07/2023'], 'InputFormat','MM/dd/yyyy', 'Format','MM/dd/yyyy');
ScaledResult = array2table(cell2mat(DayScaled), 'VariableNames',string(Days), 'RowNames',{'A','B','C'})
ScaledResult = 3×2 table
03/06/2023 03/07/2023
__________ __________
A 0.14286 0.28571
B 0.375 0.5
C 0.17647 0.11765
x = ones(size([Day{1}],2),1)*(1:size([Day{1}],1));
rgb = 'grb';
cmy = 'cmy';
xtl = {'A','B','C'};
mk = {'s','d'};
cgttt = [0.9 0.5 0.3];
figure
plot(Days, ScaledResult{:,1:end},'.-')
legend('A','B','C', 'Location','best')
.
39 Comments
MattC
on 6 Mar 2023
The y values are the % values but there isn't no x values
03/06/2023 - A - 5%
03/06/2023 - B - 50%
03/06/2023 - C - 55%
03/07/2023 - A - 50%
03/07/2023 - B - 40%
03/07/2023 - C - 65%
Example of plot for A should be like : https://www.mathworks.com/matlabcentral/answers/1924095-plot-data-in-matlab#comment_2648385
this is different from the scaled values that were used before. Here as well there is no xaxis value it is day1 or 3/6/2023 but the y values are in %. I tried using plot(A) but that comes as blank
Star Strider
on 6 Mar 2023
The dependent variables can be whatever you define them to be. I just used the scaled values earlier.
To use the percentages —
Days = datetime(['03/06/2023';'03/07/2023'], 'InputFormat','MM/dd/yyyy', 'Format','MM/dd/yyyy');
Data = array2table([5 50; 50 40; 55 65], 'VariableNames',string(Days), 'RowNames',{'A','B','C'})
Data = 3×2 table
03/06/2023 03/07/2023
__________ __________
A 5 50
B 50 40
C 55 65
VN = Data.Properties.VariableNames;
figure
plot(categorical(VN), table2array(Data))
grid
xlabel('Date')
ylabel('Percent')
legend(Data.Properties.RowNames, 'Location','best')
.
MattC
on 6 Mar 2023
Will this work when I only have data for one day?
When I am trying to print that currently I am seeing an issue as the plot looks blank. I assume because there are just 3 points since it is for 1 day but they are small hence they are not visible
Star Strider
on 6 Mar 2023
Data for one day will plot as a point if you provide a marker. A line will only plot between two points.
Separate plots: yes —
Days = datetime(['03/06/2023';'03/07/2023'], 'InputFormat','MM/dd/yyyy', 'Format','MM/dd/yyyy');
Data = array2table([5 50; 50 40; 55 65], 'VariableNames',string(Days), 'RowNames',{'A','B','C'})
Data = 3×2 table
03/06/2023 03/07/2023
__________ __________
A 5 50
B 50 40
C 55 65
VN = Data.Properties.VariableNames;
Rows = Data.Properties.RowNames;
figure
tiledlayout(3,1)
for k = 1:3
nexttile
plot(categorical(VN), table2array(Data(k,:)))
grid
xlabel('Date')
ylabel('Percent')
title(Rows{k})
ylim([0 100])
end
.
MattC
on 6 Mar 2023
Edited: MattC
on 6 Mar 2023
How do show the data if I would have only one day? I added marker to the code as well but still do not see it
Also, I am using the subplot option to plot the data. Can this be achieved by building 3 subplots like these? I only see the "A" plot with this code. I am assuming something to do with the loop I am running is incorrect?
I want to build a code which can take multiple days but currently I only have 1 days data and want to show that which is blank and the subplot option does not work as well
axeshandle(2)=subplot('position',[0.13 .641 .775 .106]);
VN = Data.Properties.VariableNames;
Rows = Data.Properties.RowNames;
for k = 1:numel(Day)
nexttile
plot(categorical(VN), table2array(Data(k,:)))
title(Rows{k})
ylim([0 100])
end
Star Strider
on 7 Mar 2023
Actually, you did not designate a marker.
To designate a marker, do something like this —
plot(categorical(VN), table2array(Data(k,:)), 'p-')
This will plot markers (here a pentagram), and if there are more than one point, will connect them with a line.
Days = datetime(['03/06/2023';'03/07/2023'], 'InputFormat','MM/dd/yyyy', 'Format','MM/dd/yyyy');
Data = array2table([5 50; 50 40; 55 65], 'VariableNames',string(Days), 'RowNames',{'A','B','C'})
Data = 3×2 table
03/06/2023 03/07/2023
__________ __________
A 5 50
B 50 40
C 55 65
VN = Data.Properties.VariableNames;
Rows = Data.Properties.RowNames;
% axeshandle(2)=subplot('position',[0.13 .641 .775 .106]);
VN = Data.Properties.VariableNames;
Rows = Data.Properties.RowNames;
figure
tiledlayout(3,1)
for k = 1:numel(Rows)
nexttile
plot(categorical(VN), table2array(Data(k,:)), 'p-')
title(Rows{k})
ylim([0 100])
end
.
MattC
on 7 Mar 2023
Okay I see single point for all A,B,C now which is correct.
However, I am planning on adding these 3 plots on top of a current implementation of plot we had yesterday. So total 4 plots + some other that I might add later hence I am using the axeshandle and subplot option.
Is there a better way to show these plots in single figure? I do not want multiple figures opening up
Also, the code does not show the first plot at the moment with this implmentation of axeshandle and subplot I am doing
axeshandle(2)=subplot('position',[0.13 .641 .775 .106]);
Star Strider
on 7 Mar 2023
They are all in a single figure in my code.
I do not understand what you want to do with the ‘axeshandle’ object, since it is not referenced anywhere. (That is the reasonI commented it out.)
Perhaps this —
Days = datetime(['03/06/2023';'03/07/2023'], 'InputFormat','MM/dd/yyyy', 'Format','MM/dd/yyyy');
Data = array2table([5 50; 50 40; 55 65], 'VariableNames',string(Days), 'RowNames',{'A','B','C'})
Data = 3×2 table
03/06/2023 03/07/2023
__________ __________
A 5 50
B 50 40
C 55 65
VN = Data.Properties.VariableNames;
Rows = Data.Properties.RowNames;
VN = Data.Properties.VariableNames;
Rows = Data.Properties.RowNames;
f = figure;
tiledlayout(3,1)
for k = 1:numel(Rows)
nexttile
plot(categorical(VN), table2array(Data(k,:)), 'p-')
title(Rows{k})
ylim([0 100])
end
axeshandle(2)=subplot('position',[0.13 .641 .775 .106]);
f.Position = axeshandle(2).Position;
I still do not understand what you want to do with it.
.
MattC
on 7 Mar 2023
Sorry fo the confusion. Yes that is right they are all in a single figure for me as well. However, axeshandle(2) is something I was trying because what I am trying to do is create subplots in one figure.
Subplot 1: which we built yesterday
Subplot2: the three plots that are built in the code above.
So in total, one figure would have 4 subplots and with the capacity to add more plots to a figure. However, with the current implementation, I am not able to incorporate the fourth plot from yesterday. So I am trying to get 4 subplots in one figure if that makes sense
Star Strider
on 7 Mar 2023
It makes sense. I use tiledlayout here as a substitute for subplot, simply because it is easier. (It was introduced in R2019b, although changes were made to it in R2021a, so the syntax for it may be different in R2020a that I use here.) I put all of them in one figure here, similar to what I would do for subplot plots.
MattC
on 7 Mar 2023
How do I add this to my plot1? and plot2?
I removed the subplot part from the code and trying to use titledlayout but the below implementation still prints them in 2 separate figures
Since there are 4 plots in total
For plot1 I did
tiledlayout(4,1)
For plot 2 I did
tiledlayout(4,2)
Star Strider
on 7 Mar 2023
They are actually separate axes objects in the same figure. They are similar in their behaviour to subplot plots.
The ‘plot1’ tiledlayout call will create 1 column of 4 axes.
The ‘plot2’ tiledlayout call will create 8 axes, 4 rows and 2 columns.
.
Star Strider
on 7 Mar 2023
The tiledlayout axes are different axes in the same figure, created by the initial tiledlayout call. They are similar to subplot objects in that respect.
I create separate specific figure objects for tiledlayout arrays, just as I do for subpolot arrays. It is easier to keep track of them that way.
Star Strider
on 7 Mar 2023
I create a figure object that tiledlayout then uses. It is the same as just using a plot call (that creates its own figure object) and creating the figure object first, and then plotting the axes in it. Creating the figure object first makes defining what I plot in them and keeping track of them easier. It is just my programming style.
MattC
on 7 Mar 2023
I am using the subplot option but not able to plot the subplots for each one of them using the subplot. There is plot1,plot2,plot3 but it only plots plot3 for me in the 2nd row. Here is my code:
Days = datetime(['03/06/2023';'03/07/2023'], 'InputFormat','MM/dd/yyyy', 'Format','MM/dd/yyyy');
Data = array2table([5 50; 50 40; 55 65], 'VariableNames',string(Days), 'RowNames',{'A','B','C'})
subplot(4,1,2)
grid on
box on
VN = Data.Properties.VariableNames;
Rows = Data.Properties.RowNames;
for k = 1:3
plot(categorical(VN), table2array(Data(k,:)), 'p-')
title(Rows{k})
ylim([0 100])
end
Can you please help what's wrong? I am thinking since I have (4,1,2) it plots the last plot which is table3 plot and overwrites the previous 2 plots
Star Strider
on 7 Mar 2023
I changed the code a bit, putting the subplot call in the loop, and incrementing the subplot plots according to the loop index.
I assume that ihis is what you want —
Days = datetime(['03/06/2023';'03/07/2023'], 'InputFormat','MM/dd/yyyy', 'Format','MM/dd/yyyy');
Data = array2table([5 50; 50 40; 55 65], 'VariableNames',string(Days), 'RowNames',{'A','B','C'})
Data = 3×2 table
03/06/2023 03/07/2023
__________ __________
A 5 50
B 50 40
C 55 65
% subplot(4,1,2)
grid on
box on
VN = Data.Properties.VariableNames;
Rows = Data.Properties.RowNames;
for k = 1:3
subplot(3,1,k)
plot(categorical(VN), table2array(Data(k,:)), 'p-')
title(Rows{k})
ylim([0 100])
end
Also, this aproach will plot all the various values for the entire table when there are more date variables in the table. (Just now, there are only two.)
.
MattC
on 7 Mar 2023
So, will this approach work if I already have a subplot(4,1,1) above these 3 plots? I want these 3 plots to start from row 2 because the row1 already has a plot and I believe looping in with k will do something like (4,1,1),(4,1,2),(4,1,3) so wouldn’t that overwrite my already existing plot in (4,1,1)?
Star Strider
on 7 Mar 2023
I do not understand. You will need to increment the subplot plots, simillar to what I did. If you want to increment them starting with k=2, that is perfectly fine.
Days = datetime(['03/06/2023';'03/07/2023'], 'InputFormat','MM/dd/yyyy', 'Format','MM/dd/yyyy');
Data = array2table([5 50; 50 40; 55 65], 'VariableNames',string(Days), 'RowNames',{'A','B','C'})
Data = 3×2 table
03/06/2023 03/07/2023
__________ __________
A 5 50
B 50 40
C 55 65
% subplot(4,1,2)
grid on
box on
VN = Data.Properties.VariableNames;
Rows = Data.Properties.RowNames;
subplot(4,1,1)
plot(categorical(VN), table2array(Data(1,:)), 'p-')
title(Rows{1})
ylim([0 100])
for k = 2:3
subplot(3,1,k)
plot(categorical(VN), table2array(Data(k,:)), 'p-')
title(Rows{k})
ylim([0 100])
end
.
MattC
on 7 Mar 2023
There are two things here as I explained in the comments above. I know I need a loop for plotting the subplots but there is a different plot independent plot existing outside of this plot inbuild in the loops
Plot 1: Not the one we are discussing but needs to stay in subplot(4,1,1)
%% Data here for plot1
Scaled = @(data,Th) [data(:) ones(size(data(:)))] * ([Th 1; 0 1] \ [1; 0]); % Scale Data By Threshold
Day{1} = [90;
30;
15];
Day{2} = [20;
40;
90];
A_Threshold = 70;
B_Threshold = 80;
C_Threshold = 85;
Thrshld = [A_Threshold; B_Threshold; C_Threshold];
for k1 = 1:numel(Day)
for k2 = 1:size(Thrshld,1)
DayScaled{k1}(k2,:) = Scaled(Day{k1}(k2,:),Thrshld(k2));
end
end
ScaledResult = array2table(cell2mat(DayScaled), 'VariableNames',compose('Day %3d',1:numel(DayScaled)), 'RowNames',{'A','B','C'})
%% Plot 1 starting here
subplot(4,1,1)
x = ones(size([Day{1}],2),1)*(1:size([Day{1}],1));
gb = 'grb';
cmy = 'cmy';
xtl = {'A','B','C'};
mk = {'s','d'};
cgttt = [0.9 0.5 0.3];
figure
hold on
for k1 = 1:numel(Day)
for k2 = 1:size(Thrshld,1)
hp0{k2} = plot(NaN, 1, mk{1}, 'Color',rgb(k2), 'MarkerSize', 10, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
y = DayScaled{k1}(k2,:);
L = y<=1;
hp1{k1,k2} = plot(x(L,k1), y(L), mk{1}, 'Color',rgb(k2), 'MarkerSize', 10, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
% hp1{k1,k2} = plot(x(L,k1), y(L), mk{1}, 'Color',rgb(k2), 'MarkerSize', 10, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s \\leq Threshold',xtl{k2}));
hp2{k1,k2} = plot(x(~L,k1), y(~L), mk{1}, 'Color',rgb(k2), 'MarkerSize', 10, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('> Threshold'));
end
end
xlim([0 3])
set(gca, 'XTick',1:2, 'XTickLabel',compose('Day %d',1:2))
hold off
yl = yline(1, '--r', 'Threshold', 'LineWidth',2, 'DisplayName','Threshold');
legend([hp0{:} yl], 'Location','northoutside', 'NumColumns',4, 'FontSize',8)
Plot 2:
%% Data here for plot2
Days = datetime(['03/06/2023';'03/07/2023'], 'InputFormat','MM/dd/yyyy', 'Format','MM/dd/yyyy');
Data = array2table([5 50; 50 40; 55 65], 'VariableNames',string(Days), 'RowNames',{'A','B','C'})
grid on
box on
VN = Data.Properties.VariableNames;
Rows = Data.Properties.RowNames;
%% Plot 2 starting here
subplot(4,1,1)
plot(categorical(VN), table2array(Data(1,:)), 'p-')
title(Rows{1})
ylim([0 100])
for k = 2:3
subplot(3,1,k)
plot(categorical(VN), table2array(Data(k,:)), 'p-')
title(Rows{k})
ylim([0 100])
end
Now if you run this code and plot. Its only gonna plot the plots from the second part which has 3 plots in itself because of the overlap in subplot () the first plot that I want is not coming
Can you please help how to have these 4 plots as subplots in a figure?
Star Strider
on 7 Mar 2023
%% Data here for plot1
Scaled = @(data,Th) [data(:) ones(size(data(:)))] * ([Th 1; 0 1] \ [1; 0]); % Scale Data By Threshold
Day{1} = [90;
30;
15];
Day{2} = [20;
40;
90];
A_Threshold = 70;
B_Threshold = 80;
C_Threshold = 85;
Thrshld = [A_Threshold; B_Threshold; C_Threshold];
for k1 = 1:numel(Day)
for k2 = 1:size(Thrshld,1)
DayScaled{k1}(k2,:) = Scaled(Day{k1}(k2,:),Thrshld(k2));
end
end
ScaledResult = array2table(cell2mat(DayScaled), 'VariableNames',compose('Day %3d',1:numel(DayScaled)), 'RowNames',{'A','B','C'})
ScaledResult = 3×2 table
Day 1 Day 2
_______ _______
A 1.2857 0.28571
B 0.375 0.5
C 0.17647 1.0588
%% Plot 1 starting here
subplot(4,1,1)
title('subplot(4,1,1)')
x = ones(size([Day{1}],2),1)*(1:size([Day{1}],1));
rgb = 'grb';
cmy = 'cmy';
xtl = {'A','B','C'};
mk = {'s','d'};
cgttt = [0.9 0.5 0.3];
figure
hold on
for k1 = 1:numel(Day)
for k2 = 1:size(Thrshld,1)
hp0{k2} = plot(NaN, 1, mk{1}, 'Color',rgb(k2), 'MarkerSize', 10, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
y = DayScaled{k1}(k2,:);
L = y<=1;
hp1{k1,k2} = plot(x(L,k1), y(L), mk{1}, 'Color',rgb(k2), 'MarkerSize', 10, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
% hp1{k1,k2} = plot(x(L,k1), y(L), mk{1}, 'Color',rgb(k2), 'MarkerSize', 10, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s \\leq Threshold',xtl{k2}));
hp2{k1,k2} = plot(x(~L,k1), y(~L), mk{1}, 'Color',rgb(k2), 'MarkerSize', 10, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('> Threshold'));
end
end
xlim([0 3])
set(gca, 'XTick',1:2, 'XTickLabel',compose('Day %d',1:2))
hold off
yl = yline(1, '--r', 'Threshold', 'LineWidth',2, 'DisplayName','Threshold');
legend([hp0{:} yl], 'Location','northoutside', 'NumColumns',4, 'FontSize',8)
%% Data here for plot2
Days = datetime(['03/06/2023';'03/07/2023'], 'InputFormat','MM/dd/yyyy', 'Format','MM/dd/yyyy');
Data = array2table([5 50; 50 40; 55 65], 'VariableNames',string(Days), 'RowNames',{'A','B','C'})
Data = 3×2 table
03/06/2023 03/07/2023
__________ __________
A 5 50
B 50 40
C 55 65
grid on
box on
VN = Data.Properties.VariableNames;
Rows = Data.Properties.RowNames;
figure
%% Plot 2 starting here
subplot(4,1,1)
plot(categorical(VN), table2array(Data(1,:)), 'p-')
title(Rows{1})
title('subplot(4,1,1)')
ylim([0 100])
for k = 2:3
subplot(4,1,k)
plot(categorical(VN), table2array(Data(k,:)), 'p-')
title(Rows{k})
ylim([0 100])
end
I am not certain what you want to do with ‘subplot(4,1,1)’ . The problem is that they are initially in different figures. You need to plot supblot(4,11,1) in the same figure as the other subplots. In the later code they are in the same figure.
.
MattC
on 7 Mar 2023
When I run the code I want the output for the plots to look like this:
One single figure having total 4 plots. 1st plot from the first code and the last 3 plots from the second part of the code hence I am after subplot(4,1,1) missing the first code
Star Strider
on 7 Mar 2023
Try this —
%% Data here for plot1
Scaled = @(data,Th) [data(:) ones(size(data(:)))] * ([Th 1; 0 1] \ [1; 0]); % Scale Data By Threshold
Day{1} = [90;
30;
15];
Day{2} = [20;
40;
90];
A_Threshold = 70;
B_Threshold = 80;
C_Threshold = 85;
Thrshld = [A_Threshold; B_Threshold; C_Threshold];
for k1 = 1:numel(Day)
for k2 = 1:size(Thrshld,1)
DayScaled{k1}(k2,:) = Scaled(Day{k1}(k2,:),Thrshld(k2));
end
end
ScaledResult = array2table(cell2mat(DayScaled), 'VariableNames',compose('Day %3d',1:numel(DayScaled)), 'RowNames',{'A','B','C'})
ScaledResult = 3×2 table
Day 1 Day 2
_______ _______
A 1.2857 0.28571
B 0.375 0.5
C 0.17647 1.0588
%% Plot 1 starting here
% subplot(4,1,1)
% title('subplot(4,1,1)')
x = ones(size([Day{1}],2),1)*(1:size([Day{1}],1));
rgb = 'grb';
cmy = 'cmy';
xtl = {'A','B','C'};
mk = {'s','d'};
cgttt = [0.9 0.5 0.3];
figure
subplot(4,1,1)
hold on
for k1 = 1:numel(Day)
for k2 = 1:size(Thrshld,1)
hp0{k2} = plot(NaN, 1, mk{1}, 'Color',rgb(k2), 'MarkerSize', 5, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
y = DayScaled{k1}(k2,:);
L = y<=1;
hp1{k1,k2} = plot(x(L,k1), y(L), mk{1}, 'Color',rgb(k2), 'MarkerSize', 5, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
% hp1{k1,k2} = plot(x(L,k1), y(L), mk{1}, 'Color',rgb(k2), 'MarkerSize', 10, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s \\leq Threshold',xtl{k2}));
hp2{k1,k2} = plot(x(~L,k1), y(~L), mk{1}, 'Color',rgb(k2), 'MarkerSize', 5, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('> Threshold'));
end
end
xlim([0 3])
ylim([0 2])
set(gca, 'XTick',1:2, 'XTickLabel',compose('Day %d',1:2))
hold off
yl = yline(1, '--r', 'Threshold', 'LineWidth',2, 'DisplayName','Threshold');
legend([hp0{:} yl], 'Location','eastoutside', 'FontSize',8)
%% Data here for plot2
Days = datetime(['03/06/2023';'03/07/2023'], 'InputFormat','MM/dd/yyyy', 'Format','MM/dd/yyyy');
Data = array2table([5 50; 50 40; 55 65], 'VariableNames',string(Days), 'RowNames',{'A','B','C'})
Data = 3×2 table
03/06/2023 03/07/2023
__________ __________
A 5 50
B 50 40
C 55 65
grid on
box on
VN = Data.Properties.VariableNames;
Rows = Data.Properties.RowNames;
% figure
%% Plot 2 starting here
for k = 2:4
subplot(4,1,k)
plot(categorical(VN), table2array(Data(k-1,:)), 'p-')
title(Rows{k-1})
ylim([0 100])
end
In ‘subplot(4,1,1)’ placing the legend 'northoutside' squashes the plot. I put it 'eastoutside' here. (There are other options for its placement, however they could require an extra subplot fot it alone.)
.
MattC
on 7 Mar 2023
A couple questions on the first plot
x = ones(size([Day{1}],2),1)*(1:size([Day{1}],1));
rgb = 'grb';
cmy = 'cmy';
xtl = {'A','B','C'};
mk = {'s','d'};
cgttt = [0.9 0.5 0.3];
figure
subplot(4,1,1)
hold on
for k1 = 1:numel(Day)
for k2 = 1:size(Thrshld,1)
hp0{k2} = plot(NaN, 1, mk{1}, 'Color',rgb(k2), 'MarkerSize', 5, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
y = DayScaled{k1}(k2,:);
L = y<=1;
hp1{k1,k2} = plot(x(L,k1), y(L), mk{1}, 'Color',rgb(k2), 'MarkerSize', 5, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
% hp1{k1,k2} = plot(x(L,k1), y(L), mk{1}, 'Color',rgb(k2), 'MarkerSize', 10, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s \\leq Threshold',xtl{k2}));
hp2{k1,k2} = plot(x(~L,k1), y(~L), mk{1}, 'Color',rgb(k2), 'MarkerSize', 5, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('> Threshold'));
end
end
xlim([0 3])
ylim([0 2])
set(gca, 'XTick',1:2, 'XTickLabel',compose('Day %d',1:2))
hold off
yl = yline(1, '--r', 'Threshold', 'LineWidth',2, 'DisplayName','Threshold');
legend([hp0{:} yl], 'Location','eastoutside', 'FontSize',8)
- Why do we have xlim([0 3]) ylim([0 2]) in the plot? Wont it limit if the value exceeds 2? Which is not what we want, it should even show up when we do have values exceeding 2
- If there just 1 day's data it still shows day2 for some reason on the x axis. Though it does not have any data for day2 but the ticker is still there. Can that be removed?
- Is this plot scalable in case there are more no of days data in future?
Star Strider
on 7 Mar 2023
- The plot was being squashed, so I increased the ylim values, since I wanted to see how it looked. That can be changed or completely eliminated (although it might be best to keep it so that the highest value plotted does not plot on the highest value of ylim). I also reduced the marker size.
- If there is only one day’s data, that should be reflected in the ‘Data’ table. All the information for the first scatter plot derives from that table. The same applies to the other plots with the data that created them.
- All the plots should be completely scalable. They derive from whatever data you give them.
MattC
on 7 Mar 2023
Thanks for the explanation, so what does this part do in the code?
set(gca, 'XTick',1:2, 'XTickLabel',compose('Day %d',1:2))
I still see the day2 mark on the x axis despite me changing the data for testing to only 1 day
It towards the end of the box so there are no data point in there. Is this happening because of the above part of the code?
Star Strider
on 7 Mar 2023
As always,. my pleasure!
It creates the x-axis tick labels for the first plot.
A more general approach would be:
set(gca, 'XTick',1:2, 'XTickLabel',compose('Day %d',1:numel(Day)))
That’s how I should have written it to begin with. (I was probably just tired by the tieme I wrote that.)
.
Star Strider
on 7 Mar 2023
I corrected part of it, not al lof it.
This should work:
set(gca, 'XTick',1:numel(Day), 'XTickLabel',compose('Day %d',1:numel(Day)))
.
Star Strider
on 8 Mar 2023
As always, my pleasure!
My apologies for not catching that originally.
.
MattC
on 8 Mar 2023
Edited: MattC
on 8 Mar 2023
Scaled = @(data,Th) [data(:) ones(size(data(:)))] * ([Th 1; 0 1] \ [1; 0]); % Scale Data By Threshold
Day{1} = [90;
30;
15];
Day{2} = [20;
40;
90];
A_Threshold = 70;
B_Threshold = 80;
C_Threshold = 85;
Thrshld = [A_Threshold; B_Threshold; C_Threshold];
for k1 = 1:numel(Day)
for k2 = 1:size(Thrshld,1)
DayScaled{k1}(k2,:) = Scaled(Day{k1}(k2,:),Thrshld(k2));
end
end
ScaledResult = array2table(cell2mat(DayScaled), 'VariableNames',compose('Day %3d',1:numel(DayScaled)), 'RowNames',{'A','B','C'})
x = ones(size([Day{1}],2),1)*(1:size([Day{1}],1));
rgb = 'grb';
cmy = 'cmy';
xtl = {'A','B','C'};
mk = {'s','d'};
cgttt = [0.9 0.5 0.3];
figure
subplot(4,1,1)
hold on
for k1 = 1:numel(Day)
for k2 = 1:size(Thrshld,1)
hp0{k2} = plot(NaN, 1, mk{1}, 'Color',rgb(k2), 'MarkerSize', 5, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
y = DayScaled{k1}(k2,:);
L = y<=1;
hp1{k1,k2} = plot(x(L,k1), y(L), mk{1}, 'Color',rgb(k2), 'MarkerSize', 5, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
hp2{k1,k2} = plot(x(~L,k1), y(~L), mk{1}, 'Color',rgb(k2), 'MarkerSize', 5, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('> Threshold'));
end
end
set(gca, 'XTick',1:numel(Day), 'XTickLabel',compose('Day %d',1:numel(Day)))
hold off
yl = yline(1, '--r', 'Threshold', 'LineWidth',2, 'DisplayName','Threshold');
legend([hp0{:} yl], 'Location','eastoutside', 'FontSize',8)
Is it possible to assign a different shape to each of the A,B,C values? and accordingly change this in the legend as well?
I want to keep the colors same (blue) for A,B,C if they are below the Threshold and similarly if they are above threshold then they could have the same color (pink). Just want to differentiate them based on the different shape we assign to them
Star Strider
on 8 Mar 2023
Yes.
The markers are controlled by the ‘mk’ cell array, and they would be indexed by ‘k2’. With those changes, the colours (the ‘rgb’ vector) and the markers change with ‘k2’. You can change the markers and colours if you want.
Scaled = @(data,Th) [data(:) ones(size(data(:)))] * ([Th 1; 0 1] \ [1; 0]); % Scale Data By Threshold
Day{1} = [90;
30;
15];
Day{2} = [20;
40;
90];
A_Threshold = 70;
B_Threshold = 80;
C_Threshold = 85;
Thrshld = [A_Threshold; B_Threshold; C_Threshold];
for k1 = 1:numel(Day)
for k2 = 1:size(Thrshld,1)
DayScaled{k1}(k2,:) = Scaled(Day{k1}(k2,:),Thrshld(k2));
end
end
ScaledResult = array2table(cell2mat(DayScaled), 'VariableNames',compose('Day %3d',1:numel(DayScaled)), 'RowNames',{'A','B','C'})
ScaledResult = 3×2 table
Day 1 Day 2
_______ _______
A 1.2857 0.28571
B 0.375 0.5
C 0.17647 1.0588
x = ones(size([Day{1}],2),1)*(1:size([Day{1}],1));
rgb = 'grb';
cmy = 'cmy';
xtl = {'A','B','C'};
mk = {'s','d','o'};
cgttt = [0.9 0.5 0.3];
figure
subplot(4,1,1)
hold on
for k1 = 1:numel(Day)
for k2 = 1:size(Thrshld,1)
hp0{k2} = plot(NaN, 1, mk{k2}, 'Color',rgb(k2), 'MarkerSize', 5, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
y = DayScaled{k1}(k2,:);
% L = y<=1;
L = true;
hp1{k1,k2} = plot(x(L,k1), y(L), mk{k2}, 'Color',rgb(k2), 'MarkerSize', 5, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
% hp2{k1,k2} = plot(x(~L,k1), y(~L),mk{1}, 'Color',rgb(k2), 'MarkerSize', 5, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('> Threshold'));
end
end
set(gca, 'XTick',1:numel(Day), 'XTickLabel',compose('Day %d',1:numel(Day)))
hold off
yl = yline(1, '--r', 'Threshold', 'LineWidth',2, 'DisplayName','Threshold');
legend([hp0{:} yl], 'Location','eastoutside', 'FontSize',8)
xlim([0 3]) % Optional
There wa some problem with the login system at MathWorks for the last 5 hours. I was just able to log in a few minutes ago. (It would be interesting to know what caused that. I suspect hackers, however I have no direct knowledge to support that.)
.
MattC
on 8 Mar 2023
Thanks for replying back. So, for the value less than threshold can I put them in same color and above ones in same color irrespective of which table they belong? I think the different shapes for tables are easy to identify them
Star Strider
on 9 Mar 2023
The way I have it here, there are no differences in the markers or colours above or below the threshold. They just plot above or below the threshold, depending on their scaled values.
MattC
on 9 Mar 2023
That’s how I was expecting it but for the color it has 3 different colors based on ABC but what I am trying is colors based on if a value is above the red color different color and if it’s below the line then a different color
Star Strider
on 9 Mar 2023
We can go back to that easily enough —
Scaled = @(data,Th) [data(:) ones(size(data(:)))] * ([Th 1; 0 1] \ [1; 0]); % Scale Data By Threshold
Day{1} = [90;
30;
15];
Day{2} = [20;
40;
90];
A_Threshold = 70;
B_Threshold = 80;
C_Threshold = 85;
Thrshld = [A_Threshold; B_Threshold; C_Threshold];
for k1 = 1:numel(Day)
for k2 = 1:size(Thrshld,1)
DayScaled{k1}(k2,:) = Scaled(Day{k1}(k2,:),Thrshld(k2));
end
end
ScaledResult = array2table(cell2mat(DayScaled), 'VariableNames',compose('Day %3d',1:numel(DayScaled)), 'RowNames',{'A','B','C'})
ScaledResult = 3×2 table
Day 1 Day 2
_______ _______
A 1.2857 0.28571
B 0.375 0.5
C 0.17647 1.0588
x = ones(size([Day{1}],2),1)*(1:size([Day{1}],1));
rgb = 'grb';
cmy = 'cmy';
xtl = {'A','B','C'};
mk = {'s','d','o'};
cgttt = [0.9 0.5 0.1];
figure
subplot(4,1,1)
hold on
for k1 = 1:numel(Day)
for k2 = 1:size(Thrshld,1)
hp0{k2} = plot(NaN, 1, mk{k2}, 'Color',rgb(k2), 'MarkerSize', 5, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
y = DayScaled{k1}(k2,:);
L = y<=1;
hp1{k1,k2} = plot(x(L,k1), y(L), mk{k2}, 'Color',rgb(k2), 'MarkerSize', 5, 'MarkerFaceColor',rgb(k2), 'DisplayName',sprintf('%s',xtl{k2}));
hp2{k1,k2} = plot(x(~L,k1), y(~L),mk{k2}, 'Color',cgttt, 'MarkerSize', 5, 'MarkerFaceColor',cgttt, 'DisplayName',sprintf('> Threshold'));
end
end
set(gca, 'XTick',1:numel(Day), 'XTickLabel',compose('Day %d',1:numel(Day)))
hold off
yl = yline(1, '--r', 'Threshold', 'LineWidth',2, 'DisplayName','Threshold');
legend([hp0{:} yl], 'Location','eastoutside', 'FontSize',8)
xlim([0 3]) % Optional
The shapes identify the class, with a different (same) colour.if greater than the threshold.
More Answers (1)
Image Analyst
on 7 Mar 2023
From your other post it seems you want one plot/graph/axes with one curve in it, then another plot/graph/axes with 3 curves in it.
Looks like you're missing a hold on. Plus It makes no sense to make your numbers a table only to make them an array again inside the plot loop, unless you need it as a table later on in the code.
Days = datetime(['03/06/2023';'03/07/2023'], 'InputFormat','MM/dd/yyyy', 'Format','MM/dd/yyyy');
Data = array2table([5 50; 50 40; 55 65], 'VariableNames',string(Days), 'RowNames',{'A','B','C'})
VN = Data.Properties.VariableNames;
Rows = Data.Properties.RowNames;
subplot(4,1,2) % Make the active plot the second graph in a layout of 4 vertical graphs.
% Plot 3 sets of data.
for k = 1:3
plot(categorical(VN), table2array(Data(k,:)), '-', 'LineWidth', 3)
hold on; % Don't let new plots blow away existing plots.
end
legend(Rows(1:3));
ylim([0 100])
grid on
box on
2 Comments
Image Analyst
on 7 Mar 2023
Please explain exactly what "plot" means to you. It's kind of a layman's term that's not very precise. Try to use MATLAB lingo. So a "figure" is the entire window. A figure can contain many "axes". An "axes" is a box that can contain an image, or one or more plotted curves. So you could have one figure with (say) 4 axes on it. The data curves are plotted within an "axes".
So, answer these questions (by number please)
- How many figures do you want? I'm assuming one figure with multiple axes on it.
- How many "axes" do you want on each figure? 2? 4?
- In each of the axes, how many data curves do you want plotted? Like (say) one curve in the top axes, 3 curves in the second axes, and two curves in the third axes, or whatever you want.
- Then specify what data is to be plotted in what axes.
See Also
Categories
Find more on Axes Appearance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)