如何在 Matplotlib 中繪出真/假或啟用/停用資料?


要在 Matplotlib 中繪製真/假或啟用/停用資料,我們可以採取以下步驟 −

  • 設定圖形尺寸並調整子圖之間和周圍的內邊距。
  • 使用 True 或 False 透過 numpy 建立資料。
  • 使用 figure() 方法建立一個新圖形或啟用現有圖形。
  • '~.axes.Axes' 作為 subplot 佈局的一部分新增到 figure。
  • 使用 imshow() 方法將資料顯示為影像,即在 2D 規則光柵上顯示。
  • 要顯示圖形,請使用 show() 方法。

示例

import matplotlib.pyplot as plt
import numpy as np

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

data = np.random.random((20, 20)) > 0.5
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(data, aspect='auto', cmap="copper", interpolation='nearest')

plt.show()

輸出

更新於: 2021 年 6 月 15 日

2K+ 瀏覽

開啟你的事業

完成該課程以獲得認證

開始
廣告
© . All rights reserved.