Python中的 Matplotlib.axes.Axes.set_ylim()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.set_ylim()函数:
matplotlib 库的 axes 模块中的Axes.set_ylim()函数用于设置 y 轴视图范围。
Syntax: Axes.set_ylim(self, bottom=None, top=None, emit=True, auto=False, *, ymin=None, ymax=None)
Parameters: This method accepts the following parameters.
- bottom : This parameter is the bottom ylim in data coordinates
- top : This parameter is the top ylim in data coordinates
- emit : This parameter is used to notify observers of limit change.
- auto : This parameter is used to turn on autoscaling of the x-axis.
- ymin, ymax: These parameter are equivalent to bottom and top and it is an error to pass both ymin and bottom or ymax and top.
Returns:This method returns the following
- bottom, top:This returns the new y-axis limits in data coordinates.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.set_ylim()函数:
示例 1:
# Implementation of matplotlib function
from matplotlib.widgets import Cursor
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(19680801)
fig, ax = plt.subplots(facecolor ='# A0F0CC')
x, y = 4*(np.random.rand(2, 100) - .5)
ax.plot(x, y, 'g')
ax.set_ylim(-3, 3)
cursor = Cursor(ax, useblit = True, color ='red',
linewidth = 2)
ax.set_title('matplotlib.axes.Axes.set_ylim() Example\n',
fontsize = 14, fontweight ='bold')
plt.show()
输出:
示例 2:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
fig1, ax1 = plt.subplots()
fig2, ax2 = plt.subplots()
ax1.set(xlim =(-0.5, 1.5), ylim =(-0.5, 1.5),
autoscale_on = False)
ax2.set(xlim =(0.5, 0.75), ylim =(0.5, 0.75),
autoscale_on = False)
x, y, s, c = np.random.rand(4, 200)
s *= 200
ax1.scatter(x, y, s, c)
ax2.scatter(x, y, s, c)
def GFG(event):
if event.button != 1:
return
x, y = event.xdata, event.ydata
ax2.set_xlim(x - 0.1, x + 0.1)
ax2.set_ylim(y - 0.1, y + 0.1)
fig2.canvas.draw()
fig1.canvas.mpl_connect('button_press_event', GFG)
ax1.set_title('matplotlib.axes.Axes.set_ylim()\
Example\n Original Window ',
fontsize = 14, fontweight ='bold')
ax2.set_title('Zoomed window ',
fontsize = 14, fontweight ='bold')
plt.show()
输出: