views:

355

answers:

1

Hello all, I was able to load my csv file into a numpy array: data = np.genfromtxt('csv_file', dtype=None, delimiter=',') 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

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.

many thanks

+1  A: 

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).

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' refers to the particular matplotlib color map
# i chose 'r' just means 'reverse'
# so that '0' is white; 
# there are quite a few color maps in matplotlib
# use dir(matplotlib.cm) to get a list of the installed colormaps

# select the color map
cmap = CM.get_cmap('gray_r', 10)

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

# plot it
PLT.show()
doug
Thanks for the reply.I can get the plot easily with random number, however I can't get my csv file to plot.first it refuses to reshape. I have NaNs there so I tried masking but that failed too. Also, I had to delete the header and first column, is there a way to leave them and get labels for the axes?I've edited the original question to include an excerpt of the csv file.many thanks.
Schrodinger's Cat
Should `A2` be replaced by `M`? It seems that A2 has not been properly defined in this example
Stedy