Python 如何在Cartopy中制作热图
我正在映射纬度、经度,然后在cartopy上映射一个单独的值 我如何使点的颜色像一个热图的基础上的名单称为klist?我找不到任何可以与cartopy一起使用的代码片段 该列表有一系列的值,我希望根据值的大小对其进行着色Python 如何在Cartopy中制作热图,python,python-3.x,matplotlib,scientific-computing,cartopy,Python,Python 3.x,Matplotlib,Scientific Computing,Cartopy,我正在映射纬度、经度,然后在cartopy上映射一个单独的值 我如何使点的颜色像一个热图的基础上的名单称为klist?我找不到任何可以与cartopy一起使用的代码片段 该列表有一系列的值,我希望根据值的大小对其进行着色 # Define a Cartopy 'ordinary' lat-lon coordinate reference system. crs_latlon = ccrs.PlateCarree() def make_plot(projection_name, project
# Define a Cartopy 'ordinary' lat-lon coordinate reference system.
crs_latlon = ccrs.PlateCarree()
def make_plot(projection_name, projection_crs):
ax = plt.axes(projection=projection_crs)
# Set display limits to include a set region of latitude * longitude.
# (Note: Cartopy-specific).
ax.set_extent((-65.0, -62, 44, 45.5), crs=crs_latlon)
# Add coastlines and meridians/parallels (Cartopy-specific).
ax.coastlines(linewidth=0.2, color='black')
ax.gridlines(crs=crs_latlon, linestyle='-')
# Mark some particular places with a small circle and a name label...
# Define some test points with latitude and longitude coordinates.
#city_data = [('Halifax, NS', 44.67, -63.61)]
plt.plot(lon,lat,marker='x', markersize=1.0, markeredgewidth=2.5,
markerfacecolor='black',
transform=crs_latlon)
# Add a title, and display.
iplt.show("Mission #1: Attenuation Coeffiecient")
def main():
# Demonstrate with two different display projections.
make_plot('Equidistant Cylindrical', ccrs.PlateCarree())
if __name__ == '__main__':
main()
如果您需要不同颜色的点,这可能会对您有所帮助(基于您的代码):
我对制图了解不够,无法理解为什么两个投影中的点位于不同的位置,但也许您知道这意味着什么以及如何更正它。如果您想要不同颜色的点,这可能会帮助您(根据您的代码):
我对地图学了解不够,无法理解为什么两个投影中的点位于不同的位置,但也许你知道这意味着什么以及如何更正它。从我所看到的,你可以像在plain
matplotlib
中生成热图一样生成热图。只需使用pcolormesh
(或pcolor
或任何东西)并使用正确定义的网格网格。在这里,我修改了@berna1111的答案,以生成一个彩色地图,而不是在地图上画圆圈
为了避免在海岸线之外绘制,可以使用或,尽管前者可能是最好的
在下面的示例中,我提供了一个heat_data
,它是一个numpy数组,其中包含将进行颜色编码的数据。为了方便起见,我假设该数据是在整个地图范围内定义的。您的数据可能不同。
因为我没有实际的数据,所以我根据heat_data
的范围和大小创建lat
和lon
数组。在main()
将cartopy.crs导入为CCR
将matplotlib.pyplot作为plt导入
将numpy作为np导入
def make_绘图(投影名称、投影crs、范围、热量数据):
"""
?
"""
图=plt.图()
rect=0.1,0.1,0.8,0.8
ax=图添加轴(矩形,投影=投影)
#设置显示限制以包括设置的纬度*经度区域。
#(注:纸箱专用)。
最大设置范围(范围,crs=投影范围)
#添加海岸线和子午线/平行线(特定于Cartopy)。
最大海岸线(线宽=0.2,颜色=黑色)
最大网格线(crs=投影,线型='-')
lat=np.linspace(范围[0],范围[1],热数据.shape[0])
lon=np.linspace(区段[2],区段[3],热量数据.shape[1])
Lat,Lon=np.meshgrid(Lat,Lon)
ax.pcolormesh(Lat、Lon、np.transpose(heat_数据))
plt.savefig(“Test_fig.pdf”,bbox_inches='tight')
def main():
#范围=(-65.0,-62,44,45.5)
范围=(-90,-40,30,60)
#使用纬度和经度坐标定义一些测试点。
#城市大学数据=[('Halifax,NS',44.67,-63.61,'black'),
#(“邻居”,45,-63,“蓝色”),
#(“其他地方”,44.1,-64,“红色”)]
热数据=np.随机.正常(0.0,0.2,尺寸=(100150))
#使用两个不同的显示投影进行演示。
#定义Cartopy“普通”横向坐标参考系。
crs_latlon=ccrs.PlateCarree()
绘制图(“等距圆柱”,crs坐标,范围,热量数据)
#crs_ae=ccrs.lambert()
#绘制图(“兰伯特圆柱”,crs\U ae,范围,热量数据)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
从我所看到的情况来看,生成热图的方法与在plainmatplotlib
中生成热图的方法相同。只需使用pcolormesh
(或pcolor
或任何东西)并使用正确定义的网格网格。在这里,我修改了@berna1111的答案,以生成一个彩色地图,而不是在地图上画圆圈
为了避免在海岸线之外绘制,可以使用或,尽管前者可能是最好的
在下面的示例中,我提供了一个heat_data
,它是一个numpy数组,其中包含将进行颜色编码的数据。为了方便起见,我假设该数据是在整个地图范围内定义的。您的数据可能不同。
因为我没有实际的数据,所以我根据heat_data
的范围和大小创建lat
和lon
数组。在main()
将cartopy.crs导入为CCR
将matplotlib.pyplot作为plt导入
将numpy作为np导入
def make_绘图(投影名称、投影crs、范围、热量数据):
"""
?
"""
图=plt.图()
rect=0.1,0.1,0.8,0.8
ax=图添加轴(矩形,投影=投影)
#设置显示限制以包括设置的纬度*经度区域。
#(注:纸箱专用)。
最大设置范围(范围,crs=投影范围)
#添加海岸线和子午线/平行线(特定于Cartopy)。
最大海岸线(线宽=0.2,颜色=黑色)
最大网格线(crs=投影,线型='-')
lat=np.linspace(范围[0],范围[1],热数据.shape[0])
lon=np.linspace(区段[2],区段[3],热量数据.shape[1])
Lat,Lon=np.meshgrid(Lat,Lon)
ax.pcolormesh(Lat、Lon、np.transpose(heat_数据))
plt.savefig(“Test_fig.pdf”,bbox_inches='tight')
def main():
#范围=(-65.0,-62,44,45.5)
范围=(-90,-40,30,60)
#使用纬度和经度坐标定义一些测试点。
#城市大学数据=[('Halifax,NS',44.67,-63.61,'black'),
#(“邻居”,45,-63,“蓝色”),
#(“其他地方”,44.1,-64,“红色”)]
热数据=np.随机.正常(0.0,0.2,尺寸=(100150))
#使用两个不同的显示投影进行演示。
#定义Cartopy“普通”横向坐标参考系。
crs_latlon=ccrs.PlateCarree()
绘制图(“等距圆柱”,crs坐标,范围,热量数据)
#crs_ae=ccrs.lambert()
#绘制图(“兰伯特圆柱”,crs\U ae,范围,热量数据)
我
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
def make_plot(projection_name, projection_crs, extent, city_data):
"""
?
"""
fig = plt.figure()
rect = 0.1, 0.1, 0.8, 0.8
ax = fig.add_axes(rect, projection=projection_crs)
# Set display limits to include a set region of latitude * longitude.
# (Note: Cartopy-specific).
ax.set_extent(extent, crs=projection_crs)
# Add coastlines and meridians/parallels (Cartopy-specific).
ax.coastlines(linewidth=0.2, color='black')
ax.gridlines(crs=projection_crs, linestyle='-')
# Mark some particular places with a small circle and a name label...
for city in city_data:
ax.plot(city[2], city[1], marker='o',
markersize=2.0, markeredgewidth=1.0,
markeredgecolor=city[3], markerfacecolor=city[3],
linestyle='None', label=city[0], transform=projection_crs)
# Add a title, legend, and display.
ax.set_title(''.join(("Mission #1: Attenuation Coeffiecient - ",
projection_name)))
ax.legend()
fig.show()
def main():
#extent = (-65.0, -62, 44, 45.5)
extent = (-90, -40, 30, 60)
# Define some test points with latitude and longitude coordinates.
city_data = [('Halifax, NS', 44.67, -63.61, 'black'),
('Neighbour', 45, -63, 'blue'),
('Other_Place', 44.1, -64, 'red')]
# Demonstrate with two different display projections.
# Define a Cartopy 'ordinary' lat-lon coordinate reference system.
crs_latlon = ccrs.PlateCarree()
make_plot('Equidistant Cylindrical', crs_latlon, extent, city_data)
crs_ae = ccrs.LambertCylindrical()
make_plot('Lambert Cylindrical', crs_ae, extent, city_data)
if __name__ == '__main__':
main()