Python中的 Matplotlib.pyplot.subplots_adjust()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Pyplot是Matplotlib模块的基于状态的接口,它提供了一个类似 MATLAB 的接口。在 Pyplot 中可以使用各种图,包括线图、等高线图、直方图、散点图、3D 图等。
matplotlib.pyplot.subplots_adjust()函数
matplotlib 库的 pyplot 模块中的 subplots_adjust ()函数用于调整子图布局。
Syntax: matplotlib.pyplot.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)
Parameters: This method accept the following parameters that are described below:
- left : This parameter is the left side of the subplots of the figure.
- right : This parameter is the right side of the subplots of the figure.
- bottom : This parameter is the bottom of the subplots of the figure.
- top : This parameter is the top of the subplots of the figure.
- wspace : This parameter is the amount of width reserved for space between subplots expressed as a fraction of the average axis width.
- hspace : This parameter is the amount of height reserved for space between subplots expressed as a fraction of the average axis height.
下面的示例说明了 matplotlib.pyplot 中的 matplotlib.pyplot.subplots_adjust()函数:
示例 1:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
x = [1, 12, 3, 9]
y = [1, 4, 9, 16]
labels = ['Geeks1', 'Geeks2', 'Geeks3', 'Geeks4']
plt.plot(x, y)
plt.xticks(x, labels, rotation ='vertical')
plt.margins(0.2)
plt.subplots_adjust(bottom = 0.15)
plt.title('matplotlib.pyplot.subplots_adjust() Example')
plt.show()
输出:
示例 2:
# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import TextBox
fig, ax = plt.subplots()
plt.subplots_adjust(bottom = 0.2)
t = np.arange(-2.0, 2.0, 0.001)
s = np.sin(t)+np.cos(2 * t)
initial_text = "sin(t) + cos(2t)"
l, = plt.plot(t, s, lw = 2)
def submit(text):
ydata = eval(text)
l.set_ydata(ydata)
ax.set_ylim(np.min(ydata), np.max(ydata))
plt.draw()
axbox = plt.axes([0.4, 0.05, 0.3, 0.075])
text_box = TextBox(axbox, 'Formula Used : ',
initial = initial_text)
text_box.on_submit(submit)
fig.suptitle('matplotlib.pyplot.subplots_adjust() Example')
plt.show()
输出: