Plot data in matlab

8 Ansichten (letzte 30 Tage)
MattC
MattC am 6 Mär. 2023
Kommentiert: Star Strider am 9 Mär. 2023
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 Kommentare
Image Analyst
Image Analyst am 7 Mär. 2023
MattC
MattC am 7 Mär. 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 6 Mär. 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 Kommentare
MattC
MattC am 9 Mär. 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
Star Strider am 9 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 7 Mär. 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 Kommentare
MattC
MattC am 7 Mär. 2023
Please see my comment above. I am trying to add 4 plots to a figure when running
Image Analyst
Image Analyst am 7 Mär. 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)
  1. How many figures do you want? I'm assuming one figure with multiple axes on it.
  2. How many "axes" do you want on each figure? 2? 4?
  3. 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.
  4. Then specify what data is to be plotted in what axes.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Axes Appearance finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by