Tim
Tim

Reputation: 105

Matplotlib: multiple y axes, grid lines applied to both?

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

Upvotes: 5

Views: 8360

Answers (1)

Amro
Amro

Reputation: 124553

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

Upvotes: 4

Related Questions