更改 Matplotlib 圖表預設背景顏色
若要更改 Matplotlib 圖表的預設背景顏色,我們可以採取以下步驟:
步驟
- 設定圖形大小並調整子圖之間的填充。
- 獲取當前座標軸。
- 使用 nrows=1, ncols=2 和 index=1 向當前圖形新增一個子圖。
- 使用 plots() 方法繪製隨機 x 和 y 資料點。
- 設定子圖的標題。
- 使用 nrows=1, ncols=2 和 index=2 向當前圖形新增一個子圖。
- 獲取當前座標軸。
- 設定自定義面部顏色。
- 使用 plot() 方法繪製 x 和 y 資料點。
- 設定圖表的標題。
- 若要顯示圖形,請使用 show() 方法。
示例
import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True ax = plt.gca() print("Default face color is: ", ax.get_facecolor()) plt.subplot(121) plt.plot(np.random.rand(10), np.random.rand(10)) plt.title("With default face color") plt.subplot(122) ax = plt.gca() ax.set_facecolor("orange") plt.plot(np.random.rand(10), np.random.rand(10)) plt.title("With customize face color") plt.show()
輸出
廣告