数据格式如下:

8_15/l_eye/2732.png -20.5773 -5.17769 -3.34583 21.5859
9_13_1/l_eye/1211.png -10.1145 34.9928 -38.2122 -26.3371
8_20/l_eye/5966.png -44.0264 50.2898 63.5838 -49.1353
8_13/l_eye/8780.png -16.9358 50.4528 -44.2617 -57.1462
9_16_2/l_eye/5370.png -21.2264 17.0589 4.33619 -20.3562
9_15_1/l_eye/66.png 40.5758 -21.0923 12.0032 40.8452
8_13/l_eye/6664.png 51.0789 55.3987 -67.2433 -79.1243
9_15_2/l_eye/4429.png 16.958 30.0386 -24.5935 -26.4802
8_21/l_eye/2579.png -20.619 4.7845 21.9891 27.529
8_21/l_eye/8464.png -36.8559 54.4664 -32.1576 -67.6335
8_21/l_eye/359.png 20.9732 2.25414 -3.88966 41.175
9_16_2/l_eye/3065.png 7.16623 43.091 35.9651 -28.4994
9_14_2/l_eye/1961.png 33.3302 28.3553 22.7904 -28.5209
9_16_1/l_eye/2038.png 56.9721 24.6518 -23.5831 -39.2209

以2、3列为x、y绘制一个热力图

以4、5列为x、y绘制一个热力图

#!/usr/bin/python
# -*- encoding: utf-8 -*- import numpy as np
from matplotlib import pyplot as plt #import thread
#from threading import Thread
from multiprocessing import Process import pdb def generate_heat_array(is_test=0):
#pdb.set_trace()
if is_test==1:
# gussian distribution
mean = [0,0]
cov = [[0,1],[1,0]]
x, y = np.random.multivariate_normal(mean, cov, 10000).T
show_heat_map(x,y)
return x_head=[]
y_head=[]
x_gaze=[]
y_gaze=[]
for line in open('train.txt'):
split_data=line.split()
x_head.append(float(split_data[1]))
y_head.append(float(split_data[2]))
x_gaze.append(float(split_data[3]))
y_gaze.append(float(split_data[4])) #用thread库实现多线程
#由于主线程退出时,子线程自动中止,因此需要join;由于thread库未提供join方法,所以需要自己手动实现。
#thread.start_new_thread(show_heat_map,(x_head,y_head,1))
#thread.start_new_thread(show_heat_map,(x_gaze,y_gaze,2)) #用threading库实现多线程
#threading库提供了join方法。但是由于matplotlib.pyplot中的方法都是全局的,因此用多线程绘图会有错误:RuntimeError: main thread is not in main loop
#head_thread=Thread(target=show_heat_map, args=(x_head,y_head,1,))
#gaze_thread=Thread(target=show_heat_map,args=(x_gaze,y_gaze,2,))
#head_thread.start()
#gaze_thread.start()
#head_thread.join()
#gaze_thread.join() #用multiprocessing实现多进程
head_process=Process(target=show_heat_map,args=(x_head,y_head,1,))
gaze_process=Process(target=show_heat_map,args=(x_gaze,y_gaze,2,))
head_process.start()
gaze_process.start()
head_process.join()
gaze_process.join() def show_heat_map(x,y,n):
#pdb.set_trace()
fig=plt.figure(n)
plt.hist2d(x,y,bins=100)
plt.grid(True)
plt.colorbar()
#fig.savefig('%02i.png'%n)
plt.show() if __name__=='__main__':
generate_heat_array(0)

绘制热力图的方法:

    plt.hist2d(x,y,bins=100)

x为横轴的值的list,y为纵轴值的list

修改bins可以控制区间大小

参考:http://blog.topspeedsnail.com/archives/707

使用meshgrid+imshow的话横纵坐标会有问题

多进程使用matplotlib.pyplot绘heatmap(多线程不可以)的更多相关文章

  1. Matplotlib.pyplot 常用方法

    1.介绍 Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形.通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图 ...

  2. matplotlib.pyplot 导引

    matplotlib.pyplot 是采用 python 语言和使用数值数学库 numpy 数组数据的绘图库.其主要目标是用于数据的可视化显示. 输出图形组成 matplotlib.pyplot 模块 ...

  3. matplotlib.pyplot 绘图详解 matplotlib 安装

    apt-get install python-matplotlib 转载自: http://www.cnblogs.com/qianlifeng/archive/2012/02/13/2350086. ...

  4. matplotlib.pyplot.hist

    **n, bins, patches = plt.hist(datasets, bins, normed=False, facecolor=None, alpha=None)** ## 函数说明 用于 ...

  5. 数据分析之matplotlib.pyplot模块

    首先都得导模块. import numpy as np import pandas as pd import matplotlib.pyplot as plt from pandas import S ...

  6. Python Matplotlib.pyplot plt 中文显示

    话不多说,上代码 # -*- coding: UTF-8 -*- import matplotlib.pyplot as plt from matplotlib.font_manager import ...

  7. 在绘图的时候import matplotlib.pyplot as plt报错:ImportError: No module named '_tkinter', please install the python-tk package

    在绘图的时候import matplotlib.pyplot as plt报错:ImportError: No module named '_tkinter', please install the ...

  8. Matplotlib.pyplot 把画图保存为图片

    在plt.show()之前执行plt.savefig()函数即可. 简单例子: import matplotlib.pyplot as plt x=[1,2,3,4,5] y=[10,5,15,10, ...

  9. 使用numpy与matplotlib.pyplot画图

    使用numpy与matplotlib.pyplot画图 1. 折线图 1 # -*- enccoding:utf-8 -*- 2 import numpy as np 3 import matplot ...

随机推荐

  1. dbcp 详细配置

    1.配置参数 username : 连接用户名 password:  连接密码 url :  连接 url( 如果连接 mysql ,格式为 jdbc:mysql://ip:port/dbname) ...

  2. Log4j容器深入探究

    1.思考 容器是什么? 水壶是容器,水壶中存放着水,等我们需要水了,直接用 Spring中的ApplicationContext是容器,里面存放着各种Bean,等我们需要了直接用,并且可以创建管理Be ...

  3. Linux下C语言的调试

    调试是每个程序员都会面临的问题. 如何提高程序员的调试效率, 更好更快地定位程序中的问题从而加快程序开发的进度, 是大家共同面对的问题. 可能Windows用户顺口就会说出:用VC呗 :-) , 它提 ...

  4. 省市联动js代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. 华为/中兴 3G 模块的调试

    1 检查/etc/init.d/pppd『红颜色的使上电自动上网注册(如果没有红颜色的语句),请添加』 #!/bin/sh#DAEMON=/usr/sbin/pppdPIDFILE=/var/run/ ...

  6. HTTP Content-Disposition Explanation [ from MDN ]

    在常规的HTTP应答中,Content-Disposition 消息头指示回复的内容该以何种形式展示,是以内联的形式(即网页或者页面的一部分),还是以附件的形式下载并保存到本地. 在multipart ...

  7. hashSet的底层是数组,其查询效率非常高

    如果偷懒,没有设定equals(),就会造成返回hashCode虽然结果相同,但在程序执行的过程中会多次地调用equals(),从而影响程序执行的效率. 我们要保证相同对象的返回的hashCode一定 ...

  8. 对于try catch放在能够很好地处理例外的位置

    Exception有一个message属性.在使用catch的时候可以调用: Catch(IOException e){System.out.println(e.message())}; Catch( ...

  9. 【BZOJ】1034: [ZJOI2008]泡泡堂BNB(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1034 弱的比弱的强就用,强的比强的强就用: 否则弱的和强的比. 输的情况就是2n-ans(b,a), ...

  10. 【BZOJ】1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏(floyd)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1641 这种水题无意义... #include <cstdio> #include < ...