2016-03-29 6 views
0

Ich mag den max ist anstelle einem 2D-Histogramm findenNumpy: Wie bekomme ich die maximale Behälterposition?

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
import numpy as np 

fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
x, y = np.random.rand(2, 100) * 10 
hist, xedges, yedges = np.histogram2d(x, y, bins=20) 

elements = (len(xedges) - 1) * (len(yedges) - 1) 
xpos, ypos = np.meshgrid(xedges[:-1]+0.25, yedges[:-1]+0.25) 

xpos = xpos.flatten() 
ypos = ypos.flatten() 
zpos = np.zeros(elements) 
dx = 0.5 * np.ones_like(zpos) 
dy = dx.copy() 
dz = hist.flatten() 

ax.bar3d(xpos, ypos, zpos, dx, dy, dz, zsort='average') 
plt.show() 

enter image description here

Das heißt, ich will den (x, y) wissen, die hist.max().

Ich denke, ich kann mit hist.argmax() durchkommen. Aber ich verstehe nicht, wie ich mit dem Rest umgehen soll (Umwandlung der 1d-Position in 2d). Oder wenn es bessere Lösungen gibt?

Antwort

2

np.unravel_index ist der idiomatische Weg, dies zu lösen

+1

So wäre dies 'Zeile, Spalte = np.unravel_index (hist.argmax(), hist.shape)'? es würde die Zeile col zurückgeben, und dann kann ich den Wert a 'xedges [row], yedges [col]' erhalten – cqcn1991

1

Teilen Sie die Position im abgeflachten Array durch die Anzahl der Spalten und Sie erhalten die Zeile. Der Rest ist die Spalte.

row, col = divmod(np.argmax(hist), hist.shape[0]) 
Verwandte Themen