# Plotting Multiple y-axes with different range on the same plot

18 Ansichten (letzte 30 Tage)
Isa Isa am 23 Dez. 2012
Bearbeitet: emami.m am 4 Jan. 2021
Good Day ,
Please assist me on these problems
1. I wish to put six plots on the same plot using matlab. The details of each plot are stated below
PLOT 1 x axis range 0:1 y axis range 0:1
PLOT 2 x axis range 0:1 y axis range 0:1
PLOT 3 x axis range 0:1 y axis range 0:1
PLOT 4 x axis range 0:1 y axis range 0:1
PLOT 5 x axis range 0:1 y axis range 0:2
PLOT 6 x axis range 0:1 y axis range 0:7
Please how do I put all these plots on one single plot despite different y axis range?
Thanks
Best regards, Isa
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Antworten (3)

Laura Proctor am 23 Dez. 2012
Bearbeitet: Laura Proctor am 23 Dez. 2012
The issue is that this function can only plot one line per set of axes - so only three lines.
I have hacked Denis Gilbert's plotyyy code to facilitate your specific needs. See below:
%%Dummy data
x1 = 0:0.01:1;
x2 = x1;
x3 = x1;
x4 = x1;
x5 = x1;
x6 = x1;
y1 = x1;
y2 = x2.^2;
y3 = x3.^3;
y4 = sin(x4);
y5 = fliplr(2*x5.^2);
y6 = 7*x6;
ylabels{1}='A'; ylabels{2}='B'; ylabels{3}='C';
fh = figure('units','normalized');
cfig = get(fh,'Color');
ah(1) = axes('Parent',fh);
lh1 = plot(x1,y1,x2,y2,x3,y3,x4,y4);
lineColors = get(ah(1),'ColorOrder')
ah(2) = axes('Parent',fh);
lh2 = plot(x5,y5,'Color',lineColors(5,:));
set(ah(2),'YAxisLocation','right','Color','none');
pos = [0.1 0.1 0.7 0.8];
offset = pos(3)/5.5;
%Reduce width of the two axes generated by plotyy
pos(3) = pos(3) - offset/2;
set(ah,'position',pos);
%Determine the position of the third axes
pos3=[pos(1) pos(2) pos(3)+offset pos(4)];
%Determine the proper x-limits for the third axes
limx1=get(ah(1),'xlim');
limx3=[limx1(1) limx1(1) + 1.2*(limx1(2)-limx1(1))];
ah(3)=axes('Position',pos3,'box','off',...
'Color','none','XColor','k','YColor',lineColors(6,:),...
'xtick',[],'xlim',limx3,'yaxislocation','right');
lh3 = line(x6,y6,'Color',lineColors(6,:),'Parent',ah(3));
limy3=get(ah(3),'YLim');
%Hide unwanted portion of the x-axis line that lies
%between the end of the second and third axes
line([limx1(2) limx3(2)],[limy3(1) limy3(1)],...
'Color',cfig,'Parent',ah(3),'Clipping','off');
axes(ah(2))
%Label all three y-axes
set(get(ah(1),'ylabel'),'string',ylabels{1})
set(get(ah(2),'ylabel'),'string',ylabels{2})
set(get(ah(3),'ylabel'),'string',ylabels{3})
##### 2 KommentareKeine anzeigenKeine ausblenden
Tony Castillo am 4 Apr. 2017
Bearbeitet: Walter Roberson am 4 Apr. 2017
Hi, Laura I've been trying to adapt your code to my data, but I'm having a message of error, could you help me with it?.
I attached the data I'm actually using to graph it.
close all
clear all
t1=tms(1,:)'/3600;
t = timeseries(t1);
% % Potencias
pot1=(tms(2,:).*tms(3,:))';
pot=timeseries(pot1);
PL=IL*VL; %Carga
% PL.Time=PL.Time/3600;
Pc=Ic*Vc; %Salida de convertidor
% Pc.Time=Pc.Time/3600;
hold on
grid on
ylim([-1000 3e5]);
xlim([0 24]);
ax = gca;
ax.XTick = [0:3:24];
% legend('Cell Array Power','Buck Power', 'Load Power')
%%las graficas
x1 = t;
x2 = t;
x3 = t;
x4 = t;
y1 = pot;
y2 = Pc;
y3 = PL;
y4 = soc;
ylabels{1}='Power (W)'; ylabels{2}='%SOC';
fh = figure('units','normalized');
cfig = get(fh,'Color');
ah(1) = axes('Parent',fh);
lh1 = plot(x1,y1,x2,y2,x3,y3,x4,y4);
lineColors = get(ah(1),'ColorOrder')
ah(2) = axes('Parent',fh);
lh2 = plot(x5,y5,'Color',lineColors(5,:));
set(ah(2),'YAxisLocation','right','Color','none');
pos = [0.1 0.1 0.7 0.8]; %fija la linea Azul que sirve como escala
offset = pos(3)/5.5;
%Reduce width of the two axes generated by plotyy
pos(3) = pos(3) - offset/2;
set(ah,'position',pos);
%Determine the position of the third axes
% pos3=[pos(1) pos(2) pos(3)+offset pos(4)];
%Determine the proper x-limits for the third axes
limx1=get(ah(1),'xlim');
limx3=[limx1(1) limx1(1) + 1.2*(limx1(2)-limx1(1))];
ah(3)=axes('Position',pos3,'box','off',...
'Color','none','XColor','k','YColor',lineColors(6,:),...
% 'xtick',[],'xlim',limx3,'yaxislocation','right');
% lh3 = line(x6,y6,'Color',lineColors(6,:),'Parent',ah(3));
% limy3=get(ah(3),'YLim');
%Hide unwanted portion of the x-axis line that lies
%between the end of the second and third axes
% line([limx1(2) limx3(2)],[limy3(1) limy3(1)],...
% 'Color',cfig,'Parent',ah(3),'Clipping','off');
axes(ah(2))
%Label all three y-axes
set(get(ah(1),'ylabel'),'string',ylabels{1})
set(get(ah(2),'ylabel'),'string',ylabels{2})
Walter Roberson am 4 Apr. 2017
Tony Castillo: what error message do you observe?
Note: you omitted attaching the data.

Melden Sie sich an, um zu kommentieren.

Walter Roberson am 23 Dez. 2012
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Isa Isa am 23 Dez. 2012
Bearbeitet: Walter Roberson am 23 Dez. 2012
Hi Walter,
Thanks. I used plotyyy as advised
ylabels{1}='A';
ylabels{2}='B';
ylabels{3}='C';
[ax,hlines] = plotyyy(x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,ylabels)
I received the following error message
Error using plotyyy
Too many input arguments.
I cannot figure out what went wrong. Please assist.
Thanks
Isa

Melden Sie sich an, um zu kommentieren.

Azzi Abdelmalek am 23 Dez. 2012
Bearbeitet: Azzi Abdelmalek am 23 Dez. 2012
close all
x=0:0.5:10;
y1=0.1*sin(0.1*x);
y2=0.1*cos(0.1*x);
y3=0.1*cos(0.2*x);
y4=0.2*sin(0.4*x);
y5=0.7*sin(0.6*x)
y6=0.1*sin(0.8*x)
%-----------------------------------------------------------------------
xlab='time' % x-axis title
pas=8; % Number of ticks per axe
tail=8; % font size
mark1='d',c1='g'
mark2='<',c2='b'
mark3='s',c3='r'
mark4='p',c4='k'
mark5='o',c5='m'
mark6='*',c6='y';
fontname='courrier'
fontsize=9.2;
fontsize_ax=8;
param1='tg\delta',
param2='Cx',
param3='Us',
param4='Q',
param5='L'
param6='H'
%-----------------y-axis titles----------------------------------------
paramm1='tg\delta .10^-^2',
paramm2='Cx(pF)',
paramm3=' Us(kV)',
paramm4='Q(pC)',
paramm5='L(mm)';
paramm6='H(m)';
%----------------------------------------------plot y1---------------------
plot(x,y1,c1)
ax1=gca;
set(gcf,'position',[100 100 1300 550])
line(x,y1,'color',c1,'Marker',mark1,'LineStyle','none','Markersize',tail,'parent',ax1);
set(ax1,'Ylim',decimal([min(y1) max(y1)]),'Xlim',[0 max(x)]);
ylabel(paramm1)
xlabel(xlab)
set(ax1,'position',[0.16 0.11 0.73 0.8],'fontsize',fontsize_ax,'Ycolor',c1)
pos=double(get(ax1,'position'))
%----------------------------------------------plot y2--------------------
ax2=axes('position',pos, 'XAxisLocation','bottom','YAxisLocation','left', 'Color','none', 'XColor',c2,'YColor',c2);
plot(x,y2,c2);
line(x,y2,'color',c2,'Marker',mark2,'LineStyle','none','Markersize',tail,'parent',ax2);
set(ax2,'Ylim',decimal([min(y2) max(y2)]),'Xlim',[0 max(x)],'Visible','off')
%----------------------------------------------plot y3-------------------
axe3=axes('position',pos, 'XAxisLocation','bottom','YAxisLocation','left','Color','none', 'XColor',c2,'YColor',c2);
plot(x,y3,c3);
line(x,y3,'color',c3,'Marker',mark3,'LineStyle','none','Markersize',tail,'parent',axe3);
set(axe3,'Ylim',decimal([min(y3) max(y3)]),'Xlim',[0 max(x)],'Visible','off')
%----------------------------------------------plot y4-----------------
axe4=axes('position',pos, 'XAxisLocation','bottom','YAxisLocation','left', 'Color','none', 'XColor',c2,'YColor',c2);
plot(x,y4,c4);
line(x,y4,'color',c4,'Marker',mark4,'LineStyle','none','Markersize',tail+2,'parent',axe4);
set(axe4,'Ylim',decimal([min(y4) max(y4)]),'Xlim',[0 max(x)],'Visible','off')
%----------------------------------------------plot y5--------------------
axe5=axes('position',pos, 'XAxisLocation','bottom','YAxisLocation','left', 'Color','none', 'XColor',c2,'YColor',c2);set(axe5,'Xlim',[0 max(x)])
plot(x,y5,c5);
line(x,y5,'color',c5,'Marker',mark5,'LineStyle','none','Markersize',tail,'parent',axe5);
set(axe5,'Ylim',decimal([min(y5) max(y5)]),'Xlim',[0 max(x)],'Visible','off');
%----------------------------------------------plot y6--------------------
axe6=axes('position',pos, 'XAxisLocation','bottom','YAxisLocation','left', 'Color','none', 'XColor',c2,'YColor',c2);set(axe5,'Xlim',[0 max(x)])
plot(x,y6,c6);
line(x,y6,'color',c6,'Marker',mark6,'LineStyle','none','Markersize',tail,'parent',axe6);
set(axe6,'Ylim',decimal([min(y6) max(y6)]),'Xlim',[0 max(x)],'Visible','off');
%---------------------------------------------------------ax12------------
pos12=[pos(1)+pos(3)-0.001 pos(2) 0.001 pos(4)]
axe12=axes('position',pos12,'XAxisLocation','top','YAxisLocation','right',...
'Color','none','fontsize',fontsize_ax, 'XColor',c2,'YColor',c2);
set(get(axe12,'XLabel'),'String',strvcat(' ',paramm2),'Fontname',fontname,'Fontsize',fontsize);
set(axe12,'Ylim',decimal([min(y2) max(y2)]))
inc2=abs(max(y2)-min(y2))/pas;
my=min(y2):inc2:max(y2);
set(axe12,'Ytick',my)
%---------------------------------------------------------ax13-------------
pos13=[0.94 pos(2) 0.001 pos(4)]
axe13=axes('position',pos13, 'XAxisLocation','top','YAxisLocation','right',...
'Color','none','fontsize',fontsize_ax, 'XColor',c3,'YColor',c3);
set(get(axe13,'XLabel'),'String',strvcat(' ',paramm3),'Fontname',fontname,'Fontsize',fontsize)
set(axe13,'Ylim',decimal([min(y3) max(y3)]))
inc3=(max(y3)-min(y3))/pas;
my=min(y3):inc3:max(y3);
set(axe13,'Ytick',my)
%---------------------------------------------------------ax14------------
pos14=[0.11 pos(2) 0.001 pos(4)]
axe14=axes('position',pos14, 'XAxisLocation','Bottom','YAxisLocation','left',...
'Color','none','fontsize',fontsize_ax, 'XColor',c4,'YColor',c4);
set(get(axe14,'XLabel'),'String',strvcat(' ',paramm4),'Fontname',fontname,'Fontsize',fontsize);
set(axe14,'Ylim',decimal([min(y4) max(y4)]))
inc4=(max(y4)-min(y4))/pas;
my=min(y4):inc4:max(y4);
set(axe14,'Ytick',my)
%---------------------------------------------------------ax15------------
pos15=[0.07 pos(2) 0.001 pos(4)]
axe15=axes('position',pos15,'XAxisLocation','bottom','YAxisLocation','left',...
'Color','none','fontsize',fontsize_ax, 'XColor',c5,'YColor',c5);
set(get(axe15,'XLabel'),'String',strvcat(' ',paramm5),'Fontname',fontname,'Fontsize',fontsize)
set(axe15,'Ylim',decimal([min(y5) max(y5)]))
inc5=(max(y5)-min(y5))/pas;
my=min(y5):inc5:max(y5);
set(axe15,'Ytick',my)
%---------------------------------------------------------ax16------------
pos16=[0.03 pos(2) 0.001 pos(4)]
axe16=axes('position',pos16,'XAxisLocation','bottom','YAxisLocation','left',...
'Color','none','fontsize',fontsize_ax, 'XColor',c6,'YColor',c6);
set(get(axe16,'XLabel'),'String',strvcat(' ',paramm6),'Fontname',fontname,'Fontsize',fontsize)
set(axe16,'Ylim',decimal([min(y6) max(y6)]))
inc6=(max(y6)-min(y6))/pas;
my=min(y6):inc6:max(y6);
set(axe16,'Ytick',my)
%------------------------------------------------legend------------------
ax20=axes('position',pos, 'Color','none')
line(-100,100,'color',c1,'Marker',mark1,'LineStyle','-','markerfacecolor',c1,'Markersize',tail,'parent',ax20);
hold on;line(-100,100,'color',c2,'Marker',mark2,'LineStyle','-','markerfacecolor',c2,'Markersize',tail,'parent',ax20);
hold on;line(-100,100,'color',c3,'Marker',mark3,'LineStyle','-','markerfacecolor',c3,'Markersize',tail,'parent',ax20);
hold on;line(-100,100,'color',c4,'Marker',mark4,'LineStyle','-','markerfacecolor',c4,'Markersize',tail+2,'parent',ax20);
hold on;line(-100,100,'color',c5,'Marker',mark5,'LineStyle','-','markerfacecolor',c5,'Markersize',tail,'parent',ax20);
hold on;line(-100,100,'color',c6,'Marker',mark6,'LineStyle','-','markerfacecolor',c6,'Markersize',tail,'parent',ax20);
set(ax20,'Xlim',[0 1]);
set(ax20,'visible','off');
grid(ax1);
name={param1;param2;param3;param4;param5;param6}
hleg=legend(ax20,name)
title(ax1,'figure1')
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
emami.m am 4 Jan. 2021
Bearbeitet: emami.m am 4 Jan. 2021
Hi Azizi,
What is 'decimal' function?
Also, ylabels are not visible.

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by