多色热图错误Python

时间:2015-05-06 05:12:09

标签: python file numpy heatmap

我试图通过从文件中读取数据来绘制具有多种颜色的热图。我可以很好地生成2D和普通热图,但不能绘制像附加图像那样的热图。当使用随机数时,我可以绘制这个但是在从文件中读取数据时它显示错误。

enter image description here

上面的热图是用随机数据生成的

输入:col[1]col[2]xy坐标

00022d9064bc 819251 440006 1073260801 1073260803 2.0 
00022dba8f51 819251 440006 1073260801 1073260803 2.0 
00022de1c6c1 819251 440006 1073260801 1073260803 2.0 
003065f30f37 819251 440006 1073260801 1073260803 2.0 
00904b48a3b6 819251 440006 1073260801 1073260803 2.0 
00904b83a0ea 819213 439954 1073260803 1073260810 7.0 
00904b85d3cf 817526 439458 1073260803 1073261920 1117.0 
00904b14b494 817558 439525 1073260804 1073265410 4606.0 
00904b99499c 817558 439525 1073260804 1073262625 1821.0 
00904bb96e83 817558 439525 1073260804 1073265163 4359.0 
00904bf91b75 817558 439525 1073260804 1073263786 2982.0 
00022d36a6df 820428 438735 1073260807 1073260809 2.0 

代码:

from matplotlib import pyplot as plt 
from matplotlib import cm as CM
from matplotlib import mlab as ml
import numpy as np 

data = np.loadtxt('inputfile', unpack=True, dtype='str, int, int, int, int, float')

x  = data[1]
y  = data[2]

X, Y = np.meshgrid(x,y)

x = X.ravel()
y = Y.ravel()

gridsize = 30 
plt.subplot(111)

cb = plt.colorbar()
cb.set_label('density')
plt.show() 

错误:

Traceback (most recent call last):
  File "heat3.py", line 11, in <module>
    X, Y = np.meshgrid(x,y)
  File "/usr/lib/python2.7/dist-packages/numpy/lib/function_base.py", line 3106, in meshgrid
    mult_fact = np.ones(shape, dtype=int)
  File "/usr/lib/python2.7/dist-packages/numpy/core/numeric.py", line 178, in ones
    a = empty(shape, dtype, order)
MemoryError

任何有用的建议都表示赞赏

1 个答案:

答案 0 :(得分:0)

您看到的错误来自meshgrid尝试构建可能非常庞大的矩阵。如果您的数据包含N行,则矩阵将为N×N。根据您拥有的点数以及它们的密集程度,您将需要热图中的两件事之一。任

  1. 您希望在相距很远的点之间插值以形成光滑的表面,或
  2. 您希望通过计算特定区域(2D直方图)中的落数来聚合密集点。
  3. 我已修改下面的代码以适用于第二种情况(hexbin自动执行此操作),因为您似乎没有引用要插入的数据中的第三个值。

    from matplotlib import pyplot as plt 
    from matplotlib import cm as CM
    from matplotlib import mlab as ml
    import numpy as np 
    
    data = np.loadtxt('inputfile', unpack=True, dtype='str, int, int, int, int, float')
    
    x = data[1]
    y = data[2]
    z = data[5]
    
    # These lines are completely unnecessary and perhaps come
    # from a different solution which was interpolating between points
    #X, Y = np.meshgrid(x,y)
    #x = X.ravel()
    #y = Y.ravel()
    
    gridsize = 30 
    #plt.subplot(111)  # <- You don't need this as it is one plot anyway
    plt.hexbin(x, y, C=z)   # <- You need to do the hexbin plot
    cb = plt.colorbar()
    cb.set_label('density')
    plt.show() 
    

    我上面评论过的残余meshgrid调用可能来自你在某处找到第一个选项的代码(在间隔点之间进行插值),也许是我使用griddata 。如果这实际上是你想要的,你可以看一下网格不规则间隔数据的cookbook entry