📜  如何在 Matplotlib 中设置轴范围?

📅  最后修改于: 2022-05-13 01:55:41.920000             🧑  作者: Mango

如何在 Matplotlib 中设置轴范围?

Matplotlib 通过在该轴上查找极值(即最小值和最大值)来设置轴的默认范围。但是,为了更好地查看数据,有时会使用 pyplot 模块根据 Matplotlib 中的要求设置图形的轴范围。以下是在 Matplotlib 中设置轴范围的方法:

句法:

For setting range of x-axis:
 matplotlib.pyplot.xlim(limit_range)
For setting range of y-axis: 
 matplotlib.pyplot.ylim(limit_range)

示例 1:

让我们在不设置轴范围的情况下绘制正弦波函数:

Python
# import packages
import matplotlib.pyplot as plt
import numpy as np
  
# return values between 0 and 10 with 
# even space of 0.1
x = np.arange(0, 10, 0.1)
  
# generate value of sine function for 
# given x values
y = np.sin(x)
  
# plot graph of sine function
plt.plot(y, color='blue')
  
# display plot
plt.show()


Python
# import packages
import matplotlib.pyplot as plt
import numpy as np
  
# return values between 0 and 10 with 
# even space of 0.1
x = np.arange(0, 10, 0.1)
  
# generate value of sine function for 
# given x values
y = np.sin(x)
  
# plot graph of sine function
plt.plot(y, color='blue')
  
# Set the range of x-axis
plt.xlim(0, 60)
  
# display plot
plt.show()


Python
# import packages
import matplotlib.pyplot as plt
import numpy as np
  
# return values between 0 and 10 with 
# even space of 0.1
x = np.arange(0, 10, 0.1)
  
# generate value of sine function for 
# given x values
y = np.sin(x)
  
# plot graph of sine function
plt.plot(y, color='blue')
  
# Set the range of y-axis
plt.ylim(0, 1)
  
# display plot
plt.show()


Python
# import packages
import matplotlib.pyplot as plt
import numpy as np
  
# return values between 0 and 10 with 
# even space of 0.1
x = np.arange(0, 10, 0.1)
  
# generate value of sine function for
# given x values
y = np.sin(x)
  
# plot graph of sine function
plt.plot(y, color='blue')
  
# Set the range of x-axis
plt.xlim(0, 32)
# Set the range of y-axis
plt.ylim(0, 1)
  
# display plot
plt.show()


输出:

现在,我们将绘图的 x 轴范围设置为 [0, 60]。以下是限制x轴范围的代码:

示例 2:

Python

# import packages
import matplotlib.pyplot as plt
import numpy as np
  
# return values between 0 and 10 with 
# even space of 0.1
x = np.arange(0, 10, 0.1)
  
# generate value of sine function for 
# given x values
y = np.sin(x)
  
# plot graph of sine function
plt.plot(y, color='blue')
  
# Set the range of x-axis
plt.xlim(0, 60)
  
# display plot
plt.show()

输出:

现在,我们将绘图的 y 轴范围设置为 [0, 1]。以下是限制y轴范围的代码:

示例 3:

Python

# import packages
import matplotlib.pyplot as plt
import numpy as np
  
# return values between 0 and 10 with 
# even space of 0.1
x = np.arange(0, 10, 0.1)
  
# generate value of sine function for 
# given x values
y = np.sin(x)
  
# plot graph of sine function
plt.plot(y, color='blue')
  
# Set the range of y-axis
plt.ylim(0, 1)
  
# display plot
plt.show()

输出:

我们还可以同时为绘图的两个轴设置范围。现在,我们将 x 轴范围设置为 [0, 32],y 轴范围设置为 [0, 1]。以下是限制x轴和y轴范围的代码:

示例 4:

Python

# import packages
import matplotlib.pyplot as plt
import numpy as np
  
# return values between 0 and 10 with 
# even space of 0.1
x = np.arange(0, 10, 0.1)
  
# generate value of sine function for
# given x values
y = np.sin(x)
  
# plot graph of sine function
plt.plot(y, color='blue')
  
# Set the range of x-axis
plt.xlim(0, 32)
# Set the range of y-axis
plt.ylim(0, 1)
  
# display plot
plt.show()

输出: