2 x-axes

Gökhan Sever wrote:

Hi,

The code below should create a properly placed 2nd x-axis. You might need to
adjust the placement of the figure canvas to match into the window.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid.parasite_axes import SubplotHost

fig = plt.figure(figsize=(10,8))
host = SubplotHost(fig, 111)
fig.add_subplot(host)
parx = host.twiny()

parx.axis["top"].set_visible(False)
offset = 0, -50
new_axisline = parx.get_grid_helper().new_fixed_axis
parx.axis["bottom"] = new_axisline(loc="bottom", axes=parx, offset=offset)
parx.axis["bottom"].label.set_visible(True)

hplt, = host.plot(np.random.rand(100))
p2, = parx.plot(np.linspace(0,20,100), np.random.rand(100)*5.0,
color='green')

plt.show()

There is also another example at:

http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/users/overview.html#axisartist-
with-parasiteaxes

Hope this helps.

Yes, that's very helpful. Just one thing. How would I get a bit more bottom
margin on the main figure to leave more room for the extra axis?

I'm using this as an example. I experimented with plt.subplots_adjust, which
seems like it might do the right thing. Is this the 'best' approach?
(I really don't know what all these methods do, just guessing)

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid.parasite_axes import SubplotHost

from matplotlib.backends.backend_pdf import PdfPages

pdf = PdfPages('results.pdf')

fig = plt.figure(figsize=(10,8))
host = SubplotHost(fig, 111)
ax = fig.add_subplot(host)
plt.subplots_adjust (bottom=0.1)
parx = host.twiny()

parx.axis["top"].set_visible(False)
offset = 0, -30
new_axisline = parx.get_grid_helper().new_fixed_axis
parx.axis["bottom"] = new_axisline(loc="bottom", axes=parx, offset=offset)
parx.axis["bottom"].label.set_visible(True)

hplt, = host.plot(np.linspace(0,20,100), np.random.rand(100))
plt.xlabel ('Es/No')
p2, = parx.plot(np.linspace(0,20,100)-5, np.random.rand(100)*5.0, color='green')
parx.set_xlabel ('$Eb_{i}/No$')
#plt.show()

pdf.savefig (fig)
plt.close()
pdf.close()

Yes, you need to fiddle with subplots_adjust command. The current
development branch of matplotlib (not yet released) has a new function
"tight_layout", which does this automatically for you.
Regards,

-JJ

···

On Sun, Sep 11, 2011 at 10:16 PM, Neal Becker <ndbecker2@...287...> wrote:

Yes, that's very helpful. Just one thing. How would I get a bit more bottom
margin on the main figure to leave more room for the extra axis?

I'm using this as an example. I experimented with plt.subplots_adjust, which
seems like it might do the right thing. Is this the 'best' approach?
(I really don't know what all these methods do, just guessing)