I like to switch x axis with y axis after plotting a graph with matplotlib? Any easy way for it? Thanks in advance.
-
2stackoverflow.com/questions/15767781/… newer duplicate of this posttacaswell– tacaswell2013-04-07 05:26:27 +00:00Commented Apr 7, 2013 at 5: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– Jimmy Lee Jones2018-03-25 07:10:15 +00:00Commented Mar 25, 2018 at 7:10
-
Possible duplicate of python matplotlib: way to transpose axesOriolAbril– OriolAbril2018-04-10 18:52:28 +00:00Commented Apr 10, 2018 at 18:52
-
I'm wondering what the situation is in which you need this, could you explain that?Energya– Energya2018-06-24 09:47:58 +00:00Commented Jun 24, 2018 at 9:47
Add a comment
|
3 Answers
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)
3 Comments
Dr_Zaszuś
What is the first line this solution refers to? Does it mean that if there are >=2 lines, it will fail?
raphael
in case you want to switch multiple lines, simply put it in a loop! (e.g.
for line in ax.lines: ...Bennimi
only work if axis have the same dtype tho!
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)
1 Comment
Daehyok Shin
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.
Rotate the monitor 90 degrees and look at its mirror image. An O(1) solution.
1 Comment
tupui
Love the joke ;)