增加 Matplotlib 中 X 轴标签间距


要增加 Matplotlib 中 X 轴标签的间距,我们可以在subplots_adjust()方法的参数中使用间距变量。

步骤

  • 设置图形大小,并调整子图之间以及周围的填充。

  • 创建新图形或者使用 figure() 方法激活现有的图形。

  • 使用 numpy 创建 x 和 y 数据点。

  • 使用 plot() 方法绘制 x 和 y。

  • 使用LaTex表达式,使用 xlabel() 方法放置xlabel

  • 使用 subplots_adjust() 方法增加或减少 X 轴标签的间距

  • 要显示图形,请使用 show() 方法。

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
x = np.linspace(-2, 2, 10)
y = np.exp(x)
plt.plot(x, y)
plt.xlabel("$\bf{y=e^{x}}$")
spacing = 0.100
fig.subplots_adjust(bottom=spacing)
plt.show()

输出

更新时间: 01-Jun-2021

23K+ 浏览

开启您的 职业生涯

完成课程即可获得认证

开始学习
广告
© . All rights reserved.