Matplotlib 繪圖中的等值線陰影


為了繪製具有陰影的輪廓線,我們可以按照如下步驟進行操作 −

  • 設定圖形大小並調整子圖之間和周圍的邊距。
  • 使用 numpy 建立 xyz 資料點。
  • 使 xy 資料點扁平化。
  • 建立一個圖形和一組子圖。
  • 繪製具有不同陰影的輪廓線。
  • 為標量可對映例項建立一個顏色條。
  • 要顯示圖形,請使用 show() 方法。

例項

import matplotlib.pyplot as plt
import numpy as np

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

x = np.linspace(-3, 5, 150).reshape(1, -1)
y = np.linspace(-3, 5, 120).reshape(-1, 1)
z = np.cos(x) + np.sin(y)

x, y = x.flatten(), y.flatten()

fig1, ax1 = plt.subplots()

cs = ax1.contourf(x, y, z, hatches=['-', '/', '\', '//'],
                  cmap='gray', extend='both', alpha=0.5)
fig1.colorbar(cs)

plt.show()

輸出

更新於: 2021 年 6 月 15 日

664 次檢視

職業生涯的開端

完成課程以獲得認證

開始
廣告
© . All rights reserved.