Reputation: 7333
This seems like a simple task or a repeat, but bear with me-- I've searched for a little while and haven't found any easy answers.
I have a scatterplot that I would like to display as a heatmap. My values look like this:
{ (3, 3): 1.7314, (3,4):-6.99, (4,3):-17.3, (4, 4):-100.0 }
I would like to display a matrix starting with cell (3,3)
, which has a brightness of 1.7314
, etc.
I have found several questions and answers regarding situations where you give lists of two dimensional tuples (X, Y)
and the Z
value (the intensity) for each point (x,y,z)
is created by the number of occurrences around (x, y)
.
I have also used imshow
to draw such a plot, but for imshow
you drop the (3,3)
, etc. So things can be shifted strangely. One option is to use imshow
and then manually adjust the axis labels afterward. But I feel like someone must have solved this before without hacking pylab
too much.
What is the best way to do this?
Upvotes: 1
Views: 1856
Reputation: 9172
Ok, let's try an easy example using your sample data:
import numpy as np
from pylab import *
data = { (3, 3): 1.7314, (3,4):-6.99, (4, 3):-17.3, (4, 4):-100.0 }
matrix = np.zeros((5, 5))
for (x, y), z in data.items():
matrix[y,x] = z
imshow(matrix[3:, 3:], origin='lower', interpolation='none', extent=[2.5, 4.5, 2.5, 4.5])
show()
As you see, you can control the axes by specifying the limits using extent
. The default (if None
) would be (-0.5, numcols-0.5, -0.5, numrows-0.5)
(left, right, bottom, top) if you specify you want the origin
in the lower part, for the Y axis.
Also, interpolation='none'
is important for your case.
Upvotes: 2