Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch axes in matplotlib?

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

like image 810
Daehyok Shin Avatar asked Mar 02 '10 08:03

Daehyok Shin


People also ask

How do you flip the Y-axis in Python?

By using invert_yaxis() method To invert Y-axis, we can use invert_yaxis() method. In the above example, we import the matplotlib. pyplot and numpy library.

How do I change the y-axis values in Matplotlib?

To specify the value of axes, create a list of characters. Use xticks and yticks method to specify the ticks on the axes with x and y ticks data points respectively. Plot the line using x and y, color=red, using plot() method. Make x and y margin 0.


1 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) 
like image 100
raphael Avatar answered Oct 13 '22 00:10

raphael