Plot data in matlab
8 次查看(过去 30 天)
显示 更早的评论
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 个评论
MattC
2023-3-6
Thanks for getting back on this @Dyuman Joshi, can you please help me what should be the plot type for this?
MattC
2023-3-6
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%
Plot for A should be like:
Image Analyst
2023-3-7
What about my answer to your other question: https://www.mathworks.com/matlabcentral/answers/1924235-axeshandle-with-matlab-code#answer_1186870
MattC
2023-3-7
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
采纳的回答
Star Strider
2023-3-6
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 个评论
MattC
2023-3-6
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
2023-3-6
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
2023-3-6
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
MattC
2023-3-6
Or it is possible to plot all the 3 variables on separate plots? Might help in plotting the points clearly as well
To avoid the confusion incase the number of days increase ?
Star Strider
2023-3-6
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
2023-3-6
编辑:MattC
2023-3-6
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
2023-3-7
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
2023-3-7
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
2023-3-7
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
2023-3-7
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
2023-3-7
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
2023-3-7
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
2023-3-7
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.
.
MattC
2023-3-7
So, what I am trying to ask is: how to fix this so that they appear in the same figure?
I think what I did is not doing the thing
Star Strider
2023-3-7
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.
MattC
2023-3-7
I do not understand still. Can you show an example? Of what you mean when you say " create separate specific figure objects for tiledlayout arrays"
Star Strider
2023-3-7
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
2023-3-7
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
2023-3-7
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
2023-3-7
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
2023-3-7
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
2023-3-7
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
2023-3-7
%% 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
2023-3-7
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
2023-3-7
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
2023-3-7
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
2023-3-7
- 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
2023-3-7
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
2023-3-7
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.)
.
MattC
2023-3-7
Okay, this does work but for some reason this created Day1 now instead of Day2
Looks like it does not go away :D
Star Strider
2023-3-7
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)))
.
MattC
2023-3-8
编辑:MattC
2023-3-8
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
2023-3-8
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
2023-3-8
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
2023-3-9
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
2023-3-9
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
2023-3-9
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.
更多回答(1 个)
Image Analyst
2023-3-7
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 个评论
Image Analyst
2023-3-7
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Axes Appearance 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)