2 axis plotting with different markers

Dear all, i'm trying to plot 2 axis lines with the corresponding 5 different markers. But the code (commented one) always got errors.
Could anyone tell me how can i solve this issue???
Any help is greatly appreciated.
%plotting
% I want to plot two axis with different markers for Yleft(hollow marker)and Xleft (filled marker),
% different markers are connected with lines
rate=[5,10,15,20,25]
Yleft=[1.99,2.78,2.67,2.54,2.45]
d=plot(f,Yleft,'.-')
%plot(f(1),Yleft(1),'o',rate(2),Yleft(2),'*',rate(3),Yleft(3)','^',rate(4),Yleft(4),'square',rate(5),Yleft(5),'dimaond')
% d(1).Marker="o";
% d(2).Marker="*"
% d(3).Marker="^"
% d(4).Marker="square"
% d(5).Marker='diamond'
yyaxis left
title('Yleft and Xleft vs. z ')
xlabel('flow rate [m^3/s]')
ylabel('YLeft [W/m/K]')
yyaxis right
Xleft=[1070,695,636,680,758]
f=plot(rate,Xleft,'*-')
% f(1).Marker="o";
% f(2).Marker="*"
% f(3).Marker="^"
% f(4).Marker="square"
% f(5).Marker='diamond'
ylabel('XLeft [W/m^2/K]')

 Accepted Answer

I would do somthing like this:
%plotting
% I want to plot two axis with different markers for lambda_r (hollow marker)and alpha_w (filled marker),
% different markers are connected with lines
f=[5,10,15,20,25];
lam=[1.99,2.78,2.67,2.54,2.45];
d=plot(f,lam,'.-');
hold on
markers = {'o','*','^','square', 'diamond'};
for i = 1:numel(f)
plot(f(i),lam(i),markers{i});
end
yyaxis left
title('lambda\_r and alpha\_w vs. z ')
xlabel('flow rate [m^3/s]')
ylabel('lambda\_r [W/m/K]')

2 Comments

Even though VBBV already posted, I'll answer to your questions regarding the color in the following way:
%plotting
% I want to plot two axis with different markers for lambda_r (hollow marker)and alpha_w (filled marker),
% different markers are connected with lines
f=[5,10,15,20,25];
lam=[1.99,2.78,2.67,2.54,2.45];
plot(f,lam,'b.--' )
hold on
markers = {'o','*','^','square', 'diamond','b'};
for i = 1:numel(f)
plot(f(i),lam(i),['b' markers{i}]);
end
yyaxis left
title('lambda\_r and alpha\_w vs. z ')
xlabel('flow rate [m^3/s]', 'Color', 'k')
ylabel('lambda\_r [W/m/K]', 'Color', 'b')
ax = gca;
set(ax, 'YColor', 'r')
yyaxis right
k_w=[1070,695,636,680,758]
k_w = 1×5
1070 695 636 680 758
plot(f,k_w,'r*--')
markers = {'o','*','^','square', 'diamond'};
for i = 1:numel(f)
d = plot(f(i),k_w(i),['r' markers{i}]);
end
ylabel('alpha\_w [W/m^2/K]', 'Color', 'r')
ax = gca;
set(ax, 'YColor', 'b')
Dear @Askic V, thank you very much! This works!!!
But i have one last question: Do you know to legend out only the "o" markers in both red and blue lines??
i try to legend both of them. However, the first legend always comes out the "-" straight line marker, instead of the round "o" marker legend...

Sign in to comment.

More Answers (1)

If you want to have legend only on points 'o' in both red and blue lines, I would do this:
%plotting
% I want to plot two axis with different markers for lambda_r (hollow marker)and alpha_w (filled marker),
% different markers are connected with lines
f = [5,10,15,20,25];
lam = [1.99,2.78,2.67,2.54,2.45];
plot(f,lam,'b.-' );
hold on
markers = {'o','*','^','square', 'diamond','b'};
for i = 1:numel(f)
d(i)= plot(f(i),lam(i),['b' markers{i}]);
end
yyaxis left
title('lambda\_r and alpha\_w vs. z ')
xlabel('flow rate [m^3/s]', 'Color', 'k')
ylabel('lambda\_r [W/m/K]', 'Color', 'b')
ax = gca;
set(ax, 'YColor', 'r')
yyaxis right
k_w=[1070,695,636,680,758];
plot(f,k_w,'r*--')
markers = {'o','*','^','square', 'diamond'};
for i = 1:numel(f)
h(i) = plot(f(i),k_w(i),['r' markers{i}]);
end
ylabel('alpha\_w [W/m^2/K]', 'Color', 'r')
legend([d(1) h(1)],'Point o blue','Point o red');
ax = gca;
set(ax, 'YColor', 'b')

1 Comment

Wow, thank you very much sir!!!!! I was struggling with this for the whole day yesterday.
Thank you again!

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2022a

Asked:

on 17 Dec 2022

Edited:

on 27 Mar 2023

Community Treasure Hunt

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

Start Hunting!