Turning y axis upside down in MATLAB

0 votes
asked Nov 19, 2009 by rook

Is there a way to turn the y axis upside down in matlab plots, so that the positive direction of the y axis, instead of up, points down ?

(I beg of you; please do not say, print it out, then turn the paper around ;-)

5 Answers

0 votes
answered Nov 19, 2009 by gnovice

The 'YDir' axes property can be either 'normal' or 'reverse'. By default it is 'normal' for most plots, but some plots will automatically change it to 'reverse', such as the image or imagesc functions.

You can set the y-axis direction of an axes with either the set function or dot indexing (in newer MATLAB versions):

h = gca;  % Handle to currently active axes
set(h, 'YDir', 'reverse');
% or...
h.YDir = 'reverse';

I'm baffled by some of the other answers saying that the 'YDir' property has somehow disappeared or is giving an error. I haven't seen any such behavior in versions of MATLAB from 2013, 2014, or 2016. There are only two potential pitfalls I came across:

  • The property can't be set with a cell array, only a character string:

    >> set(gca, 'YDir', {'reverse'});
    Error using matlab.graphics.axis.Axes/set
    While setting property 'YDir' of class 'Axes':
    Invalid enum value. Use one of these values: 'normal' | 'reverse'.
    

    although this works:

    set(gca, {'YDir'}, {'reverse'});  % Property name is also a cell array
    
  • The gca function can't be used interchangeably as a handle when performing dot indexing (which is why I first saved it to a variable h in the above example):

    >> gca.YDir
    Undefined variable "gca" or class "gca.YDir". 
    >> gca.YDir = 'reverse'  % Creates a variable that shadows the gca function
    gca = 
      struct with fields:
    
        YDir: 'reverse'
    

Finally, if you want some code that will toggle the 'YDir' property no matter what its current state is, you can do this:

set(gca, 'YDir', char(setdiff({'normal', 'reverse'}, get(gca, 'YDir'))));
% or...
h = gca;
h.YDir = char(setdiff({'normal', 'reverse'}, h.YDir));
0 votes
answered Jan 14, 2013 by kashiraja

The command

axis ij

Will also reverse the Y-axis (negative above x-axis; positive below).

0 votes
answered Jan 11, 2015 by ejo60

The solutions on the top of the stack did not work for me,

  • imagesc(x,y,data) % results in a flipped plot, the y axis is upside down

  • set(gca,'YDir','reverse'); % gives an error

  • axis ij; % still gives the flipped plot

what did work was the following:

imagesc(x,y,data); axis xy;  % results in the correct plot

The YDir property has vanished in the matlab version (2013 and up) that I'm using.

0 votes
answered Jan 22, 2015 by vxxed

To update this answer, since it is still a popular Google result: As of R2014a, the correct way to flip the Y axis is the following:

>> axis ij

This change can be reversed through the following command

>> axis ji

To flip the X or Z axes, do the following

set(gca,'XDir','reverse');

set(gca,'ZDir','reverse');

Personally, I think it would have been easier to keep the YDir option, but what do I know.

0 votes
answered Sep 15, 2017 by steve

As an alternative to YDir (for some reason I cannot currently see) you can rotate the axes with view. To turn the y-axis upside down, use

view(0,-90);
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...