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.826
Solution 1:
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()
Post a Comment for "Matplotlib Annotated Heatmaps Formatting"