multiaxes and tiles inside a loop, how to plot different values for each tile independently

2 views (last 30 days)
In the following code I will like to ask the following questions:
  1. How can I plot a horizontal line of 0.3 in zone 1 and another horizontal line value in zone 2 and different for zone 3 and zone 4
  2. How can I center the x axis label
  3. How can this code be enhanced
  4. I am having trouble with x labes and y labels and tick marks. How can I label each y axis independendently.
Thank you
C_p_max_single_Tap=[0.45,0.383,0.513,1.683,1.494];
clear all; %get rid of everything in memory
close all; %close all open figures
C_p_max_single_Tap= [0.450,0.383,0.513,1.683,1.494];
C_p_max_single_Tap=round(C_p_max_single_Tap,3)
C_p_max_single_Tap = 1×5
0.4500 0.3830 0.5130 1.6830 1.4940
clear data
figure
t=tiledlayout(1,5,'TileSpacing','none');
for i = 1:5
plot(nexttile,1,C_p_max_single_Tap(i),'o', 'MarkerFaceColor', [0 0 1], 'MarkerEdgeColor',...
[0 0 1], 'MarkerSize', 14)
hold on
yline(0.3,'--','LineWidth',3, 'Color', [0.9, 0.2, 0.2]);
text(1,1.85, "ZONE " + i ,'FontSize', 26,...
'HorizontalAlignment', 'center', 'VerticalAlignment',...
'middle','Interpreter', 'latex');
ylabel('${C_p}$','Interpreter', 'Latex','FontSize', 26);
ylim([0 2])
set(gca,'xtick',[1],'FontSize', 22);
box on
end
linkaxes(t.Children)
xlim([0.001 2])
xlabel('{\rm{Test case number}}','Interpreter', 'Latex','FontSize', 26);
set(t.Children(1:4), 'YTickLabel', [])
set(t.Children(1:4), 'ylabel', [])

Answers (1)

Dave B
Dave B on 13 Nov 2021
Edited: Dave B on 13 Nov 2021
Here are answers to each of your questions
Q1: How can I plot a horizontal line of 0.3 in zone 1 and another horizontal line value in zone 2 and different for zone 3 and zone 4?
In your for loop, you're calling yline with 0.3 on each axes, if you want to call it with a different value for each axes, make a vector at the beginning and call it with those values:
ylinevals=[0.3 0.4 0.2 0.5 0.1];
...
for i = 1:5
...
yline(ylinevals(i),...)
Q2: How can I center the x axis label?
The x axis label is currently centered...but on the 5th axes. the function xlabel uses the current axes as a target by default. Instead, set the label of the tiledlayout:
xlabel(t, ...)
Q3: How can this code be enhanced ?
(I think you need to be more specific here)
Q4: I am having trouble with x labes and y labels and tick marks. How can I label each y axis independendently.
You can set them in your loop, using ylabel (taking the strategy as above)
mylabels=["a" "b" "c" "d" "e"];
...
for i = 1:5
...
ylabel(mylabels(i))
You can set them after by specifying a target for ylabel (using nexttile helps here)
...
end
% set(t.Children(1:4), 'ylabel', []) % OMIT this line of code either way
ylabel(nexttile(1),"Label for left-most axes")
ylabel(nexttile(2),"Label for second axes")
...
If you want to keep the y tick labels, omit the line where you remove them.

Community Treasure Hunt

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

Start Hunting!