如何使用 Matplotlib 作聯合雙變數分佈圖?


要在 matplotlib 中繪製聯合雙變數分佈圖,我們可以使用 scatter 方法。

步驟

  • 設定圖形大小並調整子圖之間的填充。

  • 使用 numpy 建立 x 和 y 資料點。

  • 建立一個圖表和一組子圖。

  • 使用 scatter() 方法繪製 x 和 y。

  • 要顯示圖表,請使用 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 = 2 * np.random.randn(5000)
y = x + np.random.randn(5000)

fig, ax = plt.subplots()
_ = ax.scatter(x, y, alpha=0.08, cmap="copper", c=x)

plt.show()

輸出

更新時間:03-06-2021

229 次瀏覽

開啟您的 職業生涯

完成課程獲得認證

立即開始
廣告
© . All rights reserved.