views:

119

answers:

1

I've got a Matplotlib graph with two y axes, created like:

ax1 = fig.add_subplot(111)
ax1.grid(True, color='gray')
ax1.plot(xdata, ydata1, 'b', linewidth=0.5)
ax2 = ax1.twinx()
ax2.plot(xdata, ydata2, 'g', linewidth=0.5)

I need grid lines but I want them to apply to both y axes not just the left one. The scales of each axes will differ. What I get is grid lines that only match the values on the left hand axes.

Can Matplotlib figure this out for me or do I have to do it myself?

Edit: Don't think I was completely clear, I want the major ticks on both y axes to be aligned but the scales and ranges are potentially quite different making it tricky to setup the mins and maxes manually to achieve this. I am hoping that matplotlib will be able to do this "tricky" bit for me. Thanks

+1  A: 

EDIT

Consider this simple example:

from pylab import *

# some random values
xdata = arange(0.0, 2.0, 0.01)
ydata1 = sin(2*pi*xdata)
ydata2 = 5*cos(2*pi*xdata) + randn(len(xdata))

# number of ticks on the y-axis
numSteps = 9;

# plot
figure()

subplot(121)
plot(xdata, ydata1, 'b')
yticks( linspace(ylim()[0],ylim()[1],numSteps) )
grid()

subplot(122)
plot(xdata, ydata2, 'g')
yticks( linspace(ylim()[0],ylim()[1],numSteps) )
grid()

show()

alt text

Amro
Thanks, I'm aware that I could set the limits manually, that's what I'm trying to avoid :)
Tim
Its not that involved, all you have to do is take the min/max of both `ydata1` and `ydata2` and set it as the y-limits for both axes
Amro
I don't think will achieve what I want, I've edited the question to try and clarify what I'm trying to do.
Tim
@Tim: I edited my answer... Now as you can the plots have two different ranges but with the same number of yticks on the y-axis
Amro
Thanks, although it's not exactly what I was hoping for it's given me the info I need to get the result I want. I wasn't familiar with the linspace() function, that'll make it much simpler.
Tim