Flip the Y-axis ticks without flipping the plot

66 visualizaciones (últimos 30 días)
I'm having a 2D scatter plot. The Y-axis ticks go like 0,5,10..35
I want the plot to remain as it is, but flip the ticks so that they go like 35,30...5,0.
Can this be done?
  1 comentario
Adam
Adam el 2 de Nov. de 2018
Editada: Adam el 2 de Nov. de 2018
Surely if you flip only the y axis values then the plot will be wrong if it stays as is? If flipping only the y axis is what makes it correct then I would suggest you plot your data correctly in the first place so that it is consistent.
Flipping the y axis is just a matter of whether you have the origin at the top or the bottom, but the data will move with it, if you just change the YDir setting of the axes

Iniciar sesión para comentar.

Respuesta aceptada

madhan ravi
madhan ravi el 2 de Nov. de 2018
yticklabels([1:10]) %an example
set ( gca, 'ydir', 'reverse' )
  3 comentarios
madhan ravi
madhan ravi el 2 de Nov. de 2018
Anytime :)
Adam
Adam el 2 de Nov. de 2018
It doesn't answer the question you actually asked! It gives a good solution if you want to flip the axes and have the data consistently flip with it, as you would often want to happen. Wanting the axes to flip but the data to stay put is not generally desirable though anyway.

Iniciar sesión para comentar.

Más respuestas (2)

Elias Gule
Elias Gule el 2 de Nov. de 2018
I guess you want something like what the ff code produces.
x = 30:5:65;
y = 0:5:35;
scatter(x,y);
yTickLabels = arrayfun(@num2str,sort(y,'descend'),'uni',false);
ax = gca;
ax.YAxis.TickLabels = yTickLabels;

Pradyumna Byappanahalli Suresha
Pradyumna Byappanahalli Suresha el 15 de Abr. de 2020
Editada: Pradyumna Byappanahalli Suresha el 15 de Abr. de 2020
If you are plotting a matrix via imagesc or something similar, below method helps in inverting the tickAxes without flipping the data. This is an improved version of Elias Gule's answer.
% Generate a random 100X100 matrix and plot it via `imagesc`.
s = rand(100,100);
imagesc(s);
ax = gca;
% Replace the original yTick values of the `imagesc` plot.
yTicks = ax.YAxis.TickValues;
tickDifference = yTicks(2) - yTicks(1);
for ii = 1:length(yTicks)
yTicks(ii) = size(s,1) - tickDifference * (ii - 1);
end
yTicks = sort(yTicks);
ax.YAxis.TickValues = yTicks;
% Replace the ticklabel values to match the flipped axis.
yTickLabels = ax.YAxis.TickLabels;
scaling = 10^double(ax.YAxis.Exponent); % Take care of scaling
for ii = 1:length(yTickLabels)
yTickLabels{ii} = num2str(size(s,1) - str2double(yTickLabels{ii})*scaling);
end
ax.YAxis.TickLabels = yTickLabels;

Community Treasure Hunt

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

Start Hunting!

Translated by