35

I like to switch x axis with y axis after plotting a graph with matplotlib? Any easy way for it? Thanks in advance.

Serenity
  • 35,289
  • 20
  • 120
  • 115
Daehyok Shin
  • 415
  • 1
  • 4
  • 5
  • 2
    http://stackoverflow.com/questions/15767781/python-matplotlib-way-to-transpose-axes/15859177#15859177 newer duplicate of this post – tacaswell Apr 07 '13 at 05:26
  • Could you please add some more information as to what exactly do you mean by "switch x axis with y axis after plotting a graph"? Do you want to plot the axis transposed? Do you want to store transposed values? – Jimmy Lee Jones Mar 25 '18 at 07:10
  • Possible duplicate of [python matplotlib: way to transpose axes](https://stackoverflow.com/questions/15767781/python-matplotlib-way-to-transpose-axes) – OriolAbril Apr 10 '18 at 18:52
  • I'm wondering what the situation is in which you need this, could you explain that? – Energya Jun 24 '18 at 09:47

2 Answers2

11

I guess this would be a way to do it in case you really want to change data of an existing plot:

execute this code first to get a figure:

# generate a simple figure
f, ax = plt.subplots()
ax.plot([1,2,3,4,5], [5,6,7,8,9])
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])

and then use this to switch the data of an existing line

# get data from first line of the plot
newx = ax.lines[0].get_ydata()
newy = ax.lines[0].get_xdata()

# set new x- and y- data for the line
ax.lines[0].set_xdata(newx)
ax.lines[0].set_ydata(newy)
raphael
  • 2,159
  • 17
  • 20
0

You can simply switch x and y parameters in the plot function:

I[3]: x = np.linspace(0,2*np.pi, 100)

I[4]: y = np.sin(x)

I[5]: plt.plot(x,y)

I[6]: plt.figure(); plt.plot(y,x)
Gökhan Sever
  • 8,004
  • 13
  • 36
  • 38
  • 16
    Thanks, but it is not the answer I am seeking. I am asking how to switch x and y axes after I have plotted all data. – Daehyok Shin Mar 03 '10 at 04:47