Python中的 Matplotlib.pyplot.autoscale()
Matplotlib是Python中一个广泛使用的库,用于绘制各种图形,因为它还为复杂的绘图提供了非常有效的方法和易于理解的方法。 matplotlib.pyplot是Python编程语言中用于 2D 图形的绘图库。 Pyplot 是使 matplotlib 像 MATLAB 一样工作的命令样式函数的集合。
自动缩放()函数
matplotlib.pyplot.autoscale()是一种简单的轴视图自动缩放的方法。它打开或关闭自动缩放,然后,如果任一轴的自动缩放打开,它将在指定的一个或多个轴上执行自动缩放。
Syntax: matplotlib.pyplot.autoscale(enable=True, axis=’both’, tight=None)
Parameters:
enable is a Boolean valued parameter, if it is set to True the autoscaling is on else auto-scaling is off. It is an optional parameter. When not specified, it takes the default value as ‘true’.
axis is another optional parameter which states that axis to operate on. Usually it can be ‘both’, ‘x’, or ‘y’. When not specified, it takes default value as ‘both’.
tight takes the boolean argument, If True, first set the margins to zero. Then, this argument is forwarded to autoscale_view (regardless of its value). This is also an optional parameter of the autoscale method.
示例 1:
# importing the required module
import matplotlib.pyplot as plt
# x axis values
x = [1,2,3]
# corresponding y axis values
y = [2,4,1]
# plotting the points
plt.plot(x, y)
# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')
plt.autoscale()
plt.show()
输出:
示例 2:
import numpy as np
import matplotlib.pyplot as plt
x, y = np.arange(0, 101, 1), 300 - 0.1 * np.arange(0, 101, 1)
mask = (x >= 50) & (x <= 100)
fig, ax = plt.subplots()
ax.scatter(x[mask], y[mask])
plt.autoscale()
plt.show()
输出: