multiple plots on a single graph with legend labelling

3 views (last 30 days)
Please I have been trying to get a plot as given in the attached picture. I have a code already which i have been trying to modify to give the grapgh as shown below. Although my own plot is that I want to compare three control methodologies.
1. I want the three parameters which are the outputs to be plotted (Y1 Y2 Y3) in relation to each of the controllers to have the same line style. The attached plot has two controllers. One is dashed line and another is solid line. I want my own to have an additional linestyle. Hence each line style represents each controller
2. I want to have legend in terms of the three controllers
3. I want the text label on the plots after plotting with the three controllers be Z_r, N_r and T_l as given in the reference attached.
Below is the code I want to modify. Likewise, Marker and Color may not be neccessary since it will be easier to distinguish as i explained above.
x=time;
y1=n_r(:,1);
y2=zr;
y3=T_l;
%--------------------------------------------------------------------
xlab='time' % x-axis title
pas=8; % Number of ticks per axe
tail=8; % font size
%----------------markers and color/graph----------------------------------
mark1='d',
c1='k'
mark2='<',
c2='b'
mark3='s',
c3='r'
%---------font name and size---------------------------------------------
fontname='courrier'
fontsize=9.2;
fontsize_ax=8;
% -----------your legend names--------------------------------------------
param1='n_r actual',
param2='Reference n_r',
param3='T_l',
%-----------------y-axis titles----------------------------------------
paramm1='y1',
paramm2='y2',
paramm3='y3',
%----------------------------------------------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',[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',[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',[min(y3) max(y3)],'Xlim',[0 max(x)],'Visible','off')
%---------------------------------------------------------ax12-------------
pos12=[0.11 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',[min(y2) max(y2)])
inc2=abs(max(y2)-min(y2))/pas;
my=min(y2):inc2:max(y2);
set(axe12,'Ytick',my)
%---------------------------------------------------------ax13-------------
pos13=[0.07 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',[min(y3) max(y3)])
inc3=(max(y3)-min(y3))/pas;
my=min(y3):inc3:max(y3);
set(axe13,'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);
set(ax20,'Xlim',[0 1]);
set(ax20,'visible','off');
grid(ax1);
name={param1;param2;param3}
hleg=legend(ax20,name)
title(ax1,'figure1')

Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!