多色热图错误Python [英] multi colored Heat Map error Python

查看:104
本文介绍了多色热图错误Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过从文件中读取数据来绘制具有多种颜色的热图.我可以很好地生成2D和法线热图,但无法绘制出像附加图像那样的图.当使用随机数时,我可以绘制此图,但是从文件中读取数据时却显示错误.

I am trying to plot a heat map with multiple colors by reading data from a file. I can very well generate a 2D and normal heat map but not able to plot the one like attached image. When used random numbers I can plot this but while reading data from the file it is showing me error.

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

The above heat maps is generated with random data

输入: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

任何有用的建议表示赞赏

Any useful suggestion appreciated

推荐答案

您看到的错误来自meshgrid试图建立可能很大的矩阵.如果您的数据包含N行,则矩阵将为N×N.根据您拥有的点数和它们的密集程度,您将需要热图中的两件事之一.要么

The error you are seeing is coming from meshgrid trying to build probably quite massive matrices. If your data contains N lines, the matrices will be N×N. Depending how many points you have and how densely they are packed, you will be wanting one of two things in the heatmap. Either

  1. 您想要在相距较远的点之间进行插值以形成光滑的表面,或者
  2. 您要通过统计在特定区域中落下的点来聚集密集点(二维直方图).

我已经修改了下面的代码以适用于第二种情况(hexbin会自动执行此操作),因为您似乎没有引用数据中的第三个值进行插值.

I've modified your code below to work for the second case (hexbin does this automatically) as you don't appear to be referencing a third value in your data to interpolate on.

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调用可能来自您在第一个选项(在隔开的点之间进行插值)的某个地方找到的一段代码,也许是我使用的食谱条目网格化不规则间隔的数据

The vestigial meshgrid call which I've commented out above is perhaps from a piece of code you found somewhere which does the first option (interpolating between spaced-out points), perhaps my using griddata. If this is in fact what you want, you can have a look at this cookbook entry on gridding irregularly spaced data

这篇关于多色热图错误Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆