【问题标题】:Python 2-D Histogram with Discrete Colormap具有离散颜色图的 Python 2-D 直方图
【发布时间】:2014-06-27 19:43:20
【问题描述】:

我有以下代码绘制点然后绘制直方图:

# Produce a number of points in x-y from 1 distribution. 
mean = [3,4]
cov = [[3,1],[1,3]] 
x,y = np.random.multivariate_normal(mean,cov,1000).T
plt.plot(x,y,'x'); plt.axis('equal'); plt.show()
Z = np.array([x,y])

# Produce 2D histogram projection
H,xedges,yedges = np.histogram2d(x,y,10,normed=False)
X,Y = np.meshgrid(xedges,yedges)
plt.imshow(H)
plt.grid(True)

我只是希望 imshow() 上的直方图更块状而不是如此模糊,以便每个方形箱的密度更清晰。我不知道该怎么做。

【问题讨论】:

    标签: python matplotlib histogram


    【解决方案1】:

    所以,你的初始代码应该是这样的

    从 matplotlib 导入 pyplot 作为 plt 将 numpy 导入为 np

    # Produce a number of points in x-y from 1 distribution. 
    mean = [3,4]
    cov = [[3,1],[1,3]] 
    x,y = np.random.multivariate_normal(mean,cov,1000).T
    plt.plot(x,y,'x'); plt.axis('equal'); plt.show()
    Z = np.array([x,y])
    
    # Produce 2D histogram projection
    H,xedges,yedges = np.histogram2d(x,y,10,normed=False)
    X,Y = np.meshgrid(xedges,yedges)
    plt.imshow(H)
    plt.grid(True)
    

    如果我正确理解“blockier”,你想要这样的东西!?

    plt.hist2d(x, y, bins=40)
    plt.colorbar()
    plt.show()
    

    【讨论】:

      【解决方案2】:

      您可以简单地设置imshow使用的插值方法:

      plt.imshow(H, interpolation = 'none')
      

      这里的an example 显示了不同的插值方法,the docs 列出了所有实现的方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-02
        • 1970-01-01
        • 2017-06-29
        • 2021-09-05
        • 2016-06-25
        • 1970-01-01
        相关资源
        最近更新 更多