Tengo una serie de valores (x, y) que quiero trazar un histograma en 2d de usar matplotlib de python. Usando hexbin, me sale algo como esto: alt text http://img121.imageshack.us/img121/1339/hexbin.png Pero yo estoy buscando algo como esto: alt text http://img17.imageshack.us/img17/1927/recthist.png Ejemplo de código:Conteo rectangular de Python Matplotlib
from matplotlib import pyplot as plt
import random
foo = lambda : random.gauss(0.0,1.0)
x = [foo() for i in xrange(5000)]
y = [foo() for i in xrange(5000)]
pairs = zip(x,y)
#using hexbin I supply the x,y series and it does the binning for me
hexfig = plt.figure()
hexplt = hexfig.add_subplot(1,1,1)
hexplt.hexbin(x, y, gridsize = 20)
#to use imshow I have to bin the data myself
def histBin(pairsData,xbins,ybins=None):
if (ybins == None): ybins = xbins
xdata, ydata = zip(*pairsData)
xmin,xmax = min(xdata),max(xdata)
xwidth = xmax-xmin
ymin,ymax = min(ydata),max(ydata)
ywidth = ymax-ymin
def xbin(xval):
xbin = int(xbins*(xval-xmin)/xwidth)
return max(min(xbin,xbins-1),0)
def ybin(yval):
ybin = int(ybins*(yval-ymin)/ywidth)
return max(min(ybin,ybins-1),0)
hist = [[0 for x in xrange(xbins)] for y in xrange(ybins)]
for x,y in pairsData:
hist[ybin(y)][xbin(x)] += 1
extent = (xmin,xmax,ymin,ymax)
return hist,extent
#plot using imshow
imdata,extent = histBin(pairs,20)
imfig = plt.figure()
implt = imfig.add_subplot(1,1,1)
implt.imshow(imdata,extent = extent, interpolation = 'nearest')
plt.draw()
plt.show()
Parece que ya debería haber una manera de hacer esto sin necesidad de escribir mi propio método "binning" y usando imshow.
este es uno dimensional. Estoy buscando un histograma bidimensional, similar a lo que imshow() o hexbin() – job
* hist * pueden hacer datos 2D, a menos que no entienda su punto. Si publicó algunos datos de ejemplo, eso podría ayudar. – Seth
Hist funciona en datos 2D, pero solo crea dos histogramas 1D intercalados. –