如何自動調整 matplotlib Python 中的文字?


想要自動調整 matplotlib 中的文字,我們可以執行緊密佈局並旋轉刻度線。

步驟

  • 設定圖形大小並調整子圖之間的和周圍的填充。

  • 繪製範圍為 10 的資料點。

  • 製作一個標籤列表。

  • 使用 30 度旋轉在 X 軸上放置刻度線標籤

  • 使用Show()方法顯示圖表。

示例

from matplotlib import pyplot as plt

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

plt.plot(range(10))

labels = [7 * repr(i) for i in range(10)]

plt.xticks(range(10), labels, rotation=30)

plt.show()

輸出

它會產生以下輸出 −

更新日期:2021 年 10 月 8 日

386 次瀏覽

開啟您的職業

完成此課程以獲取認證

開始
廣告
© . All rights reserved.