如何在 matplotlib.pyplot 中將 colorbar 與 hist2d 一起使用?
要在 matplotlib.pyplot 中將 colorbar 與 hist2d 一起使用,我們可以按以下步驟進行。
步驟
設定圖形大小,調整子圖之間和周圍的邊距。
初始化變數 “N”,表示樣本資料個數。
使用 numpy 建立 x 和 y 資料點。
使用 subplots() 方法建立圖表和一組子圖。
使用 hist2D() 繪製二維直方圖。
為 hist2d 標量對映例項建立 colorbar。
要顯示圖表,請使用 Show() 方法。
示例
from matplotlib.colors import LogNorm import matplotlib.pyplot as plt import numpy as np # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Number of sample data N = 1000 # Create x and y data points x = np.random.rand(N) y = np.random.rand(N) fig, ax = plt.subplots() # 2D histogram plot with x and y hh = ax.hist2d(x, y, bins=40, norm=LogNorm()) fig.colorbar(hh[3], ax=ax) # Display the plot plt.show()
輸出
將生成以下輸出 -

廣告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP