how to flip the direction of the axis?

1 121 vues (au cours des 30 derniers jours)
Lilya
Lilya le 28 Oct 2018
Commenté : DGM le 17 Jan 2025 à 12:05
Dear all,
I want to change the x-axis direction from right to left but also keeping the values to be not affected (reversed/flipped). when I used the fooling command the mentioned problem is caused:
set(gca, 'XDir','reverse')
How can I solve that?
thanks

Réponse acceptée

madhan ravi
madhan ravi le 28 Oct 2018
xticklabels([1:10]) %an example
set ( gca, 'xdir', 'reverse' )
  2 commentaires
madhan ravi
madhan ravi le 28 Oct 2018
It’s 'xdir' not 'XDir'
DGM
DGM le 11 Fév 2024
Modifié(e) : DGM le 11 Fév 2024
From @Matthew Stengrim's flag-as-comment:
"incorrect"
While that's a valid point, please use comments and try to explain what you mean to readers instead of just sniping with unhelpful single-word comments.
set() and get() are case-insensitive:
xticklabels([1:10]) %an example
set ( gca, 'XDir', 'reverse' ) % set still works
% see also:
get(gca,'XDir') % the actual case of the property name
ans = 'reverse'
get(gca,'xdir') % lower case
ans = 'reverse'
get(gca,'xdIR') % goofy case
ans = 'reverse'
% case does matter if you directly use dot indexing on the axes object
hax = gca;
hax.XDir % there is an XDir property
ans = 'reverse'
hax.xdir % but there is no xdir property
Unrecognized method, property, or field 'xdir' for class 'matlab.graphics.axis.Axes'.
As to what the nuances of OP's actual problem were, I can't know.

Connectez-vous pour commenter.

Plus de réponses (1)

Oli Fairfax
Oli Fairfax le 16 Jan 2025 à 10:18
Modifié(e) : Oli Fairfax le 16 Jan 2025 à 10:19
You're probably better off reversing the data and the axes rather than playing with the tick lables, in my opinion:
% Example data
x = [1:0.1:10];
y = sin(x);
% Normal plot
figure;
plot(x,y)
% Reversed axes plot
figure
plot(flip(x),y)
set(gca, 'XDir','reverse')
  1 commentaire
DGM
DGM le 17 Jan 2025 à 12:05
Yes, sometimes it's necessary to do some combination of flipping the data and/or the axes. This is often an issue when trying to plot data overlaid atop images.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by