How to label x and y axis in case of subplots

4 visualizaciones (últimos 30 días)
Safi ullah
Safi ullah el 9 de Mzo. de 2017
Editada: dpb el 11 de Mzo. de 2017
I have used the following loop to get subplots.
for j=1:19;
Aj=B(j,:);
subplot(5,4,j);
plot(Aj,h)
end
for all these subplots I need to have only one x-label and one y-label. Also how to insert legend to all the subplots.

Respuesta aceptada

dpb
dpb el 9 de Mzo. de 2017
Editada: dpb el 11 de Mzo. de 2017
Post comments:
for j=1:20 % note filled out the array fully here
hAx(j)=subplot(5,4,j); % save the subplot axes handle
...
end
p1=get(hAx(1),'position'); % position of UL-most axes
p2=get(hAx(20),'position'); % ditto for LR-most
hAxOuter=axes('position',[p1(1) p2(2) p2(1)+p2(3)-p1(1) p1(2)+p1(4)-p2(2)], ...
'color','none','visible','off'); % an outer axis for global p
delete(hAx(20)) % remove the unwanted last subplot axes
hX=text(-0.1,0.5,'YLabel','rotation',90,'fontsize',11, ...
'horizontalalignment','center','verticalalignment','bottom');
hY=text(0.5,-0.1,'XLabel','rotation',0,'fontsize',11, ...
'horizontalalignment','center','verticalalignment','top');
hL(1)=plot(hAx(1),1:3,randn(1,3)); % throw some random data
hL(19)=plot(hAx(19),1:3,rand(1,3)); % on a couple subplot axes
hLg=legend(hAxOuter,hL([1;19]),num2str([1;19],'Line %02d'),'location','southeast');
produces the attached figure. The outer axis is used to get positions for writing globally via text; x/ylabel for that axes will position the labels where want but if that axis is visible the left/bottom axis lines are visible(*) but need an axes of that size enclosing all the others for easily computing the positions. I filled out the full 5x4 array so could retrieve the bounding limits of the range from only two handles; otherwise need three to get all the coordinates needed. The algebra in computing the position looks much more complicated than it is; simply the position vector is [left bottom width height] so it's taking the left and bottom positions as reference and adding the difference of topmost/rightmost for the total height/width to overlay the new axis on top of the outline of the subplots.
The legend written in the lower RH corner in the blank space seems like is going to get very cramped for room with 19 lines; I've got other engagement at the moment but give it a go using the handles for the lines retrieved from plot--ensure you don't overwrite them; if there's one line per axis as 1D array is ok; if more then you really have a space constraint though and it appears your code does do only one line per...likely you'll have to go to one of the 'outside' positions, I'd guess.
(*) Continuing beef of some 20-yr standing. Even with R2015 release of the accessibility of the [X/Y/Z]Axis properties via the various "ruler" objects and the (finally!!!) format option for numeric values, we still have from the documentation
NumericRuler Properties
Control axis with numeric values
NumericRuler properties control the appearance and behavior of an x-, y-, or
z-axis that shows numeric values. ... By changing property values of the
ruler, you can modify certain aspects of a specific axis.
...
Appearance
Color — Color of axis line and labels
...
So, while provided access to the axes, the Color attribute is still only a single property for both the label and the axis, including the line and there's also no separate 'visible' property for the axis line itself; only the axes object as a whole. Hence, one can't have the outer axes created above and use the builtin labels for it because to make them show up the lines will also have to show and they will occlude the subplot axes. Dumb! :(
And, the other "obvious" workaround doesn't either of first finding the bounding size needed, then create hAxOuter first, then the subplots so they're on the foreground plane. Great, except subplot deletes the other axes underneath with no way to override that behavior so you can't do this except by explicitly calling axes with the 'position' argument for each subplot which is more painful than the above mishmash with text imo. One could, I suppose, go thru the exercise twice't; make all the subplots and save the position vectors for each, then delete them all and build the outer box and then recreate via axes. A lot of overhead and I'm not positive whether it works or not without trying...
  2 comentarios
Safi ullah
Safi ullah el 10 de Mzo. de 2017
@dpb I need the superlabel of x and y only?
dpb
dpb el 10 de Mzo. de 2017
Ah....different problem.
That'll probably be easiest by putting another axes on the figure that overlays the bounds of the subplots. For a legend then you'll need to have also saved the line handles to apply to desired lines; that'll be a 2D array of subplot,lines for each.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by