Jeroen
Jeroen

Reputation: 841

Matplotlib annotated heatmaps formatting

I have counted samples for lat/lon bins:

dlon = [4.90148783 4.91438189 4.92727594 4.94017    4.95306406 4.96595812] # longitudes
dlat = [51.81923676 51.82162018 51.8240036  51.82638702 51.82877045 51.83115387] # latitudes
count = [[10. 16. 16.  0.  5.]
 [ 0.  0.  0.  5.  0.]
 [ 0.  0.  0.  0.  2.]
 [ 0.  0.  0.  2.  0.]
 [12.  0.  6. 13. 13.]] # number of times a variable is within the gridcell

I created an annotated heatmap by:

fig =  plt.figure()
ax = fig.add_subplot(111)
# Show all ticks...
ax.set_xticks(np.arange(len(dlon)))
ax.set_yticks(np.arange(len(dlat)))
# .Label them with the respective entries
ax.set_xticklabels(np.around(dlon, decimals=4))
ax.set_yticklabels(np.around(dlat, decimals=4))
im = ax.imshow(count)
cbar = fig.colorbar(im)

for i in np.arange(np.shape(count)[0]): # over all rows of count 
    for j in np.arange(np.shape(count)[1]): # over all cols of count 
        text = ax.text(j, i, count[i, j],ha="center", va="center" ,color="w")

ax.set_title("Counts in bins")
plt.show()

I have two questions: 1) In the heatmap the latitudes on the y-axis are in reversed order. How do I flip them so the largest value ends in the top left? 2) I have a 5x5 grid (5 rows and 5 cols in count), the boundaries of lat/lon is 6x6. Now it shows the middle of my grid points, but I want to show the corners of the grid. How do I do this?

Upvotes: 1

Views: 974

Answers (1)

JohanC
JohanC

Reputation: 80309

imshow(..., origin='lower') sets the origin at the lower left. Note that images usually start at the top, so if you need something more similar to a xy plot the origin has to be set explicitly.

The ticks have an axis that goes 0,1,2 for the centers of the 'pixels'. If you want to label the edges between the 'pixels', you can use the positions -0.5, 0.5, 1.5, ...

import matplotlib.pyplot as plt
import numpy as np

dlon = np.array([4.90148783, 4.91438189, 4.92727594, 4.94017, 4.95306406, 4.96595812])  # longitudes
dlat = np.array([51.81923676, 51.82162018, 51.8240036, 51.82638702, 51.82877045, 51.83115387])  # latitudes
count = np.array([[10., 16., 16., 0., 5.],
                  [0., 0., 0., 5., 0.],
                  [0., 0., 0., 0., 2.],
                  [0., 0., 0., 2., 0.],
                  [12., 0., 6., 13., 13.]])  # number of times a variable is within the gridcell
fig = plt.figure()
ax = fig.add_subplot(111)
# Show all ticks...
ax.set_xticks(np.arange(len(dlon)) - 0.5)
ax.set_yticks(np.arange(len(dlat)) - 0.5)
# .Label them with the respective entries
ax.set_xticklabels(np.around(dlon, decimals=4))
ax.set_yticklabels(np.around(dlat, decimals=4))
im = ax.imshow(count, origin='lower', cmap='plasma')
cbar = fig.colorbar(im)
for i in np.arange(np.shape(count)[0]):  # over all rows of count
    for j in np.arange(np.shape(count)[1]):  # over all cols of count
        text = ax.text(j, i, int(count[i, j]), ha="center", va="center", color="w")
ax.set_title("Counts in bins")
plt.show()

resulting plot

Upvotes: 1

Related Questions