19

我有一系列 (x,y) 值,我想绘制使用 python 的 matplotlib 的 2d 直方图。使用 hexbin,我得到这样的东西: 替代文字 但我正在寻找这样的东西: 替代文字 示例代码:

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()

似乎应该已经有一种方法可以做到这一点,而无需编写我自己的“分箱”方法并使用 imshow。

4

5 回答 5

12

Numpy 有一个名为histogram2d的函数,其文档字符串还向您展示了如何使用 Matplotlib 对其进行可视化。添加interpolation=nearest到 imshow 调用以禁用插值。

于 2010-01-15T15:57:15.677 回答
2

我意识到有一个补丁提交给 matplotlib,但我采用了另一个示例中的代码来满足我的一些需求。

现在直方图是从左下角绘制的,就像在传统数学中一样(不是计算)

此外,分箱范围之外的值将被忽略,我使用 2d numpy 数组作为二维数组

我将数据输入从对更改为两个一维数组,因为这是将数据提供给 scatter(x,y) 和类似函数的方式

def histBin(x,y,x_range=(0.0,1.0),y_range=(0.0,1.0),xbins=10,ybins=None):
    """ Helper function to do 2D histogram binning
        x, y are  lists / 2D arrays 
        x_range and yrange define the range of the plot similar to the hist(range=...) 
        xbins,ybins are the number of bins within this range.
    """

    pairsData = zip(x,y)

    if (ybins == None):
        ybins = xbins
    xdata, ydata = zip(*pairsData)
    xmin,xmax = x_range
    xmin = float(xmin)
    xmax = float(xmax)

    xwidth = xmax-xmin
    ymin,ymax = y_range    
    ymin = float(ymin)
    ymax = float(ymax)
    ywidth = ymax-ymin

    def xbin(xval):
        return floor(xbins*(xval-xmin)/xwidth) if xmin <= xval  < xmax else xbins-1 if xval ==xmax else None


    def ybin(yval):
        return floor(ybins*(yval-ymin)/ywidth) if ymin <= yval  < ymax else ybins-1 if yval ==ymax else None

    hist = numpy.zeros((xbins,ybins)) 
    for x,y in pairsData:
        i_x,i_y = xbin(x),ybin(ymax-y)
        if i_x is not None and i_y is not None:
            hist[i_y,i_x] += 1 

    extent = (xmin,xmax,ymin,ymax)

    return hist,extent
于 2012-06-01T17:22:58.727 回答
1

我刚刚为此https://github.com/matplotlib/matplotlib/pull/805提交了拉取请求。希望它会被接受。

于 2012-03-28T21:28:58.940 回答
0

matplotlib.pyplot.hist你要找的吗?

>>> help(matplotlib.pyplot.hist)
Help on function hist in module matplotlib.pyplot:

hist(x, bins=10, range=None, normed=False, weights=None, cumulative=False, botto
m=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=Fa
lse, hold=None, **kwargs)
    call signature::

      hist(x, bins=10, range=None, normed=False, cumulative=False,
           bottom=None, histtype='bar', align='mid',
           orientation='vertical', rwidth=None, log=False, **kwargs)

    Compute and draw the histogram of *x*. The return value is a
    tuple (*n*, *bins*, *patches*) or ([*n0*, *n1*, ...], *bins*,
    [*patches0*, *patches1*,...]) if the input contains multiple
    data.
于 2010-01-09T00:54:15.247 回答
0

使用xlimylim设置绘图的范围。xlim(-3, 3)并且ylim(-3, 3)应该这样做。

于 2010-11-19T02:44:42.153 回答