Borrar filtros
Borrar filtros

ylabel changes position with ax.YAxisLocation = 'origin'

40 visualizaciones (últimos 30 días)
Sim
Sim el 24 de Jun. de 2024 a las 16:07
Comentada: Star Strider el 24 de Jun. de 2024 a las 17:27
If I use
ax.YAxisLocation = 'origin';
the text of the ylabel changes position, going to the top of the plot and appearing horizontally, instead of being in a vertical posiyion, alongside the y-axis, as in a usual plot:
hold on
scatter(-rand(1,10),rand(1,10),120,'or','filled')
scatter(rand(1,10),rand(1,10),120,'ob','filled')
ax = gca;
ax.YAxisLocation = 'origin';
ylabel('my y-label')
How to have the ylabel in a vertical position, alongside the y-axis, as in a usual plot, when using ax.YAxisLocation = 'origin' ?

Respuesta aceptada

Star Strider
Star Strider el 24 de Jun. de 2024 a las 16:46
The axis labels are text objects with the same properties. (Although not all the properties, such as 'VerticalAlignment' are listed in the ylabel properties, they can still be adressed.)
Try this —
hold on
scatter(-rand(1,10),rand(1,10),120,'or','filled')
scatter(rand(1,10),rand(1,10),120,'ob','filled')
ax = gca;
ax.YAxisLocation = 'origin';
hyl = ylabel('my y-label');
% get(hyl)
pos = hyl.Position;
hyl.Position(2) = mean(ylim); % Use Original Values, Excep[t For The 'y' Coordinate
hyl.Rotation = 90; % Change 'Rotation' Value
hyl.VerticalAlignment = 'middle'; % Set Hidden 'text' Property
There are still other tweaks possible if this is not exactly what you want.
.
  2 comentarios
Sim
Sim el 24 de Jun. de 2024 a las 17:08
thanks a lot @Star Strider! Very useful your answer as well! :-)
Star Strider
Star Strider el 24 de Jun. de 2024 a las 17:27
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

the cyclist
the cyclist el 24 de Jun. de 2024 a las 16:37
Editada: the cyclist el 24 de Jun. de 2024 a las 16:39
It's a bit kludgy, but
rng default
hold on
scatter(-rand(1,10),rand(1,10),120,'or','filled')
scatter(rand(1,10),rand(1,10),120,'ob','filled')
ax = gca;
ax.YAxisLocation = 'origin';
yL = ylabel('my y-label');
set(yL,"Rotation",90)
set(yL,"Position",[-0.25 0.5 -1])
Make that first number defining the Position closer to -1, if you had wanted the label all the way off to the left.
  1 comentario
Sim
Sim el 24 de Jun. de 2024 a las 16:43
Editada: Sim el 24 de Jun. de 2024 a las 17:10
Thanks a lot @the cyclist!! Both your and @Star Strider solutions are great!
This time I would accept the @Star Strider one, since to me, it looks like slightly "more automatic", i.e. I do not need to set manually the values in here:
set(yL,"Position",[-0.25 0.5 -1])

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by