Add second x-axis at top of figure using Python and matplotlib.pyplot


The result is:

Add second x-axis at top of figure using Python and matplotlib.pyplot

This page shows how to add second x-axis at the top of the figure using python and matplotlib.pyplot. Using "twinx" funcion, you can add x-axis at the top of the figure.

This code is based on the following websites:

See also:

Python Matplotlib Tips: Add second x-axis below first x-axis using Python and matplotlib.pyplot

This page shows how to draw second x-axis below the first x-axis. Using "twinx", "set_ticks_position", and "set_label_position", and "spines['bottom'].set_position", you can move the second x-axis from the top of the figure to below the first x-axis.


In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
In [2]:
# Data to plot
xx = np.linspace(0,100,50)
yy = (xx/10)**2

# Plot the data
ax1 = plt.subplot(1,1,1)
ax1.plot(xx,yy)
ax1.set_ylabel(r'ylabel')
ax1.set_xlabel(u'Temperature [\u2103]')

# Set scond x-axis
ax2 = ax1.twiny()

# Decide the ticklabel position in the new x-axis,
# then convert them to the position in the old x-axis
newlabel = [273.15,290,310,330,350,373.15] # labels of the xticklabels: the position in the new x-axis
k2degc = lambda t: t-273.15 # convert function: from Kelvin to Degree Celsius
newpos   = [k2degc(t) for t in newlabel]   # position of the xticklabels in the old x-axis
ax2.set_xticks(newpos)
ax2.set_xticklabels(newlabel)
ax2.set_xlabel('Temperature [K]')
ax2.set_xlim(ax1.get_xlim())

# Save the figure
plt.savefig('two_xticks.png', bbox_inches='tight', pad_inches=0.02, dpi=150)