Adjusting axis limits

While Matplotlib automatically chooses the range of x and y axis limits to spread data onto the whole plotting area, sometimes we want some adjustment, such as to show 100% as maximum instead of somewhere lower. To set the limits of x and y axes, we use the commands plt.xlim() and plt.ylim(). In our daily temperature example, the auto-scaling makes the temperature changes of less than 2 degrees Celsius seem very dramatic. Here is how we can adjust it, say, to show 0 degrees as the lower limit on the y axis for temperatures of the first 5 days only:

import matplotlib.pyplot as plt

d = [11,12,13,14,15,16,17]
t0 = [15.3,12.6,12.7,13.2,12.3,11.4,12.8]
t1 = [26.1,26.2,24.3,25.1,26.7,27.8,26.9]
t2 = [22.3,20.6,19.8,21.6,21.3,19.4,21.4]

plt.plot(d,t0)
plt.plot(d,t1)
plt.plot(d,t2)

# Set the limit for each axis
plt.xlim(11,15)
plt.ylim(0,30)

plt.show()

The preceding code produces a plot with the y axis ranging from 0 to 30, as follows:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.37.5