2 x-axes

Neal Becker wrote:

I have a semilog plot. I'd like to add a second x axis (maybe below the
existing one, or else maybe on top of graph). This second x axis is simply
describing the same existing data, in different units.

For example imagine a plot of

x - time in seconds
y - velocity

x2 - time in minutes

This almost works:
    fig = plt.figure()
    ax = fig.add_subplot(111)
...
    ax2 = ax.twiny()
    min_x, max_x = ax.get_xlim()
    ax2.set_xlim (min_x-1, max_x-1)

except the 2nd x axis is on the top, and prints right on top of the title

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.

···

On Fri, Sep 9, 2011 at 12:50 PM, Neal Becker <ndbecker2@…287…> wrote:

Neal Becker wrote:

I have a semilog plot. I’d like to add a second x axis (maybe below the

existing one, or else maybe on top of graph). This second x axis is simply

describing the same existing data, in different units.

For example imagine a plot of

x - time in seconds

y - velocity

x2 - time in minutes

This almost works:

fig = plt.figure()

ax = fig.add_subplot(111)

ax2 = ax.twiny()

min_x, max_x = ax.get_xlim()

ax2.set_xlim (min_x-1, max_x-1)

except the 2nd x axis is on the top, and prints right on top of the title


Why Cloud-Based Security and Archiving Make Sense

Osterman Research conducted this study that outlines how and why cloud

computing security and archiving is rapidly being adopted across the IT

space for its ease of implementation, lower cost, and increased

reliability. Learn more. http://www.accelacomm.com/jaw/sfnl/114/51425301/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Gökhan

Just in case, here is a version with "axes_grid1" toolkit. Note that
axes_grid is kind of deprecated.
Regards,

-JJ

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axes_grid1 as axes_grid1

host = axes_grid1.host_subplot(111)
hplt, = host.plot(np.random.rand(100))

from matplotlib.transforms import Affine2D
transfrom_from_parx_to_host = Affine2D().scale(1000, 1)
parx = host.twin(transfrom_from_parx_to_host)

if 1: # adjust axis postion etc.
    parx.axis["right"].toggle(ticklabels=False)
    parx.axis["top"].toggle(ticklabels=False)
    parx.axis["bottom"].toggle(ticklabels=True)

    parx.axis["bottom"].line.set_visible(True)
    parx.spines["bottom"].set_position(('outward',20))

plt.show()

···

On Sat, Sep 10, 2011 at 6:14 AM, Gökhan Sever <gokhansever@...287...> 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.
On Fri, Sep 9, 2011 at 12:50 PM, Neal Becker <ndbecker2@...287...> wrote:

Neal Becker wrote:

> I have a semilog plot. I'd like to add a second x axis (maybe below the
> existing one, or else maybe on top of graph). This second x axis is
> simply
> describing the same existing data, in different units.
>
> For example imagine a plot of
>
> x - time in seconds
> y - velocity
>
> x2 - time in minutes
>
>

This almost works:
fig = plt.figure()
ax = fig.add_subplot(111)
...
ax2 = ax.twiny()
min_x, max_x = ax.get_xlim()
ax2.set_xlim (min_x-1, max_x-1)

except the 2nd x axis is on the top, and prints right on top of the title

------------------------------------------------------------------------------
Why Cloud-Based Security and Archiving Make Sense
Osterman Research conducted this study that outlines how and why cloud
computing security and archiving is rapidly being adopted across the IT
space for its ease of implementation, lower cost, and increased
reliability. Learn more. http://www.accelacomm.com/jaw/sfnl/114/51425301/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

--
Gökhan

------------------------------------------------------------------------------
Why Cloud-Based Security and Archiving Make Sense
Osterman Research conducted this study that outlines how and why cloud
computing security and archiving is rapidly being adopted across the IT
space for its ease of implementation, lower cost, and increased
reliability. Learn more. http://www.accelacomm.com/jaw/sfnl/114/51425301/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users