0

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.

Community
  • 1
  • 1
Dracarys
  • 291
  • 1
  • 11

1 Answers1

1

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

cel
  • 30,017
  • 18
  • 97
  • 117
  • 1
    I would add that you should look into `imshow` (not sure if `matshow` does colorbars, but if so then use that) and adding a color bar. Check out [this example](http://matplotlib.org/1.3.1/examples/pylab_examples/colorbar_tick_labelling_demo.html) – Engineero Jun 15 '16 at 04:04
  • Thanks cel! But this may lose all the coordinate information. And the data of x, y, z may not be integers. – Dracarys Jun 15 '16 at 07:44
  • @WonderHAO, oh in that case you probably have to first discretize your data into bins. For that `numpy.digitize` will come in handy. Then you just sum up the z scores in each bin and plot the matrix you get with `imshow`. – cel Jun 15 '16 at 07:52