如何在 Matplotlib 中自定義 X 軸刻度?


要在 Matplotlib 中自定義 X 軸刻度,我們可以更改刻度的長度和寬度。

步驟

  • 設定圖形大小並調整子圖之間和周圍的邊距。
  • 高度條形圖y 軸位置資料點建立列表。
  • 使用bar()方法繪製條形圖。
  • 要自定義 X 軸刻度,我們可以使用tick_params()方法,並使用color=red, direction=outward, length=7,width=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()

輸出

更新於: 2021 年 6 月 10 日

4K+ 瀏覽量

開啟你的 職業

完成課程獲得認證

開始
廣告
© . All rights reserved.