Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turning y axis upside down in MATLAB

Tags:

matlab

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 ;-)

like image 441
Rook Avatar asked Nov 19 '09 03:11

Rook


People also ask

How do I change the direction of the y-axis in MATLAB?

Control the direction of increasing values along the x-axis and y-axis by setting the XDir and YDir properties of the Axes object. Set these properties to either 'reverse' or 'normal' (the default). Use the gca command to access the Axes object. stem(1:10) ax = gca; ax.

How do you flip an upside down graph in MATLAB?

Ydir in reverse with flip top/bottom. If you want to flip left/right you must perform Xdir reverse.

How do you flip Y-axis?

To make this change, right-click and open up axis options in the Format Task pane. There, near the bottom, you'll see a checkbox called "values in reverse order". When I check the box, Excel reverses the plot order.

Why is my graph upside down MATLAB?

The upside-down numbers in figure axes can be resolved by changing the OPENGL renderer in MATLAB 7.9 (R2009b). If the axes show correct numbered labels after following the steps above, then the issue is with the hardware version of OpenGL rendering.


2 Answers

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)); 
like image 116
gnovice Avatar answered Oct 12 '22 19:10

gnovice


The command

axis ij

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

like image 28
kashiraja Avatar answered Oct 12 '22 19:10

kashiraja