问题描述
我已经计算了纬度/经度箱的样本:
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()
我有两个问题:1) 在热图中,y 轴上的纬度顺序相反.如何翻转它们以使最大值在左上角结束?2)我有一个 5x5 的网格(5 行和 5 列计数),纬度/经度的边界是 6x6.现在它显示了网格点的中间,但是我想显示网格的角.我该怎么做?
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?
推荐答案
imshow(..., origin='lower')
在左下方设置原点.请注意,图像通常从顶部开始,因此如果您需要更类似于 xy 图的内容,则必须明确设置原点.
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.
刻度的轴为像素"的中心为 0,1,2.如果要标记像素"之间的边缘,可以使用位置 -0.5, 0.5, 1.5, ...
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()
这篇关于Matplotlib 注释热图格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!