Dracarys
Dracarys

Reputation: 291

create heatmap of 3 list using matplotlib and numpy

I have 3 Python list: x = [x0, x1, x2, ..., xn], y = [y0, y1, y2, ..., yn] and v = [v0, v1, v2, ..., vn] and what I need to do it to visualize the data by creating a heatmap, where at coordinate (x[k], y[k]), value v[k] is visualized, the result could be something like the result in GnuPlot Heatmap XYZ. Due to system constrain I cannot use other 3rd-party tools except numpy and matplotlib.

I've found some related topic (Heatmap in matplotlib with pcolor?, Generate a heatmap in MatPlotLib using a scatter data set) but it seems not resolved the same issue.

Upvotes: 0

Views: 2114

Answers (1)

cel
cel

Reputation: 31349

If the encoded matrix is not too large for memory, it can easily be converted to a dense numpy array using array slicing:

import numpy as np
import matplotlib.pyplot as plt

x = [1, 0]
y = [0, 1]
v = [2, 3]

M = np.zeros((max(x) + 1, max(y) + 1))
M[x, y] = v

fig, ax = plt.subplots()
ax.matshow(M)

plot

Upvotes: 1

Related Questions