【问题标题】:Having trouble plotting 2-D histogram with numpy.histogram2d and matplotlib使用 numpy.histogram2d 和 matplotlib 绘制二维直方图时遇到问题
【发布时间】:2014-07-22 23:22:29
【问题描述】:

我有一个海量数据集,我需要将我的绘图分成一个网格并计算每个网格正方形内的点数。我正在遵循here 概述的方法:

下面是我的代码的精简版本:

import numpy as np
import matplotlib.pyplot as plt

x = [ 1.83259571, 1.76278254, 1.38753676, 1.6406095, 1.34390352, 1.23045712, 1.85877565, 1.26536371, 0.97738438]

y = [ 0.04363323, 0.05235988, 0.09599311, 0.10471976, 0.1134464, 0.13962634, 0.17453293, 0.20943951, 0.23561945]

gridx = np.linspace(min(x),max(x),11)
gridy = np.linspace(min(y),max(y),11)

grid, _, _ = np.histogram2d(x, y, bins=[gridx, gridy])

plt.figure()
plt.plot(x, y, 'ro')
plt.grid(True)

plt.figure()
plt.pcolormesh(gridx, gridy, grid)
plt.plot(x, y, 'ro')
plt.colorbar()

plt.show()

出现问题的地方是网格将绘图的元素识别为点出现的位置,但其中一些元素中没有点;同样,在某些实际数据点出现的地方,网格不会将它们识别为实际上并不存在。

什么可能导致这个问题?另外,很抱歉没有附上剧情,我是新用户,知名度不够高。

更新 这是一个生成 100 个随机点并尝试将它们绘制成二维直方图的代码:

import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand(100)

y = np.random.rand(100)

gridx = np.linspace(0,1,11)
gridy = np.linspace(0,1,11)

grid, __, __ = np.histogram2d(x, y, bins=[gridx, gridy])

plt.figure()
plt.plot(x, y, 'ro')
plt.grid(True)

plt.figure()
plt.pcolormesh(gridx, gridy, grid)
plt.plot(x, y, 'ro')
plt.colorbar()

plt.show()

然而,当我运行它时,我遇到了与以前相同的问题:点的位置和对应于点位置密度的颜色不一致。当有人为自己运行此代码时会发生这种情况吗?

第二次更新

冒着打死马的风险,这里有一个参数图的代码:

import numpy as np
import matplotlib.pyplot as plt

t = np.linspace(0,1,100)
x = np.sin(t)
y = np.cos(t)

gridx = np.linspace(0,1,11)
gridy = np.linspace(0,1,11)

#grid, __, __ = np.histogram2d(x, y, bins=[gridx, gridy])
grid, __, __ = np.histogram2d(x, y)

plt.figure()
plt.plot(x, y, 'ro')
plt.grid(True)

plt.figure()
plt.pcolormesh(gridx, gridy, grid)
plt.plot(x, y, 'ro')
plt.colorbar()

plt.show()

这让我觉得这都是某种奇怪的缩放问题。不过还是完全迷路了……

【问题讨论】:

  • 上述不起作用的原因可能是由于您的数据中缺少数据点。对于 x 和 y,您似乎只有 9 个数据点。而您遵循的示例有 100 个数据点,请尝试使用 9 个点的相同示例,但它不起作用!
  • np.histogram2d 会不会有随机分散的小数的问题?我用100分尝试了你的建议,但它仍然没有用。奇怪的是,当我尝试 x 和 y 等于 linspace(0,1,100) 的测试用例时,colormesh 函数运行良好。

标签: python-2.7 numpy matplotlib


【解决方案1】:

我能够通过使用带有插值的 imshow 而不是 pcolormesh 来让您的示例正常工作。请参阅下面的示例代码。

我认为问题可能在于 pcolormesh 的起源约定与 plot 不同。 pcolormesh的结果看起来像左上右下翻转了。

imshow 的结果如下:

示例代码:

import numpy as np
import matplotlib.pyplot as plt

def doPlot():

    x = [ 1.83259571, 1.76278254, 1.38753676, 1.6406095, 1.34390352, 1.23045712, 1.85877565, 1.26536371, 0.97738438]

    y = [ 0.04363323, 0.05235988, 0.09599311, 0.10471976, 0.1134464, 0.13962634, 0.17453293, 0.20943951, 0.23561945]

    gridx = np.linspace(min(x),max(x),11)
    gridy = np.linspace(min(y),max(y),11)

    H, xedges, yedges = np.histogram2d(x, y, bins=[gridx, gridy])

    plt.figure()
    plt.plot(x, y, 'ro')
    plt.grid(True)

    #wrong origin convention for pcolormesh?
    #plt.figure()
    #plt.pcolormesh(gridx, gridy, H)
    #plt.plot(x, y, 'ro')
    #plt.colorbar()


    plt.figure()
    myextent  =[xedges[0],xedges[-1],yedges[0],yedges[-1]]
    plt.imshow(H.T,origin='low',extent=myextent,interpolation='nearest',aspect='auto')
    plt.plot(x,y,'ro')
    plt.colorbar()

    plt.show()

if __name__=="__main__":
    doPlot()

【讨论】:

  • 是的,做到了。非常感谢。
【解决方案2】:

参考 numpy histogram2d 的文档...

细心的读者会注意到参数是倒数的。

histogram2d(y, x, bins=(xedges, yedges)

计算两个数据样本的二维直方图。

参数

x : array_like, 形状 (N,) 一个数组,包含要成为的点的 x 坐标 直方图。

y : array_like, 形状 (N,) 一个数组,包含要成为的点的 y 坐标 直方图。

因此,您将 x 提供给函数的 y 参数,反之亦然。

问候

【讨论】:

猜你喜欢
  • 2012-10-20
  • 2021-12-03
  • 2015-05-29
  • 1970-01-01
  • 2016-06-09
  • 2015-10-25
  • 1970-01-01
相关资源
最近更新 更多