如何在 Matplotlib 中自定义 X 轴刻度?

matplotlibpythondata visualization

要在 Matplotlib 中自定义 X 轴刻度,我们可以更改刻度的长度和宽度。

步骤

  • 设置图形大小并调整子图之间和周围的填充。
  • heightbarsy_pos 数据点创建列表。
  • 使用 bar() 方法制作条形图。
  • 要自定义 X 轴刻度,我们可以使用 tick_params() 方法,其中 color=red、direction=outward、length=7width=2
  • 要显示图形,请使用 show()方法。

示例

import numpy as np
import matplotlib.pyplot as plt

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

height = [3, 12, 5, 18, 45]
bars = ('A', 'B', 'C', 'D', 'E')
y_pos = np.arange(len(bars))

plt.bar(y_pos, height, color='yellow')
plt.tick_params(axis='x', colors='red', direction='out', length=7, width=2)

plt.show()

输出


相关文章