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

9 visualizzazioni (ultimi 30 giorni)
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 Commenti
dpb
dpb il 8 Ago 2025
Modificato: dpb il 8 Ago 2025
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 .
dpb
dpb il 8 Ago 2025
Modificato: dpb il 8 Ago 2025
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.

Accedi per commentare.

Risposta accettata

dpb
dpb il 8 Ago 2025
Modificato: dpb il 9 Ago 2025
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 Commenti
dpb
dpb il 11 Ago 2025
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...
dpb
dpb il 11 Ago 2025
Modificato: dpb il 11 Ago 2025
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..

Accedi per commentare.

Più risposte (1)

Star Strider
Star Strider il 8 Ago 2025
Modificato: Star Strider il 9 Ago 2025
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.
.

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Help Center e File Exchange

Prodotti


Release

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by