Hi,
I'm shocked by how unobvious this is, so sorry for asking. I created a multiple plot figure with double axis, and all share the same label for it, as you can see in the figure. I'd like to move the label for that axis to make it more centered. Is there a way to control that from the editor? I dived into the plot browser, but I can only modify the tick labels, not the axis label. Any ideas?
Thank you in advance!!

 Réponse acceptée

dpb
dpb le 20 Oct 2016
Modifié(e) : dpb le 21 Oct 2016

0 votes

The axis labels are centered on the axis to which they're attached. It appears you used the third (2nd from bottom) subplot to attach the label to.
You can move the label via the editor manually, but programmatically, it's possible by changing the 'position' property of the label. The second value of the three-vector is the vertical location for the center of the label computed to center it on that axes in units of data coordinates. This is quite inconvenient for multiple plots where the data values aren't the same and there are multiple axes. The best idea I've got programmatically would be to
  1. retrieve the axes positions for the bottom and top axes;
  2. compute the vertical extent of these and then
  3. write the label using that position with normalized coordinates
...[former manner elided for brevity...the following is much better]...
The only other solution that eases some of those problems but has other complexities is that one could create another axes of the same extent vertically as the range of the bottom and top subplots but laying to the right of the RH axes. There's an example in the documentation that overlays another axes in the middle of a 2x2 grid that outlines the basic procedure. In this case, with that axes of that overall height, the ylabel would automagically be centered vertically.
There's a File Exchange submission for super titles that addresses the issue for titles not being associated with each axes but overall; whether somebody may have submitted similar for legends or not I don't know; undoubtedly worth a search to see what could find.
ADDENDUM
As noted, had a few minutes and looked at the added-axes option a little more in depth...
for i=1:4,hA(i)=subplot(4,1,i);end
pos1=get(hA(1),'position'); % extents of top axes
pos4=get(hA(4),'position'); % and bottom
pos=[pos4(1:3) pos1(2)+pos1(4)-pos4(2)]; % overall extent of all axes
hAOuter=axes('position',pos,'visible','off'); % invisible one overlaying those
hL=ylabel(hAOuter,'Charcoal index','fontsize',12,'visible','on');
NB: 'visible','on' in the ylabel properties; it'll be 'off' by default as its value is inherited from the parent axes that we made invisible.

8 commentaires

MMARTINC
MMARTINC le 21 Oct 2016
Thanks a lot dpb!! I somehow hoped it would be an straight forward thing I was missing. I guess it wasn't :-). I'll try it and let you know how it went. c[_] << Here's a cup of coffee (kind of) for you, you well deserved it!! ;-)
dpb
dpb le 21 Oct 2016
Well, almost nothing is straightforward w/ HG once you get past what's prepackaged, unfortunately...one can do quite a lot indeed, but it's pretty dense to delve into.
I did have a chance to look at the other alternative of the other axes a little more--it's not as painful as might think; I added an addendum to the previous answer...
Hi! Sorry for the late response.
I finally put your suggestion into practice and although I had to fight a little bit against it I finally worked it out :-).
This option won't let you modify the label in the Editor neither, but you can adjust the x position manually. So I added a workaround to the end of your code, which is not very clean, but worked for me.
I named my subplots h(1) to h(4), used your code for the left and right axis label and set the position afterwards, so just by adding this (dpb's code plus the last line) to the end of my code it worked perfectly :-):
% Left axis
pos1=get(h(1),'position'); % extents of top axes
pos4=get(h(4),'position'); % and bottom
pos=[pos4(1:3) pos1(2)+pos1(4)-pos4(2)]; % overall extent of all axes
hAOuter=axes('position',pos,'visible','off'); % invisible one overlaying those
hL=ylabel(hAOuter,'Left axis label','fontsize',18,'visible','on');
set(hL,'position',get(hL,'position')-[0.05 0 0]);
% Right axis
pos1=get(h(1),'position'); % extents of top axes
pos4=get(h(4),'position'); % and bottom
pos=[pos4(1:3) pos1(2)+pos1(4)-pos4(2)]; % overall extent of all axes
hAOuter=axes('position',pos,'visible','off'); % invisible one overlaying those
hL=ylabel(hAOuter,'Right axis label','fontsize',18,'visible','on','Color',[0.8 0.2 0]);
set(hL,'position',get(hL,'position')+[1.18 0 0]);
I hope this helps if someone else happens to get the same issue :-)
Thanks again dpb for your help
All the best
dpb
dpb le 27 Oct 2016
Modifié(e) : dpb le 29 Oct 2016
I don't follow the problem, quite...?
NB: Your second section has an issue in that you overwrite the two handles of the outer axes and labels so you've lost access to the first one.
was created from the previous by adding only two lines--
hAOuter(2)=axes('position',pos,'visible','off','yaxislocation','right');
hL(2)=ylabel(hAOuter(2),'RH Axis Label','fontsize',12,'visible','on');
I had no difficulty moving those labels around with the figure editor tool, but didn't really see a need to???
MMARTINC
MMARTINC le 28 Oct 2016
Thank you :-). Maybe I should have said that I was a bit of a noob in my first comment, so probably there's something obvious I'm missing.
So when I included your piece of code to my script I got my figure with a left label that I couldn't move in the editor to the right side, so I dug a bit into other posts until I found a way of placing the label to the right side.
I created a label using your way for each of the sides so they're automatically centered and moved them using the last line of the code. It is true that I should have named the axes differently though. Also, I didn't even think that I could specify which axis (left or right) by just adding the "(2)" at the end!! sorry for that, it does make sense now that you said it :-D
Cheers!!
"...specify which axis (left or right) by just adding the "(2)"..."
The (2) has nothing to do with specifying L or R; it's simply adding a second element into the handle arrays for the additional axes and labels when they're created. The properties (other than default) are set by the arguments to the axes and ylabel calls that create the objects and return those handles.
When using handle graphics for anything that isn't built into the default operations, you should refresh your memory and/or augment your knowledge of what properties are available that may be amenable to the objective(s) you're trying to achieve. One very useful "trick" is to simply use
set(ObjectHandle)
which will echo all properties of the given object referred to by the ObjectHandle (here one of the hA array, say). Perusing this output list will show you things such as the YAxisLocation' property as
YAxisLocation: [ {left} | right ]
indicating the two possible values and the default emphasized. From this it wouldn't be too hard to envision what should do to get the desired label on the RH side of the figure instead of left.
I apologize for presuming a somewhat more advanced knowledge; I did figure it would be apparent what to do for a RH axes instead of LH from the example.
MMARTINC
MMARTINC le 29 Oct 2016
Thanks :-). I sometimes struggle to follow some of the basics, because I started just having to get things done and looking for ways of achieving it. I'll (hopefully) soon have the time to make sense of these kind of things so I don't find myself in these situations :-D. I really appreciate that you put such an effort to help me, I hope I didn't annoy you too much!! All the best!!
dpb
dpb le 29 Oct 2016
I'd note that spending some time reading thru the tutorial "Getting Started" doc will pay large dividends in speeding up the process of, well, "getting started". :) It'll be quicker payback than time simply hunting for some solution or having to post queries in terms of return for effort...

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by