Search code examples
matlabmatlab-figure

Reverse Y-Axis on Axes


I have a GUI window on which I have an axes box and I'd like to reverse Y axis direction on this axes1 box when using a plot function. When I try to use lets say:

set(axes1,'YDir','reverse');

I get the following error

Error using matlab.graphics.chart.primitive.Line/set
There is no YDir property on the Line class.

when I check the properties of this axes1 I get

AlignVertexCenters: 'off'
            Annotation: [1x1 matlab.graphics.eventdata.Annotation]
          BeingDeleted: 'off'
            BusyAction: 'queue'
         ButtonDownFcn: ''
              Children: [0x0 GraphicsPlaceholder]
              Clipping: 'on'
                 Color: [1 0 0]
             CreateFcn: ''
             DeleteFcn: ''
           DisplayName: ''
      HandleVisibility: 'on'
               HitTest: 'on'
         Interruptible: 'on'
              LineJoin: 'round'
             LineStyle: '-'
             LineWidth: 1
                Marker: 'none'
       MarkerEdgeColor: 'auto'
       MarkerFaceColor: 'none'
            MarkerSize: 6
                Parent: [1x1 Axes]
         PickableParts: 'visible'
              Selected: 'off'
    SelectionHighlight: 'on'
                   Tag: ''
                  Type: 'line'
         UIContextMenu: [0x0 GraphicsPlaceholder]
              UserData: []
               Visible: 'on'
                 XData: [1x3937 double]
             XDataMode: 'manual'
           XDataSource: ''
                 YData: [1x3937 double]
           YDataSource: ''
                 ZData: [1x0 double]
           ZDataSource: ''

So I tried changing the YDir on the property inspector from normal to reverse and it didn't work. I've tried using flipud and it just flips the line but not the values on the Y axis.


Solution

  • It appears that what you're calling axes1 is actually a line object. You can easily check this by getting the Type property of the object.

    get(axes1, 'Type')
    
    % Or in newer versions of MATLAB
    class(axes1)
    

    You'll want to instead set the YDir on it's parent axes. We can easily get that using the ancestor function.

    hax = ancestor(axes1, 'axes');
    set(hax, 'YDir', 'reverse')
    

    Or more simply for your specific case:

    set(axes1.Parent, 'YDir', 'reverse')
    

    In the future, carefully read the entire contents of the error message. Here it is very explicit that your command isn't working because it's a line.