0

I want to generate a heatmap using Python. The map should be like this:

enter image description here I have a numpy array with dimension (n,n) and each "cell" contains a certain value. The higher higher that value is, the bigger a pink square should be. How can I plot this kind of chart using matplotlib? Are there other libraries that I can use?

Thank you.

vestland
  • 55,229
  • 37
  • 187
  • 305
RiccardoB
  • 141
  • 15
  • 1
    You mean 'Heatmap'? – Sheldore Aug 30 '18 at 11:41
  • `Heatmap` was my first thought, too, but it would mean color encoded data... This is a scatter plot with squared markers, isn't it? – SpghttCd Aug 30 '18 at 11:43
  • You can make use of filled `patches.Rectangle`. [Here](https://stackoverflow.com/questions/37435369/matplotlib-how-to-draw-a-rectangle-on-image) is a link on how to use them. You can map your values to the size of the square and use them as the length of the edges of the rectangle – Sheldore Aug 30 '18 at 11:45
  • Sometimes I find it referred as "hit-map" and sometimes as "heat-map", anyway the heat-map should be slightly different based on what I have found, indeed it doesn't change the "dimension" of the pink squares but only their colour if I am right (anyway I have already implemented a simple heatmap in Python, but I don't know how to implement this other one). – RiccardoB Aug 30 '18 at 11:46

1 Answers1

0

You could try this

n = 8
x = np.arange(n)
y = np.arange(n)
X, Y = np.meshgrid(x, y)
Z = np.random.randint(0, 800, (len(x), len(y)))
plt.figure()
plt.axes(aspect='equal')
plt.scatter(X+.5, Y+.5, Z, 'pink', marker='s')
plt.grid()
plt.xlim(0, n)
plt.ylim(0, n)
plt.tick_params(labelsize=0, length=0)

enter image description here

SpghttCd
  • 10,510
  • 2
  • 20
  • 25
  • 1
    [This answer](https://stackoverflow.com/a/48174228/4124317) can be of interest as I suppose the size of the scatter markers needs to be normalized, such that no square is bigger than the containing cell. – ImportanceOfBeingErnest Aug 30 '18 at 12:14
  • Indeed, the non-dynamical marker size of scatter might be a drawback here, so thanks for the useful link. – SpghttCd Aug 30 '18 at 12:20
  • This is very useful but there is a problem with larger charts, anyway it is possible to easily manage those cases normalizing the Z dimensions. – RiccardoB Aug 30 '18 at 12:54