figure 4 Y axis with 1 x axis

Hi all
thank you for helping
i want to draw figure in matlab that has two main effect (temprature and effiency ) in Y axis in sides when X axis is same range
for example
x=[11 22 33 44 55];
y1E=[0.5506 0.5925 0.6212 0.6419 0.6573]; %Effeiceny 1
y1T=[43.7529 45.0284 45.9019 46.5297 46.9987]; %Temprature 1
when i use this
Ax=plotyy(x,y1E,x,y1T);
hy1=get(Ax(1),'ylabel');
hy2=get(Ax(2),'ylabel');
set(hy1,'string',' efficiency 1');
set(hy2,'string','Temperature1');
xlabel('Number ');
it gives me figure with 1x axis and 2 Y axis in both side
i want to add another Effeiceny and Temprature with same X and in same figure
y2E=[0.3 0.4 0.6 0.8 0.3]; %Effeiceny 2
y2T=[50 45 43 47 55]; %Temprature 2
how can i do that
thank you for this helping

2 Comments

For cases like this, I doubt plotyy() is the best thing to be using. If you're running R2016a or newer, you can probably try building the plot using yyaxis() instead; I'm not, so this is something I can't test for you.
Generally, in the case when one needs to do complicated plots with multiple coincident axes (e.g. axes in both metric and US customary units), it's best to build the figure by manually overlaying axes objects and working from there. For instance:
Unsurprisingly, there are at least a few tools on the File Exchange which appear to address this inconvenience.
thank you very much for your comment , I found the best one untill now is using plot4y https://www.mathworks.com/matlabcentral/fileexchange/4425-ploty4-m

Sign in to comment.

 Accepted Answer

You can use this function from the file exchange.
clc
clear
t=linspace(0,pi,100);
sinplot=sin(t);
cosplot=cos(t);
tanplot=tan(t);
linecolors={'r' [0 .5 0] 'b'};
h3i=plotNy(t,sinplot,1,t,cosplot,2,t,tanplot,3,...
'Linewidth',1,'YAxisLabels',{'' '' ''},'XAxisLabel','Deg',...
'LineColor',linecolors,...
'FontSize',12,...
'Fontname','TimesNewRoman',...
'Grid','on',...
'LegendString',{'Sin' 'Cos' 'Tan'});
for i=1:length(h3i.ax)
set(h3i.ax(i),'ycolor',linecolors{i});
end

8 Comments

thank you for your answer
it gives me this error
Error using set
Value must be a handle
Error in plotNy (line 435)
set(get(h_ax(i),'XAxis'),'Visible','off')
Error in Untitled (line 18)
h3i=plotNy(t,acc,1,...
Are you using exactly the same code which I have shared, or have you made some changes?
No change al all, i used exactly the same code
Which versionof MATLAB are you using? It is working perfectly fine in 2017b version.
thank you for answering again , i am using 2015a , also i have checked this code , it gives only gives 3Y , i want 4 Y axis . I found the best one untill now is using plot4y https://www.mathworks.com/matlabcentral/fileexchange/4425-ploty4-m , but still not working perfectly .
You can modify the same code a little bit to plot 4 lines.
clc
clear
t=linspace(0,pi,100);
sinplot=sin(t);
cosplot=cos(t);
tanplot=tan(t);
sin_t_by_2_plot=sin(t/2);
linecolors={'r' [0 .5 0] 'b','m'};
h3i=plotNy(t,sinplot,1,t,cosplot,2,t,tanplot,3,t,sin_t_by_2_plot,4,...
'Linewidth',1,'YAxisLabels',{'' '' '',''},'XAxisLabel','Deg',...
'LineColor',linecolors,...
'FontSize',12,...
'Fontname','TimesNewRoman',...
'Grid','on',...
'LegendString',{'Sin' 'Cos' 'Tan','Sin x/2'});
for i=1:length(h3i.ax)
set(h3i.ax(i),'ycolor',linecolors{i});
end
You can use addaxis function to plot from the file exchange.
clc
clear
x = 0:.1:4*pi;
plot(x,sin(x));
addaxis(x,sin(x-pi/3));
addaxis(x,sin(x-pi/2),[-2 5],'linewidth',2);
addaxis(x,sin(x-pi/1.5),[-2 2],'v-','linewidth',2);
addaxis(x,5.3*sin(x-pi/1.3),':','linewidth',2);
addaxislabel(1,'one');
addaxislabel(2,'two');
addaxislabel(3,'three');
addaxislabel(4,'four');
addaxislabel(5,'five');
addaxisplot(x,sin(x-pi/2.3)+2,3,'--','linewidth',2);
addaxisplot(x,sin(x-pi/1),5,'--','linewidth',2);
legend('one','two','three','four','five','three-2','five-2');
thank you very much

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!