English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Matplotlib Setting Limits

Matplotlib automatically reaches the limits along the x, y (and3. In the case of D graph, it is the minimum and maximum values of the variable displayed on the z-axis) axis. However, you can explicitly set the limits using the set_xlim() and set_ylim() functions.

In the following figure, the automatic scaling limits of the x and y axes are displayed -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
#! /usr/bin/env python
 #coding=utf-8
 import matplotlib.pyplot as plt
 import numpy as np
 import math
 plt.rcParams['font.sans-serif'] = ['SimHei']  # Step one (replace sans-serif font)
 plt.rcParams['axes.unicode_minus'] = False  # Original text from 【Lidi Huo】, commercial redistribution please contact the author for authorization, non-commercial please retain the original link:
 ax = plt.subplot(111)
 x = np.arange(1,10)
 y = np.exp(x)
 ax.plot(x, y)
 #ax.set_ylim(0,10000)
 ax.set_title('Exponential Values')
 plt.show()

Execute the above example code to get the following results -

Now format the limits on the x-axis as (0 to10) and the y-axis (0 to10000) -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
#! /usr/bin/env python
 #coding=utf-8
 import matplotlib.pyplot as plt
 import numpy as np
 import math
 plt.rcParams['font.sans-serif'] = ['SimHei']  # Step one (replace sans-serif font)
 plt.rcParams['axes.unicode_minus'] = False  # Original text from 【Lidi Huo】, commercial redistribution please contact the author for authorization, non-commercial please retain the original link:
 ax = plt.subplot(111)
 x = np.arange(1,10)
 y = np.exp(x)
 ax.plot(x, y)
 ax.set_xlim(0,10)
 ax.set_ylim(0,10000)
 ax.set_title('Exponential Values')
 plt.show()

Execute the above example code to get the following results -