Home

Sunday, December 25, 2016

Set Min Max Axis Limit in Matplotlib Plot

To set the minimum maximum limit axis first we need to get axes from the plot by using plt.gca() and then use set_xlim([xmin,xmax]) to set min max of x-axis and set_ylim([ymin,ymax]) to set min max of y-axis as follow.

plt.gca() function is abbreviation from get current axes which return the class object of matplotlib.axes.Axes


import numpy as np
import matplotlib.pyplot as plt

plt.plot(x,y)

axes = plt.gca()
axes.set_xlim([xmin,xmax])
axes.set_ylim([ymin,ymax])

plt.show()