weights
參數需要一個與x
和y
相同長度的數組。 np.histogram2d
。它不會播出一個恆定值,所以即使質量是每個通話同樣以np.histogram2d
,你仍然必須使用類似
weights=np.ones_like(x)*mass
現在,你可能會遇到,如果你使用bin=nbin
一個問題是,根據您傳遞給np.histogram2d
的x
和y
的值,可能會改變分區邊緣,xedges
,yedges
。如果你天真地將熱點圖加在一起,最終的結果會在錯誤的地方累積粒子密度。
因此,如果您想多次調用np.histogram2d
並將部分熱圖集合在一起,則必須事先確定要在哪裏進行料倉邊緣。
例如:
import numpy as np
import itertools as IT
import matplotlib.pyplot as plt
N = 50
nbin = 10
xs = [np.array([i,i,i+1,i+1]) for i in range(N)]
ys = [np.array([i,i+1,i,i+1]) for i in range(N)]
masses = np.arange(N)
heatmap = 0
xedges = np.linspace(0, N, nbin)
yedges = np.linspace(0, N, nbin)
for x, y, mass in IT.izip(xs, ys, masses):
hist, xedges, yedges = np.histogram2d(
x, y, bins=[xedges, yedges], weights=np.ones_like(x)*mass)
heatmap += hist
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
heatmap = np.flipud(np.rot90(heatmap))
fig, ax = plt.subplots()
ax.imshow(heatmap, extent=extent, interpolation='nearest')
plt.show()
產生
![enter image description here](https://i.stack.imgur.com/WH0lG.png)
是的,這應該幾乎做的伎倆。 – Jaime