使用numpy和matplotlib将CSV文件数据绘制为热图 [英] draw csv file data as a heatmap using numpy and matplotlib

查看:266
本文介绍了使用numpy和matplotlib将CSV文件数据绘制为热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够将csv文件加载到numpy数组中:

I was able to load my csv file into a numpy array:

data = np.genfromtxt('csv_file', dtype=None, delimiter=',')

现在,我想生成一个热图.我从以下11个样本中获得19个类别:

Now I would like to generate a heatmap. I have 19 categories from 11 samples, along these lines:

 COG          station1    station2    station3    station4      
COG0001     0.019393497 0.183122497 0.089911227 0.283250444 0.074110521
COG0002     0.044632051 0.019118032 0.034625785 0.069892277 0.034073709
COG0003     0.033066112 0           0           0           0
COG0004     0.115086472 0.098805295 0.148167492 0.040019101 0.043982814
COG0005     0.064613057 0.03924007  0.105262559 0.076839235 0.031070155 
COG0006     0.079920475 0.188586049 0.123607421 0.27101229  0.274806929 
COG0007     0.051727492 0.066311584 0.080655401 0.027024185 0.059156417     
COG0008     0.126254841 0.108478559 0.139106704 0.056430812 0.099823028

我想使用matplotlib colormesh,但是我很茫然. 我可以找到的所有示例都使用了随机数数组. 任何帮助和见解将不胜感激.

I wanted to use matplotlib colormesh, but I'm at loss. all the examples I could find used random number arrays. any help and insights would be greatly appreciated.

推荐答案

我可以从您的问题中解密的是,您有一个11 x 19的数组,并且组成该数组的数字似乎是0≤范围内的实数. = x< = 1(显然,两个假设对答案都不重要).

What i can decrypt from your question is that you have an 11 x 19 array and the numbers comprising this array appear to be real numbers in the range 0 <= x <= 1 (obviously neither assumption is critical to the answer).

下面是用于创建阵列热图的代码,以使最小值较小,而较大值具有较深的灰色阴影(例如,"0"为白色,而"1"为黑色).

Below is the code to create a heatmap of your array such that the smallest values are lighter and the larger values are darker shades of grey (eg, '0' is white, and '1' is black).

因此,首先,创建一个形状和值范围相同的数组 :

So first, create an array identical in shape and value range to yours:

import numpy as NP
M = NP.random.rand(209).reshape(11, 19)
M.shape
# returns: (11, 19)
# if the array returned from your call to 'genfromtxt' 
# is not 11 x 19, 
# then you need to reshape it so that it is, 
# use, e.g., 'data.reshape(11, 19)'

from matplotlib import pyplot as PLT
from matplotlib import cm as CM


fig = PLT.figure()
ax1 = fig.add_subplot(111)

gray_r 指的是特定的matplotlib 颜色图-即,创建一个查找表,将2D数组中的每个单元格值映射到单元格颜色/hue(换句话说:颜色图只是将调色板映射到数据;

gray_r refers to a particular matplotlib color map--ie, creates a look-up table that maps each of the cell values in your 2D array to a cell color/hue (put another way: color maps just maps a palette to data;

r 只是指 reverse ;我喜欢这种映射,因为它对我来说似乎更直观-即将白色映射为0,将较大的值映射为较深的灰色阴影;

the r just refers to reverse; i pefer this mapping because it seems more intuitive to me--ie, white is mapped to 0 and larger values are mapped to darker shades of gray;

可用的颜色图在模块 cm 中; dir(matplotlib.cm) 以获得已安装颜色图的列表(有数十种); Matplotlib网站具有出色的视觉效果(作为一组matplotlib图当然).

the available colormaps are in the module cm; dir(matplotlib.cm) to get a list of the installed colormaps (there are dozens); the Matplotlib Site has an excellent visual display of them (as a set of matplotlib plots of course).

# select the color map by calling get_cmap and passing in a registered colormap 
# and an integer value for _lut_ which is just the number of different colors desired
cmap = CM.get_cmap('gray_r', 10)

# map the colors/shades to your data
ax1.imshow(M, interpolation="nearest", cmap=cmap)

# plot it
PLT.show()

这篇关于使用numpy和matplotlib将CSV文件数据绘制为热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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