Matplotlib.axes.Axes.margins() - 设置或检索自动缩放的边距

Matplotlib.axes.Axes.margins()

Matplotlib是Python中的一个库,它是NumPy库的数值-数学扩展。Axes包含了大多数图形元素:Axis、Tick、Line2D、Text、Polygon等,并设置坐标系。Axes的实例通过callbacks属性支持回调。

Matplotlib.axes.Axes.margins()函数

matplotlib库的Axes模块中的Axes.margins()函数用于设置或检索自动缩放的边距。

语法:

Axes.margin (self, *margin, x=None,y=None,tight=True)

参数:该方法接受以下参数。

  • *margin:该参数用于指定x轴和y轴的边界。
  • x, y:这些参数分别用于x轴和y轴的特定裕度值。
  • tight:该参数传递给autoscale_view(),在修改空白后执行。

返回值:该方法返回以下值。

  • xmargin
  • ymargin

下面的例子演示了matplotlib.axes.axes. margin()函数在matplotlib.axes中的作用:

示例1

# 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()

输出:

Matplotlib.axes.Axes.margins()

示例2

# 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()

输出:

Matplotlib.axes.Axes.margins()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程