Note

Go to the endto download the full example code.

Inverted axis#

This example demonstrates two ways to invert the direction of an axis:

  • If you want to setexplicit axis limits anyway, e.g. viaset_xlim, youcan swap the limit values:set_xlim(4,0) instead ofset_xlim(0,4).

  • UseAxis.set_inverted if you only want to invert the axiswithout modifyingthe limits, i.e. keep existing limits or existing autoscaling behavior.

importmatplotlib.pyplotaspltimportnumpyasnpx=np.arange(0.01,4.0,0.01)y=np.exp(-x)fig,(ax1,ax2)=plt.subplots(1,2,figsize=(6.4,4),layout="constrained")fig.suptitle('Inverted axis with ...')ax1.plot(x,y)ax1.set_xlim(4,0)# inverted fixed limitsax1.set_title('fixed limits: set_xlim(4, 0)')ax1.set_xlabel('decreasing x ⟶')ax1.grid(True)ax2.plot(x,y)ax2.xaxis.set_inverted(True)# inverted axis with autoscalingax2.set_title('autoscaling: set_inverted(True)')ax2.set_xlabel('decreasing x ⟶')ax2.grid(True)plt.show()
Inverted axis with ..., fixed limits: set_xlim(4, 0), autoscaling: set_inverted(True)

Tags:component: axisplot-type: linelevel: beginner

Gallery generated by Sphinx-Gallery