Python中的 Matplotlib.axes.Axes.margins()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.margins()函数
matplotlib 库的 axes 模块中的Axes.margins()函数用于设置或检索自动缩放边距。
Syntax: Axes.margins(self, *margins, x=None, y=None, tight=True)
Parameters: This method accepts the following parameters.
- *margins: This parameter is used to specify both margins of the x-axis and y-axis limits.
- x, y: These parameter is used to specific margin values for the x-axis and y-axis, respectively.
- tight : This parameter is passed to autoscale_view(), which is executed after a margin is changed.
Return value: The following values are returned by this method.
- xmargin
- ymargin
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.margins()函数:
示例 1:
Python3
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
fig, (ax, ax1) = plt.subplots(1, 2)
plt.subplots_adjust(bottom = 0.25)
t = np.arange(0.0, 1.0, 0.001)
a0 = 5
f0 = 3
delta_f = 5.0
s = a0 * np.sin(2 * np.pi * f0 * t)
ax.plot(t, s, lw = 2, color = 'green')
ax1.plot(t, s, lw = 2, color = 'green')
ax1.margins(0.5)
ax.set_title("Without margin() Function")
ax1.set_title("With margin value = 0")
fig.suptitle('matplotlib.axes.Axes.margins() function \
Example\n', fontweight ="bold")
fig.canvas.draw()
plt.show()
Python3
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0.0, 3.0, 0.01)
t1 = np.exp(-t) * np.cos(2 * np.pi * t)
fig, [ax1, ax2, ax3] = plt.subplots(nrows = 3)
ax1.plot(t, t1, color ="green")
ax1.text(1.1, 0.65, 'Original window')
ax2.margins(2, 2)
ax2.plot(t, t1, color ="green")
ax2.text(0, 2.0, 'Zoomed out')
ax3.margins(x = 0, y =-0.25)
ax3.plot(t, t1, color ="green")
ax3.text(1.2, 0.35, 'Zoomed in')
fig.suptitle('matplotlib.axes.Axes.margins() function\
Example\n', fontweight ="bold")
fig.canvas.draw()
plt.show()
输出:
示例 2:
Python3
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0.0, 3.0, 0.01)
t1 = np.exp(-t) * np.cos(2 * np.pi * t)
fig, [ax1, ax2, ax3] = plt.subplots(nrows = 3)
ax1.plot(t, t1, color ="green")
ax1.text(1.1, 0.65, 'Original window')
ax2.margins(2, 2)
ax2.plot(t, t1, color ="green")
ax2.text(0, 2.0, 'Zoomed out')
ax3.margins(x = 0, y =-0.25)
ax3.plot(t, t1, color ="green")
ax3.text(1.2, 0.35, 'Zoomed in')
fig.suptitle('matplotlib.axes.Axes.margins() function\
Example\n', fontweight ="bold")
fig.canvas.draw()
plt.show()
输出: