There's a csv file with format:
x0, y0, v00
x0, y1, v01
...
x1, y0 v10
...
And what I want to do is to plot a heat map, in which at location (x, y) the value v is plotted with corresponding color. Below is my current implementation.
import random
import numpy as np
import matplotlib.pyplot as plt
def create_test_csv(file):
random.seed(42)
f = open(file, "w")
for x in range(300):
for y in range(600):
value = random.randrange(255)
f.write(str(x) + "," + str(y) + "," + str(value) + "\n")
def get_xyz_from_csv_file(csv_file_path):
'''
get x, y, z value from csv file
csv file format: x0,y0,z0
'''
x = []
y = []
z = []
map_value = {}
for line in open(csv_file_path):
list = line.split(",")
temp_x = float(list[0])
temp_y = float(list[1])
temp_z = float(list[2])
x.append(temp_x)
y.append(temp_y)
z.append(temp_z)
map_value[(temp_x, temp_y)] = temp_z
return x, y, map_value
def draw_heatmap(x, y, map_value):
plt_x = np.asarray(list(set(x)))
plt_y = np.asarray(list(set(y)))
plt_z = np.zeros(shape = (len(plt_x), len(plt_y)))
for i in range(len(plt_x)):
for j in range(len(plt_y)):
if map_value.has_key((plt_x.item(i), plt_y.item(j))):
plt_z[i][j] = map_value[(plt_x.item(i), plt_y.item(j))]
z_min = plt_z.min()
z_max = plt_z.max()
plt_z = np.transpose(plt_z)
plot_name = "demo"
color_map = plt.cm.gist_heat #plt.cm.rainbow #plt.cm.hot #plt.cm.gist_heat
plt.clf()
plt.pcolor(plt_x, plt_y, plt_z, cmap=color_map, vmin=z_min, vmax=z_max)
plt.axis([plt_x.min(), plt_x.max(), plt_y.min(), plt_y.max()])
plt.title(plot_name)
plt.colorbar().set_label(plot_name, rotation=270)
ax = plt.gca()
ax.set_aspect('equal')
figure = plt.gcf()
plt.show()
return figure
if __name__ == "__main__":
csv_file_name = "test.csv"
create_test_csv(csv_file_name)
x, y, map_value = get_xyz_from_csv_file(csv_file_name)
draw_heatmap(x, y, map_value)
Function create_test_csv()
created a test csv file. Function get_xyz_from_csv_file()
create x, y coordinates list and a dict which key is tuple (x,y)
and value is v
. Function draw_heatmap()
plot the heat map using list x, y and dict map_value
.
It works but I would like to know if there is some more straightforward way to this, especially the transition from CSV to the matrix that created the heat map. It might worth to notice that in my real case the coordinate may not be integer.