如何使用 Matplotlib 中的 Python 將顏色條方向設為水平?


在 Python 中,要將顏色條方向設為水平,可在引數中使用 orientation="horizontal"

步驟

  • 設定影像大小,調整子圖之間和子圖周圍的填充。
  • 使用 numpy 建立隨機 xyz 資料點。
  • 建立一個影像和一組子圖。
  • 使用 scatter() 方法繪製 xyz 資料點。
  • ScalarMappable 例項建立一個顏色條,方向為水平。
  • 要顯示影像,請使用 show() 方法。

示例

import numpy as np
import matplotlib.pyplot as plt

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

x, y, z = np.random.rand(3, 50)
f, ax = plt.subplots()

points = ax.scatter(x, y, c=z, s=50, cmap="plasma")
f.colorbar(points, orientation="horizontal")

plt.show()

輸出

更新於: 07-Jul-2021

2K+ 瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.