zoukankan      html  css  js  c++  java
  • 多进程使用matplotlib.pyplot绘heatmap(多线程不可以)

    数据格式如下:

    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的话横纵坐标会有问题

  • 相关阅读:
    [已解决]报错:报错AttributeError: 'int' object has no attribute 'upper'
    Pandas之read_excel()和to_excel()函数解析
    [已解决]报错This event loop is already running
    [转]NMON服务器监控、指标说明
    LoadRunner编程之跳出迭代
    sqlplus与shell互相传值的几种情况
    Loadrunner中socket协议中的三个关联函数
    ORACLE expdp/impdp导出实例
    Linux下的split 命令(将一个大文件根据行数平均分成若干个小文件)
    批量快速的导入导出Oracle的数据(spool缓冲池、java实现)
  • 原文地址:https://www.cnblogs.com/zealousness/p/9753282.html
Copyright © 2011-2022 走看看