# full grid for 2nd y-axis wit twinx() ?

Hi,

I need a twinx() plot with horizontal and vertical grid lines for the second axis, just like the usual grid for the first axis. I don’t need or want to specify the ticks manually, though!

My example code just produces horizontal lines:

import pylab

datax = pylab.arange(50)

data1 = pylab.sin(datax)*1.5

data2 = datax**2

pylab.close(‘all’)

fig = pylab.figure()

ax1 = fig.add_subplot(111)

ax2 = ax1.twinx()

ax1.plot(datax, data1, ‘x’)

ax2.plot(datax, data2, ‘–’)

#ax1.grid()

ax2.grid()

fig.show()

Thanks in advance!

Does this get you where you want to be?

import pylab

datax = pylab.arange(50)
data1 = pylab.sin(datax)*1.5
data2 = datax**2

pylab.close('all')

fig = pylab.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()

ax1.plot(datax, data1, 'x')
ax2.plot(datax, data2, '--')

for ax in [ax1, ax2]:
ax.xaxis.grid(True, which='both') # `which` can be 'minor', 'major', or
'both'
ax.yaxis.grid(True, which='both')

fig.show()

···

On Thu, Jun 13, 2013 at 4:47 AM, Daniel Mader < danielstefanmader@...982...> wrote:

Hi,

I need a twinx() plot with horizontal and vertical grid lines for the
second axis, just like the usual grid for the first axis. I don't need or
want to specify the ticks manually, though!

My example code just produces horizontal lines:

import pylab

datax = pylab.arange(50)

data1 = pylab.sin(datax)*1.5

data2 = datax**2

pylab.close('all')

fig = pylab.figure()

ax1 = fig.add_subplot(111)

ax2 = ax1.twinx()

ax1.plot(datax, data1, 'x')

ax2.plot(datax, data2, '--')

#ax1.grid()

ax2.grid()

fig.show()

Thanks in advance!

Hi Paul,

I’ve modified your suggestion a little, since I don’t want a grid for the primary axis at all – unfortunately to no avail, i.e. no grid line at all:

import numpy

import matplotlib

matplotlib.use(‘agg’)

import matplotlib.pyplot as plt

datax = numpy.arange(50)

data1 = numpy.sin(datax)*1.5

data2 = datax**2

plt.close(‘all’)

fig = plt.figure()

ax1 = fig.add_subplot(111)

ax2 = ax1.twinx()

ax1.plot(datax, data1, ‘x’)

ax2.plot(datax, data2, ‘–’)

#for ax in [ax1, ax2]:

# ax.yaxis.grid(True, which=‘both’)

ax2.xaxis.grid(True, which=‘both’)

ax2.yaxis.grid(True, which=‘both’)

fig.savefig(‘twinxgrid.png’)

#fig.show()

Try throwing this in there:

ax1.xaxis.grid(False, which='both')

ax1.yaxis.grid(False, which='both')

···

On Thu, Jun 13, 2013 at 1:33 PM, Daniel Mader < danielstefanmader@...982...> wrote:

Hi Paul,

I've modified your suggestion a little, since I don't want a grid for the
primary axis at all -- unfortunately to no avail, i.e. no grid line at all:

import numpy

import matplotlib

matplotlib.use('agg')

import matplotlib.pyplot as plt

datax = numpy.arange(50)

data1 = numpy.sin(datax)*1.5

data2 = datax**2

plt.close('all')

fig = plt.figure()

ax1 = fig.add_subplot(111)

ax2 = ax1.twinx()

ax1.plot(datax, data1, 'x')

ax2.plot(datax, data2, '--')

#for ax in [ax1, ax2]:

# ax.xaxis.grid(True, which='both') # `which` can be 'minor', 'major', or
'both'

# ax.yaxis.grid(True, which='both')

ax2.xaxis.grid(True, which='both')

ax2.yaxis.grid(True, which='both')

fig.savefig('twinxgrid.png')

#fig.show()

Hi Paul,

thanks for your efforts, I’ve figured it out by myself by now, with you pieces of code:

ax1.grid()
ax2.grid()
ax1.xaxis.grid(False)

does the trick

ah, should have been:
ax1.grid()
ax2.grid()
ax1.yaxis.grid(False)