簡體   English   中英

如何為2D直方圖選擇自定義bin大小?

[英]How do you select a custom bin size for a 2D histogram?

我已經知道如何在查看此答案后設置1D直方圖的bin大小,但我不知道如何為2D直方圖執行此操作。 我已經提到了NumpyMatplotlib頁面的2D直方圖,但是我調整bin大小的所有嘗試都沒有工作/運行。我希望我的箱子的長度為20,寬度為2。

示例代碼:

import numpy as np
import matplotlib.pyplot as plt
import math

dataX = np.random.uniform(-50,50,100)
dataY = np.random.uniform(-50,50,100)

binWidth = 2.0
binLength = 20.0

xMin = min(dataX)
xMax = max(dataX)
yMin = min(dataY)
yMax = max(dataY)

#this and all other similar attempts that I have tried did not run
np.histogram2d(dataX, dataY, bins = np.arange((xMin,xMax + binWidth),(yMin,yMin +binLength),(binWidth,binLength)))

期待您的提示/幫助/建議。

你可以這樣做

plt.hist2d(dataX, dataY, bins = [np.arange(xMin, xMax, binWidth), np.arange(yMin, yMax, binLength)])

bins接受整數或數組來定義bin或其邊緣的數量,每個bin或者兩個維度都是一次,或者在包含兩個條目的列表中為x和y提供兩個不同的定義。

來自文檔:

bin:int或array_like或[int,int]或[array,array],可選

垃圾箱規格:

  If int, the number of bins for the two dimensions (nx=ny=bins). If array_like, the bin edges for the two dimensions (x_edges=y_edges=bins). If [int, int], the number of bins in each dimension (nx, ny = bins). If [array, array], the bin edges in each dimension (x_edges, y_edges = bins). A combination [int, array] or [array, int], where int is the number of bins and array is the bin edges. 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM