D. Jimenez
D. Jimenez

Reputation: 21

Shifting axis labels in Matlab subplot

The figure below shows the problem I have with the overlaping y-axis labels.

Before fix attempt

Result before fix attempt

To fix this, I tried the following code (individually for each subplot; so h1 for subplot 1, h2 for subplot 2, and so on)

offset = 0.5
h1 = get(gca,'YLabel');
set(h1,'Position',get(h1,'Position') - [0 0 offset])

Whether I try an offset of 0.1, 0.5, 0.9, or higher; the result is always the following:

After attempt

Result after fix attempt

Am I using the command incorrectly or is there a frame around each subplot that prevents me from shifting the y-labels further to the left?

Upvotes: 1

Views: 748

Answers (1)

Suever
Suever

Reputation: 65430

You are applying an offset in the Z direction (the third element of the position vector, [x y z]). Instead, you want to apply the offset to the 1st element of the position vector to shift it in x.

offset = 0.5
h1 = get(gca,'YLabel');
set(h1,'Position',get(h1,'Position') - [offset 0 0])

Also the offset is in the same units as your x axis so you may want to adjust that appropriately. Alternately if you just want a certain percentage of padding you could use the xlims to compute the range of the x axis and use a percentage of that.

padPercent = 0.1;
offset = padPercent * diff(get(gca, 'xlim'));

And as an example

axes();
hlabel = ylabel('YLABEL');
offset = 0.075 * diff(get(gca, 'xlim'));
set(hlabel, 'Position', get(hlabel, 'Position') - [offset, 0 0])

enter image description here

Upvotes: 3

Related Questions