Matplotlib 简介
Matplotlib 是 Python 中的一个综合性库,它是 NumPy 库的数值-数学扩展。Axes 类 包含了图形的大部分元素,如 Axis(轴)、Tick(刻度)、Line2D(二维线)、Text(文本)、Polygon(多边形)等,并负责设置坐标系。此外,Axes 的实例支持通过 callbacks 属性进行回调操作。
在 Matplotlib 库的 axes 模块中,Axes.set_ybound() 函数用于设置 y 轴的数值下限和上限。
> 语法: Axes.set_ybound(self, lower=None, upper=None)
>
> 参数: 此方法接受以下参数。
>
> – lower, upper: 这些参数分别代表下限和上限。如果为 None,则相应的轴界限保持不变。
>
>
> 返回值: 此方法返回以下内容
>
> – lower, upper: 返回新的 y 轴下限和上限。
注意: 在多种情况下,我们可以使用此函数来替代 set_ylim。
下面的示例说明了 matplotlib.axes.Axes.set_ybound() 函数在 matplotlib.axes 中的用法:
示例 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()
x, y = 4*(np.random.rand(2, 50) - .5)
ax.plot(x, y, ‘g‘)
ax.set_ybound(-4, 4)
ax.set_title(‘matplotlib.axes.Axes.set_ybound() Example
‘,
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 OnClick(event):
if event.button != 1:
return
x, y = event.xdata, event.ydata
ax2.set_xbound(x - 0.1, x + 0.1)
ax2.set_ybound(y - 0.2, y + 0.2)
fig2.canvas.draw()
fig1.canvas.mpl_connect(‘button_press_event‘, OnClick)
ax1.set_title(‘matplotlib.axes.Axes.set_ybound()\
Example
Original Window ‘,
fontsize = 14, fontweight =‘bold‘)
ax2.set_title(‘Zoomed window ‘,
fontsize = 14, fontweight =‘bold‘)
plt.show()
输出: