In Matlab how can I exchange the horizontal and vertical axes of an existing plot

11,187

Interesting question +1. The following example shows how to exchange the x and y axes of the current figure:

X = (1:100)'; %# Create x axis data
Y = randn(100, 1); %# Create y axis data
plot(X, Y); %# Plot the data
view(-90, 90) %# Swap the axes
set(gca, 'ydir', 'reverse'); %# Reverse the y-axis (Optional step)

Also, a relevant link to Matlab Central is here.

Share:
11,187
Isopycnal Oscillation
Author by

Isopycnal Oscillation

Updated on July 20, 2022

Comments

  • Isopycnal Oscillation
    Isopycnal Oscillation almost 2 years

    Suppose I have vectors x and y, I know I can do plot(x,y) or plot(y,x) to achieve what I want. However, my question is specifically: If I have a plot already created in a figure as plot(x,y), how can I programmatically exchange the horizontal and vertical axes so that effectively I am saying plot(y,x)?

    • noufal
      noufal about 11 years
      can you please share where does it practically needs to do? it seems to be quiet interesting though you have both the vectors ready in your hand...
    • Dan
      Dan about 11 years
      Are you saying that you no longer have x and y in memory?
    • Isopycnal Oscillation
      Isopycnal Oscillation about 11 years
      @noufal I am using a GUI of which I have limited control (I am not allowed to fundamentally alter its structure although I can add on to it). The GUI spits out the plot with the axes arranged in a particular way of which I need the opposite. Since I have limited control over the GUI itself this is the only option...
    • Isopycnal Oscillation
      Isopycnal Oscillation about 11 years
      @dan Yes, I do have them in memory.
    • Dan
      Dan about 11 years
      If they are in memory then why not just go plot(y,x)?
    • noufal
      noufal about 11 years
      @IsopycnalOscillation fine... I didn't think about such a case. . .