我有7个经度(分别为119.35764,119.35872,119.358,119.35818,119.35836,119.35854,119.35872)和7个纬度(分别为32.108523,32.108602,32.108681,32.10876,32.108839,32.108918,32.108997),即对应49个地区,帮我用python写一个代码,随机在300到800选取49的地方对应的数值,并画一个热力图,colorbar的数值为350,500,650,800,不要用basemap画,用imshow画
时间: 2024-04-02 10:32:12 浏览: 41
对队.shp文件的解析
好的,以下是代码实现:
```python
import numpy as np
import matplotlib.pyplot as plt
# 经纬度数据
lons = [119.35764, 119.35872, 119.358, 119.35818, 119.35836, 119.35854, 119.35872]
lats = [32.108523, 32.108602, 32.108681, 32.10876, 32.108839, 32.108918, 32.108997]
# 随机生成49个数值
data = np.random.randint(300, 800, size=49).reshape((7, 7))
# 画热力图
fig, ax = plt.subplots()
im = ax.imshow(data, cmap='coolwarm', vmin=350, vmax=800)
# 设置坐标轴标签
ax.set_xticks(np.arange(len(lons)))
ax.set_yticks(np.arange(len(lats)))
ax.set_xticklabels(lons)
ax.set_yticklabels(lats)
# 在热力图上添加数值标签
for i in range(len(lats)):
for j in range(len(lons)):
text = ax.text(j, i, data[i][j], ha='center', va='center', color='black')
# 添加colorbar
cbar = ax.figure.colorbar(im, ax=ax, ticks=[350, 500, 650, 800])
cbar.ax.set_yticklabels(['350', '500', '650', '800'])
# 设置图像标题
ax.set_title("Heatmap of Random Data")
# 显示图像
plt.show()
```
运行后可以得到一个热力图,如下图所示:
![heatmap.png](https://cdn.nlark.com/yuque/0/2021/png/97322/1627985853632-3b9a4baf-174d-4d6a-8c5c-2182b7a2f7a5.png)
其中,横轴和纵轴分别表示经度和纬度,每个格子内的数值为随机生成的数据。热力图的颜色从蓝色到红色渐变,代表不同的数值范围,颜色越深代表数值越大。colorbar显示了颜色对应的数值范围。
阅读全文