Amritbir Singh Gill - 1 year ago 145
Python Question

# How to add second x-axis at the bottom of the first one in matplotlib.?

In this example the users have solved the second axis problem by adding it to the upper part of the graph where it coincide with the title.

Question:
Is it possible to add the second x-axis at the bottom of the first one.?

Code:

``````import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax2 = ax1.twiny()

X = np.linspace(0,1,1000)
Y = np.cos(X*20)

ax1.plot(X,Y)
ax1.set_xlabel(r"Original x-axis: \$X\$")

new_tick_locations = np.array([.2, .5, .9])

def tick_function(X):
V = 1/(1+X)
return ["%.3f" % z for z in V]

ax2.set_xticks(new_tick_locations)
ax2.set_xticklabels(tick_function(new_tick_locations))
ax2.set_xlabel(r"Modified x-axis: \$1/(1+X)\$")
plt.show()
``````

Any help will be appreciated.!

As an alternative to the answer from @DizietAsahi, you can use `spines` in a similar way to the `matplotlib` example posted here.

``````import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax2 = ax1.twiny()

# Add some extra space for the second axis at the bottom

X = np.linspace(0,1,1000)
Y = np.cos(X*20)

ax1.plot(X,Y)
ax1.set_xlabel(r"Original x-axis: \$X\$")

new_tick_locations = np.array([.2, .5, .9])

def tick_function(X):
V = 1/(1+X)
return ["%.3f" % z for z in V]

# Move twinned axis ticks and label from top to bottom
ax2.xaxis.set_ticks_position("bottom")
ax2.xaxis.set_label_position("bottom")

# Offset the twin axis below the host
ax2.spines["bottom"].set_position(("axes", -0.15))

# Turn on the frame for the twin axis, but then hide all
# but the bottom spine
ax2.set_frame_on(True)
ax2.patch.set_visible(False)
for sp in ax2.spines.itervalues():
sp.set_visible(False)
ax2.spines["bottom"].set_visible(True)

ax2.set_xticks(new_tick_locations)
ax2.set_xticklabels(tick_function(new_tick_locations))
ax2.set_xlabel(r"Modified x-axis: \$1/(1+X)\$")
plt.show()
``````

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download