1

I just wanted to ask how could I best create a frequency pixel map using matplotlib. By this term I mean something such as:

http://img513.imageshack.us/img513/8677/g8df.png

Where the colour of the square represents the frequency for the corresponding hour and interval.

I was thinking to plot a series of 50 patches and colour them according to the data and using a colour scale? Or is there a better way to do this?

Thanks

James Elder
  • 1,583
  • 3
  • 22
  • 34
  • 1
    Can't you just use a colormap with an appropriate color scale? Or is it that you want the axes to be similar as in the figure? –  Oct 25 '13 at 11:52

1 Answers1

1

It sounds like you basically just want plt.hist2d.

import numpy as np
import matplotlib.pyplot as plt

data = np.array([[8,  15,  1, 65, 79],
                 [45, 22, 60, 43, 16],
                 [3,  75, 90, 11, 14],
                 [89, 32, 27, 59, 99],
                 [62,  5, 54, 92, 81]])
nrows, ncols = data.shape
# Generate 1-based row indicies, similar to your table
row = np.vstack(ncols * [np.arange(nrows) + 1]).T

x, y = row.flatten(), data.flatten()

xbins = np.arange(nrows+1) + 0.5
plt.hist2d(x, y, bins=(xbins, 10), cmap=plt.cm.Reds)
plt.show()

enter image description here

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • Thanks! I did see plt.hist2d but I couldn't figure out how to use it. I have one quick question, lets say I have the same matrix but I have some missing values which are represented by the value 0, how can I neglect these values when plotting? For example if hour 1 is now [0, 15, 1, 65, 79] then the bottom left (0-10) rectangle should be coloured the same as the one above (10-20). – James Elder Oct 28 '13 at 10:31
  • 1
    @JamesElder - You can just pass in the non-zero values to hist2d. E.g. `plt.hist2d(x[y != 0], y[y != 0], ...)`. I can add a more detailed example of that, if it would help? – Joe Kington Oct 28 '13 at 14:24
  • perfect thank you! I'm a little bit slow today :P – James Elder Oct 28 '13 at 15:06