How to add a second x-axis in a Matplotlib graph in Python

Adding a second x-axis results in a Matplotlib graph that contains the top and bottom x-axis labeled sharing one labeled y-axis.

Solution for How to add a second x-axis in a Matplotlib graph in Python : You can use matplotlib.axes._base._AxesBase.twiny() to add a second x-axis in a Matplotlib graph Use matplotlib.pyplot.gca() to get the current axes of the graph. Call matplotlib.axes._base._AxesBase.twiny() on the current axes to create a twin of axes that have a shared y-axis with the current axes. Call matplotlib.axes._axes.Axes.set_xticks(ticks) with ticks as a list of floats to set the x-ticks on the twin axes. Call matplotlib.axes._axes.Axes.set_xlabel(xlabel) with xlabel as the label for the x-axis for the original axes and twin axes.


how-to-add-a-second-x-axis-in-a-matplotlib-graph-in-python