📌  相关文章
📜  Python中的 Matplotlib.axes.Axes.set_xmargin()

📅  最后修改于: 2022-05-13 01:54:31.803000             🧑  作者: Mango

Python中的 Matplotlib.axes.Axes.set_xmargin()

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

matplotlib.axes.Axes.set_xmargin()函数

matplotlib 库的 axes 模块中的Axes.set_xmargin()函数用于在自动缩放之前设置 X 数据限制的填充。

下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.set_xmargin()函数:

示例 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.set_xmargin(0.5)
   
ax.set_title("Without set_xmargin() Function")
ax1.set_title("With set_xmargin value = 0.5")
   
fig.suptitle('matplotlib.axes.Axes.set_xmargin() \
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, 3, 1)
t1 = np.cos(np.pi * t) + np.sin(np.pi * t)
  
fig, [ax1, ax2, ax3] = plt.subplots(nrows = 3)
ax1.plot(t1, color ="green")
ax1.text(0.75, 0.65, 'Original window')
  
ax2.set_xmargin(2)
ax2.plot(t1, color ="green")
ax2.text(3, 0, 'Zoomed out')
  
ax3.set_xmargin(-0.45)
ax3.plot(t1, color ="green")
ax3.text(0.98, 0.45, 'Zoomed in')
  
fig.suptitle('matplotlib.axes.Axes.set_xmargin() \
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, 3, 1)
t1 = np.cos(np.pi * t) + np.sin(np.pi * t)
  
fig, [ax1, ax2, ax3] = plt.subplots(nrows = 3)
ax1.plot(t1, color ="green")
ax1.text(0.75, 0.65, 'Original window')
  
ax2.set_xmargin(2)
ax2.plot(t1, color ="green")
ax2.text(3, 0, 'Zoomed out')
  
ax3.set_xmargin(-0.45)
ax3.plot(t1, color ="green")
ax3.text(0.98, 0.45, 'Zoomed in')
  
fig.suptitle('matplotlib.axes.Axes.set_xmargin() \
function Example\n', fontweight ="bold")
fig.canvas.draw()
plt.show()

输出: