Plot not showing the desired markers and does not match the legend

Hello everyone,
I am trying to plot theta vs Cp. Cp here is calculated based on the function provided. However when plotted, the markers are not showing properly and does not match the legend.
Any help is appreciated!
I have attached the plot as well as the function and the main file.
Function File:
function Cp = Experiment_1(H,Ch11)
cu = 9.8; %mm of H20 to Pascal
%H=[-0.3,-0.5,-2.1,-3.3,-3.5,-3.2,-3.1,-3.1,-3.2,-2.7];
Ch11 = Ch11.*cu; % Converting channel 11 pressure to Pascal
for i=1:length(H)
H(i) = H(i).*cu; % To convert to Pascal
end
% Convert pressure readings to total pressure
P_atm = 99991.7925; % Pascal
for i=1:length(H)
Pt(i) = P_atm + H(i);
end
Ch11 = P_atm + Ch11;
% theta
theta = linspace(0,180,10);
% Generate the plot for total pressure
%{
plot(theta,Pt, 'o', 'MarkerSize', 5, 'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'g');
xlabel("$\theta[degrees]$","Interpreter","latex","FontSize",12)
ylabel("$Total Pressure, P_{o}[atm]$","Interpreter","latex","FontSize",12)
grid on
axis equal
set(gca,'TickLabelInterpreter','latex','layer','top');
set(gca,'TickDir','out')
set(gca, 'Layer', 'bottom')
%}
% Calculation of Cp experimental
Cp = (Pt- Ch11)./((1.0/2.0)*1.225*6.4*6.4);
% Plot the Cp along the circular cylinder
plot(theta, Cp, '-d', 'MarkerSize',5,'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'g');
hold on
plot(theta, Cp, '-o', 'MarkerSize',5)
hold on
plot(theta, Cp, '-+', 'MarkerSize',5)
hold on
plot(theta, Cp, '-*', 'MarkerSize',5)
hold on
plot(theta, Cp, '-x', 'MarkerSize',5)
xlabel('$\theta\ [^\circ]$',"Interpreter","latex","FontSize",12)
ylabel('$C_p$',"Interpreter","latex","FontSize",12)
grid on
set(gca,'TickLabelInterpreter','latex','layer','top');
set(gca,'TickDir','out')
set(gca, 'Layer', 'bottom')
legend("V=6.4 m/s", "V = 8.7 m/s" , "V = 11 m/s" , "V = 14.3 m/s", "V=12.1 m/s","Interpreter","latex")
%%
sfname = "C:\IITM MATLAB\LAB AS5110\Images\";
export_fig(strcat(sfname,'Cp20Fan'),'-png','-transparent','-m4');
Main File:
figure;
hold on;
H20 = [-0.3,-0.5,-2.1,-3.3,-3.5,-3.2,-3.1,-3.1,-3.2,-2.7];
Ch11_1 = -2.1;
H30 = [-0.3,-1.2,-4.5,-7.4,-7.5,-7.1,-6.9,-7.0,-7.0,-6.5];
Ch11_2 = -4.1;
H40 = [-0.3,-2.2,-7.5,-12.5,-12.4,-11.9,-11.8,-12.3,-12.1,-11.4];
Ch11_3 = -7.0;
H45 = [-0.5,-2.7,-9.4,-15.6,-15.6,-14.8,-14.8,-15.1,-15.0,-14.4];
Ch11_4 = -8.7;
H55 = [-0.5,-3.9,-13.4,-22.4,-22.0,-21.1,-21.0,-21.8,-21.6,-20.9];
Ch11_5 = -12.2;
Cp20 = Experiment_1(H20, Ch11_1);
Cp30 = Experiment_1(H30, Ch11_2);
Cp40 = Experiment_1(H40, Ch11_3);
Cp45 = Experiment_1(H45, Ch11_4);
Cp55 = Experiment_1(H55, Ch11_5);

2 Comments

Please attach the code as text in line -- use the CODE button to format. Attach a .mat file with the needed data if want somebody to be able to run the code.
It might be significant which release of MATLAB you're using; also update that field, please .
Thanks for putting code in line; much simpler than if have to download and then repost...
function Cp = Experiment_1(H,Ch11)
cu = 9.8; %mm of H20 to Pascal
%H=[-0.3,-0.5,-2.1,-3.3,-3.5,-3.2,-3.1,-3.1,-3.2,-2.7];
Ch11 = Ch11.*cu; % Converting channel 11 pressure to Pascal
H= H*cu; % To convert to Pascal - use MATLAB array operations instead of loop...
% Convert pressure readings to total pressure
P_atm = 99991.7925; % Pascal
Pt=P_atm + H;
Ch11 = P_atm + Ch11;
% theta
theta = linspace(0,180,10);
% Calculation of Cp experimental
Cp = (Pt- Ch11)./((1.0/2.0)*1.225*6.4*6.4);
% Plot the Cp along the circular cylinder
plot(theta, Cp, '-d', 'MarkerSize',5,'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'g');
hold on
plot(theta, Cp, '-o', 'MarkerSize',5)
plot(theta, Cp, '-+', 'MarkerSize',5)
plot(theta, Cp, '-*', 'MarkerSize',5)
plot(theta, Cp, '-x', 'MarkerSize',5)
...
Above uses MATLAB vectorized operations for the units conversions; no loops needed for arithmetic operations on arrays. That's the key feature of MAT[rix]LAB[oratory].
Also for efficiency/brevity, remove the superfluous "hold on" calls; once "on" is set, it can't get any more "on-er". <g>
Towards the specific question asked about, the above plot commands all plot the identical data so all that will show in the end will be the last line with the '-x' marker/linestyle. The colors will cycle through the default color map.
When you then call the function five separate time with the five repetitions of the plot inside, then you end up with the markers all being on top of each other in a blob in the plot, but the legend still is for each of the five individual line styles.

Sign in to comment.

 Accepted Answer

I got interrupted before able to finish up what would, like @Star Strider, be my guess of your intent.
I'd rearrange something more like
function Cp = Experiment_1(H,Ch11)
cu = 9.8; %mm of H20 to Pascal
Ch11 = Ch11.*cu; % Converting channel 11 pressure to Pascal
H=H*cu; % To convert to Pascal
% Convert pressure readings to total pressure
P_atm = 99991.7925; % Pascal
Pt=P_atm + H;
Ch11 = P_atm + Ch11;
theta = linspace(0,180,10);
% Calculation of Cp experimental
Cp = (Pt- Ch11)./((1.0/2.0)*1.225*6.4*6.4);
% Plot the Cp along the circular cylinder
hL=plot(theta, Cp,'MarkerSize',5,'MarkerEdgeColor','r','MarkerFaceColor','g');
markers={'d','o','+','*','x'}.'; % cell array of markers
set(hL.',{'Marker'},markers) % use multiple handles form of set()
xlabel('$\theta\ [^\circ]$',"Interpreter","latex","FontSize",12)
ylabel('$C_p$',"Interpreter","latex","FontSize",12)
grid on
%set(gca,'TickLabelInterpreter','latex','layer','top');
%set(gca,'TickDir','out')
%set(gca, 'Layer', 'bottom')
legend("V=6.4 m/s", "V = 8.7 m/s" , "V = 11 m/s" , "V = 14.3 m/s", "V=12.1 m/s","Interpreter","latex")
%sfname = "C:\IITM MATLAB\LAB AS5110\Images\";
%export_fig(strcat(sfname,'Cp20Fan'),'-png','-transparent','-m4');
end
figure;
% combine data into arrays, instead of multiple named variables
H = [-0.3,-0.5, -2.1, -3.3, -3.5, -3.2, -3.1, -3.1, -3.2, -2.7;
-0.3,-1.2, -4.5, -7.4, -7.5, -7.1, -6.9, -7.0, -7.0, -6.5;
-0.3,-2.2, -7.5,-12.5,-12.4,-11.9,-11.8,-12.3,-12.1,-11.4;
-0.5,-2.7, -9.4,-15.6,-15.6,-14.8,-14.8,-15.1,-15.0,-14.4;
-0.5,-3.9,-13.4,-22.4,-22.0,-21.1,-21.0,-21.8,-21.6,-20.9].';
Ch11=[-2.1;-4.1;-7.0;-8.7;-12.2].';
Cp=Experiment_1(H, Ch11);
The above takes advantage of MATLAB vectorized nature; most functions like with the use of plot above operate by column so passing the data as a vector for x and a column-wise array for y yields the plot with the one call. The unique feature of each plot then can be set with the desired values as an array for the given property.
A general tenet with MATLAB is to try to use arrays for like data as much as possible over individual variable names; processing can then be done for all in one statement rather than having to either duplicate code for each naming the variables explicitly.

8 Comments

One nicety could be added to the legend...
function Cp = Experiment_1(H,Ch11,v)
cu = 9.8; %mm of H20 to Pascal
Ch11 = Ch11.*cu; % Converting channel 11 pressure to Pascal
H=H*cu; % To convert to Pascal
% Convert pressure readings to total pressure
P_atm = 99991.7925; % Pascal
Pt=P_atm + H;
Ch11 = P_atm + Ch11;
theta = linspace(0,180,10);
% Calculation of Cp experimental
Cp = (Pt- Ch11)./((1.0/2.0)*1.225*6.4*6.4);
% Plot the Cp along the circular cylinder
hL=plot(theta, Cp,'MarkerSize',5,'MarkerEdgeColor','r','MarkerFaceColor','g');
markers={'d','o','+','*','x'}.'; % cell array of markers
set(hL.',{'Marker'},markers) % use multiple handles form of set()
xlabel('$\theta\ [^\circ]$',"Interpreter","latex","FontSize",12)
ylabel('$C_p$',"Interpreter","latex","FontSize",12)
grid on
hAx=gca; % handle to current axes
set(hAx,'TickLabelInterpreter','latex','TickDir','out')
legend(compose('V =%5.1f m/s',v),"Interpreter","latex")
%sfname = "C:\IITM MATLAB\LAB AS5110\Images\";
%export_fig(strcat(sfname,'Cp20Fan'),'-png','-transparent','-m4');
end
figure;
% combine data into arrays, instead of multiple named variables
H = [-0.3,-0.5, -2.1, -3.3, -3.5, -3.2, -3.1, -3.1, -3.2, -2.7;
-0.3,-1.2, -4.5, -7.4, -7.5, -7.1, -6.9, -7.0, -7.0, -6.5;
-0.3,-2.2, -7.5,-12.5,-12.4,-11.9,-11.8,-12.3,-12.1,-11.4;
-0.5,-2.7, -9.4,-15.6,-15.6,-14.8,-14.8,-15.1,-15.0,-14.4;
-0.5,-3.9,-13.4,-22.4,-22.0,-21.1,-21.0,-21.8,-21.6,-20.9].';
Ch11=[-2.1;-4.1;-7.0;-8.7;-12.2].';
v=[6.4,8.7,11,14.3,12.1]; % the velocities
Cp=Experiment_1(H, Ch11,v); % pass them, too..
This formats the legends so they all have the same precision and can be set as data along with the rest in the main routine. Unfortunately, the leading spaces don't seem to be rendered in legend so the decimal points aren't aligned as would be the neatest-looking solution. They are in the string, just not rendred as can be seen in
compose('V =%5.1f m/s',v.')
ans = 5×1 cell array
{'V = 6.4 m/s'} {'V = 8.7 m/s'} {'V = 11.0 m/s'} {'V = 14.3 m/s'} {'V = 12.1 m/s'}
where the command window uses the fixed-width font. OTOMH, I don't see a solution to this...
figure;
Cp=Experiment_2(H, Ch11,v); % pass them, too..
function Cp = Experiment_2(H,Ch11,v)
cu = 9.8; %mm of H20 to Pascal
Ch11 = Ch11.*cu; % Converting channel 11 pressure to Pascal
H=H*cu; % To convert to Pascal
% Convert pressure readings to total pressure
P_atm = 99991.7925; % Pascal
Pt=P_atm + H;
Ch11 = P_atm + Ch11;
theta = linspace(0,180,10);
% Calculation of Cp experimental
Cp = (Pt- Ch11)./((1.0/2.0)*1.225*6.4*6.4);
% Plot the Cp along the circular cylinder
hL=plot(theta, Cp,'MarkerSize',5,'MarkerEdgeColor','r','MarkerFaceColor','g');
markers={'d','o','+','*','x'}.'; % cell array of markers
set(hL.',{'Marker'},markers) % use multiple handles form of set()
xlabel('$\theta\ [^\circ]$',"Interpreter","latex","FontSize",12)
ylabel('$C_p$',"Interpreter","latex","FontSize",12)
grid on
hAx=gca; % handle to current axes
set(hAx,'TickLabelInterpreter','latex','TickDir','out')
legend(compose('V =%05.1f m/s',v),"Interpreter","latex")
%sfname = "C:\IITM MATLAB\LAB AS5110\Images\";
%export_fig(strcat(sfname,'Cp20Fan'),'-png','-transparent','-m4');
end
Prepending the 0-fill aligns the decimal points, but is ugly in a different way...there isn't an accounting alignment of the decimal points option like with an Excel spreadsheet column, unfortunately.
"where the command window uses the fixed-width font."
Can the legend font be specified as one of fixed-width so that space-space doesn't get munged into a single space?
You can, but the use of LaTeX interpreter overrides and there's still no way I'm aware of to add fonts into the MATLAB-packaged LaTeX interpreter.
Even adding extra spaces over two leading spaces makes no difference to the LaTeX rendering...it thinks it knows best.
Looks like "\ " works for latex, but not tex (I'm not an expert on either), if one or the other must be used (which does not appear to be the case for this specific example)
figure
plot(subplot(311),rand(2))
legend("V = 1.2","V = 10.4",'interpreter','none')
plot(subplot(312),rand(2))
legend("V =\ \ 1.2","V =\ 10.4",'interpreter','latex')
plot(subplot(313),rand(2))
legend("V =\ \ 1.2","V =\ 10.4",'interpreter','tex') % I thought tex would be the same in this regard
I hadn't thought of trying to escape the blanks...
H = [-0.3,-0.5, -2.1, -3.3, -3.5, -3.2, -3.1, -3.1, -3.2, -2.7;
-0.3,-1.2, -4.5, -7.4, -7.5, -7.1, -6.9, -7.0, -7.0, -6.5;
-0.3,-2.2, -7.5,-12.5,-12.4,-11.9,-11.8,-12.3,-12.1,-11.4;
-0.5,-2.7, -9.4,-15.6,-15.6,-14.8,-14.8,-15.1,-15.0,-14.4;
-0.5,-3.9,-13.4,-22.4,-22.0,-21.1,-21.0,-21.8,-21.6,-20.9].';
v=[6.4,8.7,11,14.3,12.1];
hL=plot(H.');
compose('V =\\%5.1f m/s',v)
ans = 1×5 cell array
{'V =\ 6.4 m/s'} {'V =\ 8.7 m/s'} {'V =\ 11.0 m/s'} {'V =\ 14.3 m/s'} {'V =\ 12.1 m/s'}
hLg=legend(compose('V =\\%5.1f m/s',v),'interpreter','latex');
Unfortunately, one has to get awfully picky about how to write generically...
function Cp = Experiment_1(H,Ch11,v)
cu = 9.8; %mm of H20 to Pascal
Ch11 = Ch11.*cu; % Converting channel 11 pressure to Pascal
H=H*cu; % To convert to Pascal
% Convert pressure readings to total pressure
P_atm = 99991.7925; % Pascal
Pt=P_atm + H;
Ch11 = P_atm + Ch11;
theta = linspace(0,180,10);
% Calculation of Cp experimental
Cp = (Pt- Ch11)./((1.0/2.0)*1.225*6.4*6.4);
% Plot the Cp along the circular cylinder
hL=plot(theta, Cp,'MarkerSize',5,'MarkerEdgeColor','r','MarkerFaceColor','g');
markers={'d','o','+','*','x'}.'; % cell array of markers
set(hL.',{'Marker'},markers) % use multiple handles form of set()
xlabel('$\theta\ [^\circ]$',"Interpreter","latex","FontSize",12)
ylabel('$C_p$',"Interpreter","latex","FontSize",12)
grid on
hAx=gca; % handle to current axes
set(hAx,'TickLabelInterpreter','latex','TickDir','out')
lgds=compose('$V =%5.1f m/s$',v); % wrap formatting string in $ for LaTeX
lgds=strrep(lgds,' ','~'); % replace blanks with LaTeX space preserving blank
legend(lgds,"Interpreter","latex")
%sfname = "C:\IITM MATLAB\LAB AS5110\Images\";
%export_fig(strcat(sfname,'Cp20Fan'),'-png','-transparent','-m4');
end
figure;
% combine data into arrays, instead of multiple named variables
H = [-0.3,-0.5, -2.1, -3.3, -3.5, -3.2, -3.1, -3.1, -3.2, -2.7;
-0.3,-1.2, -4.5, -7.4, -7.5, -7.1, -6.9, -7.0, -7.0, -6.5;
-0.3,-2.2, -7.5,-12.5,-12.4,-11.9,-11.8,-12.3,-12.1,-11.4;
-0.5,-2.7, -9.4,-15.6,-15.6,-14.8,-14.8,-15.1,-15.0,-14.4;
-0.5,-3.9,-13.4,-22.4,-22.0,-21.1,-21.0,-21.8,-21.6,-20.9].';
Ch11=[-2.1;-4.1;-7.0;-8.7;-12.2].';
v=[6.4,8.7,11,14.3,12.1]; % the velocities
Cp=Experiment_1(H, Ch11,v); % pass them, too..
works.
A gargle search located that LaTeX behavior is to always replace multiple whitespaces with one but there is the space-preserviing "~" character for the purpose that is simpler than having to count and individually escape a variable number of blanks. If one could use packages in the MATLAB incarnation, there are other alternatives such as \textt{Specific text string), but that also is an unsolved problem.
R2021b on local machine renders the above without surrounding the strings with the "$" signs but that doesn't work here; not sure about that being considered an introduced bug in later release or an unintended feature in earlier..

Sign in to comment.

More Answers (1)

It is difficult for me to understand what you're doing, although I believe this is close to what you want --
% ----- Experiment_1 -----
function Cp = Experiment_1(H,Ch11,ls)
Ch11_label = -Ch11;
cu = 9.8; %mm of H20 to Pascal
%H=[-0.3,-0.5,-2.1,-3.3,-3.5,-3.2,-3.1,-3.1,-3.2,-2.7];
Ch11 = Ch11.*cu; % Converting channel 11 pressure to Pascal
for i=1:length(H)
H(i) = H(i).*cu; % To convert to Pascal
end
% Convert pressure readings to total pressure
P_atm = 99991.7925; % Pascal
for i=1:length(H)
Pt(i) = P_atm + H(i);
end
Ch11 = P_atm + Ch11;
% theta
theta = linspace(0,180,10);
% Generate the plot for total pressure
%{
plot(theta,Pt, 'o', 'MarkerSize', 5, 'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'g');
xlabel("$\theta[degrees]$","Interpreter","latex","FontSize",12)
ylabel("$Total Pressure, P_{o}[atm]$","Interpreter","latex","FontSize",12)
grid on
axis equal
set(gca,'TickLabelInterpreter','latex','layer','top');
set(gca,'TickDir','out')
set(gca, 'Layer', 'bottom')
%}
% Calculation of Cp experimental
Cp = (Pt- Ch11)./((1.0/2.0)*1.225*6.4*6.4);
% Plot the Cp along the circular cylinder
if Ch11_label == 2.1
plot(theta, Cp, ls, 'MarkerSize',5,'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'g','DisplayName',sprintf('V = %5.1f m/s',Ch11_label));
% hold on
else
plot(theta, Cp, ls, 'MarkerSize',5,'DisplayName',sprintf('V = %5.1f m/s',Ch11_label))
end
% plot(theta, Cp, '-o', 'MarkerSize',5)
% hold on
% plot(theta, Cp, '-+', 'MarkerSize',5)
% hold on
% plot(theta, Cp, '-*', 'MarkerSize',5)
% hold on
% plot(theta, Cp, '-x', 'MarkerSize',5)
xlabel('$\theta\ [^\circ]$',"Interpreter","latex","FontSize",12)
ylabel('$C_p$',"Interpreter","latex","FontSize",12)
grid on
set(gca,'TickLabelInterpreter','latex','layer','top');
set(gca,'TickDir','out')
set(gca, 'Layer', 'bottom')
legend(Location='best', Interpreter='LaTeX')
% legend("V=6.4 m/s", "V = 8.7 m/s" , "V = 11 m/s" , "V = 14.3 m/s", "V=12.1 m/s","Interpreter","latex")
%%
sfname = "C:\IITM MATLAB\LAB AS5110\Images\";
% % % % % export_fig(strcat(sfname,'Cp20Fan'),'-png','-transparent','-m4');
end
%{
p-p(inf)= Rho g [C1 - C11]
cp = p-p(inf) / 0.5 rho(air) v^2
%}
%%
%{
C11 = -2.1;
H = [-0.3,-0.5,-2.1,-3.3,-3.5,-3.2,-3.1,-3.1,-3.2,-2.7];
for i = 1:length(H)
subt(i) = (1000*9.81 * (H(i) - C11))./1000;
cp(i) = subt(i)./(0.5*1.225 * 6.4*6.4);
end
plot(theta,cp)
%}
% ----- Experiment_1_Main -----
figure;
hold on;
H20 = [-0.3,-0.5,-2.1,-3.3,-3.5,-3.2,-3.1,-3.1,-3.2,-2.7];
Ch11_1 = -2.1;
H30 = [-0.3,-1.2,-4.5,-7.4,-7.5,-7.1,-6.9,-7.0,-7.0,-6.5];
Ch11_2 = -4.1;
H40 = [-0.3,-2.2,-7.5,-12.5,-12.4,-11.9,-11.8,-12.3,-12.1,-11.4];
Ch11_3 = -7.0;
H45 = [-0.5,-2.7,-9.4,-15.6,-15.6,-14.8,-14.8,-15.1,-15.0,-14.4];
Ch11_4 = -8.7;
H55 = [-0.5,-3.9,-13.4,-22.4,-22.0,-21.1,-21.0,-21.8,-21.6,-20.9];
Ch11_5 = -12.2;
Cp20 = Experiment_1(H20, Ch11_1, '-d');
Cp30 = Experiment_1(H30, Ch11_2,'-o');
Cp40 = Experiment_1(H40, Ch11_3, '-+');
Cp45 = Experiment_1(H45, Ch11_4, '-*');
Cp55 = Experiment_1(H55, Ch11_5, '-x');
% ---------------------- END ----------------------
I have my own script that retrieves the text from posted .m files that I used for this.
One problem is that you were printing all the data in each call to the function that actually should only print one plot at a time. I changed that. I added DisplayName calls to tine individual plot calls.
Make appropriate changes to get the result you want.
EDIT -- (9 Aug 2025 at 01:11)
I just realised that you want the specific plot details ('MarkerFaceColor', and so on) only with the first data plot ('Ch11_1 = -2.1') and apparently want the other colours to 'float', using the usual MATLAB ColorOrder. I added an if block to create that effect.
.

2 Comments

My pleasure!
If my answerr is exactlly what you needed, pleasse Accept it!

Sign in to comment.

Categories

Products

Release

R2024b

Asked:

on 8 Aug 2025

Edited:

dpb
on 11 Aug 2025

Community Treasure Hunt

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

Start Hunting!