Skip to content

Reverse axis direction in Pylab

January 14, 2010

I have been trying to remember how to do this FOREVER! I’m trying to plot a sound speed profile with depth along the y-axis and sound speed along the x-axis. But if I don’t reverse axes, the depth increases up, which isn’t really very intuitive.  In Matlab, it’s pretty easy.  But how to do it in Python/Pylab?  Here’s what the plot looked like before reversing the y-axis.

Here’s the code I used to reverse the axis:

from pylab import *

### SKIPPING SOME CODE HERE

# Plot sound speed profiles
plot(sspvector1,(depthvector1))
plot(sspvector2,(depthvector2),'r')

# Reverse the y-axis
ax = gca()
ax.set_ylim(ax.get_ylim()[::-1])

title('Sound speed profiles')
xlabel('Sound speed (m/s)')
ylabel('Depth (m)')
grid()
show()

And here’s the result:

Advertisements
One Comment leave one →
  1. anon emouse permalink
    October 11, 2011 6:33 am

    omg you saved my research! 🙂 tx!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: