如何在 Matplotlib 中自定义 X 轴?


若要自定义 X 轴标签,我们可以采取以下步骤 -

  • 设置图形尺寸并调整子图之间和周围的填充。
  • 初始化变量 N,以获取样本数据的数量。
  • 使用 numpy 创建xy数据点
  • 使用plot()方法绘制xy数据点。
  • 使用fontweight、color、fontsizealignment自定义 X 轴标签。
  • 若要显示图形,请使用show()方法。

示例

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

N = 100
x = np.random.rand(N)
y = np.random.rand(N)

plt.plot(x, y, 'r*')

plt.xlabel('X-axis Label', fontweight='bold', color='orange',
   fontsize='17', horizontalalignment='center')

plt.show()

输出

更新于: 10-6-2021

3000+ 次浏览

启动您的职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.