如何在 Matplotlib 中将 xlabel 设置在 X 轴的末尾?
matplotlibserver side programmingprogramming更新于 2025/7/4 12:22:17
要在 Matplotlib 中将 xlabel 设置在 X 轴的末尾,我们可以执行以下步骤 −
使用 numpy 为 x 创建数据点。
使用 subplot() 方法向当前图形添加子图。
使用 plot() 方法绘制 x 和 log(x)。
使用 set_label() 方法设置 X 轴上的标签,其中 fontsize=16,loc=left并且 color=red。
要设置 X 轴末端的 xlabel ,请使用坐标 x 和 y。
要显示图形,请使用 show() 方法。
示例
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(1, 2, 5)
ax = plt.subplot()
ax.plot(x, np.log(x))
ax.set_xticks(x)
label = ax.set_xlabel('X ->', fontsize=16, loc="left", c="red")
ax.xaxis.set_label_coords(1.0, -0.025)
plt.show()
输出


