如何使 matplotlib 中的標題框寬度跨越整個繪圖?


在 matplotlib 中使標題框寬度跨越整個繪圖,我們可以採取以下步驟

  • 設定圖形大小並調整子圖之間以及子圖周圍的內邊距。
  • 使用 numpy 建立 x 和 y 資料點。
  • 使用 **plot()** 方法繪製 x 和 y 資料點,其中 **color=black** 且 **linewidth=7**。
  • 使用 **gca()** 方法獲取當前軸。
  • 設定繪圖示題。
  • 使用 **get_bbox_patch()** 方法返回包圍框修補程式
  • 要顯示圖形,請使用 **show()** 方法。

示例

import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-2, 2, 100)
y = np.sin(x)
plt.plot(x, y, c='black', lw=7)
ax = plt.gca()
title = ax.set_title("y=sin(x)", backgroundcolor='red')
bb = title.get_bbox_patch()
plt.show()

輸出

更新於: 01-Jun-2021

302 次瀏覽

開啟你的 職業

透過完成課程來獲得認證

開始
廣告