3

I have three column file,5 million lines. It is like

x,y,z
3,4,6.7
9,4,7.8

X and y are pixel numbers and z are corresponding values at (x,y)
How to plot a heat map?
A 2D plot is a compromise for my original thought.
You can check my original post How to use griddata from scipy.interpolate

I tried the way below but it is just a scatter point plot.

import numpy as np
import pylab as pl
x,y,z =np.loadtxt('3columns.csv',delimiter=',',usecols=(0,1,2),unpack=True)

pl.scatter(x, y, c=z)

pl.show()
Community
  • 1
  • 1
questionhang
  • 735
  • 2
  • 12
  • 31
  • Are there any duplicates? Are all possible coordinates represented in your .csv or are there undefined pixels? – lejlot Aug 12 '13 at 15:30
  • Do you want some sort of 3D surface plot instead of a 2D scatter plot ? If so, have you looked at http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html ? – lmjohns3 Aug 12 '13 at 20:55
  • There are many duplicates because of the pipeline.Many pixelvalues are the same,but all the pixels are defined...I have checked that link.I prefer a 3d surface plot which griddata should be used.The data is sort of ireregular,so maybe a grid should be built first. – questionhang Aug 13 '13 at 01:08

1 Answers1

0

I've encountered similar problems. What I did is to set an array Z[row[0]][row[1]] = row[2].

import numpy as np
x,y,z =np.loadtxt('3columns.csv',delimiter=',',usecols=(0,1,2),unpack=True)
nx = x.max() - x.min() + 1
ny = y.max() - y.min() + 1
Z = np.zeros((nx,ny)) 

assert x.shape == y.shape == z.shape
for i in range(len(x)):
    Z[x[i]-x.min()][y[i]-y.min()] = z[i] 

import matplotlib.pyplot as plt 
fig = plt.figure()
figure_name = 'figure_name'
plt.pcolor(np.arange(nx),np.arange(ny),Z,cmap=plt.cm.Reds)
plt.colorbar()
plt.xlim(0,x.max()-x.min())
plt.ylim(0,y.max()-y.min())

xlabels = np.arange(x.min(),x.max(),Nspacingx) # define Nspacing accordingly 
ylabels = np.arange(y.min(),y.max(),Nspacingy) 
plt.xticks(np.arange(0,x.max()-x.min(),Nspacingx),xlabels)
plt.yticks(np.arange(0,y.max()-y.min(),Nspacingy),ylabels)

plt.savefig(figure_name,dpi=400)

In this way, you can plot a 2D heatmap from a 3-column data file.

Wang Zong'an
  • 1,492
  • 5
  • 24
  • 29