不同cartopy投影的颜色条大小

问题描述 投票:0回答:1

我正在将数据绘制到 PlateCarree 或 Mercator 投影上,侧面有一个颜色条。如果我使用墨卡托投影,则颜色条与图形的高度很好地对齐,但如果我使用板卡雷投影,则颜色条与图形的高度不对齐。我怎样才能实现独立于地图投影的自动化?

这是我的代码:

fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())

ax.set_extent([6, 44, 55, 72], crs=ccrs.PlateCarree())
ax.coastlines(linewidth=.5)
ax.add_feature(cfeature.LAND, color='white', alpha=.15)
ax.add_feature(cfeature.BORDERS, linestyle='-', linewidth=.6)

# data
dataset = rasterio.open(fpath)
dbz = dataset.read()

# Plotting geoTIFF-Data
extent = (dataset.bounds.left, dataset.bounds.right, dataset.bounds.bottom, dataset.bounds.top)
boundaries = [-10,-5,0,5,7.5,10,12.5,15,20,25,30,35,40,45,50]
colors = ('#FF000000', '#c4c4e4','#8989c9','#4e4eaf','#141494','#272776','#626256','#9d9d35','#d8d815','#f6eb00','#dbb100','#c17600','#a63b00','#8b0000')
cmap = mcolors.ListedColormap(colors)
norm = mcolors.BoundaryNorm(boundaries, cmap.N)

im = ax.imshow(dbz_reshaped, extent=extent, cmap=cmap, norm=norm, transform=ccrs.epsg(9391))
fig.colorbar(im, ax=ax, orientation='vertical', label='DBZ')

不同的数字看起来像:

python matplotlib cartopy
1个回答
1
投票
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import rasterio

# Load your data
dataset = rasterio.open(fpath)
dbz = dataset.read()

# Create the figure and axis
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())

# Set the extent and plot features
ax.set_extent([6, 44, 55, 72], crs=ccrs.PlateCarree())
ax.coastlines(linewidth=.5)
ax.add_feature(cfeature.LAND, color='white', alpha=.15)
ax.add_feature(cfeature.BORDERS, linestyle='-', linewidth=.6)

# Plot the data
extent = (dataset.bounds.left, dataset.bounds.right, dataset.bounds.bottom, dataset.bounds.top)
boundaries = [-10,-5,0,5,7.5,10,12.5,15,20,25,30,35,40,45,50]
colors = ('#FF000000', '#c4c4e4','#8989c9','#4e4eaf','#141494','#272776','#626256','#9d9d35','#d8d815','#f6eb00','#dbb100','#c17600','#a63b00','#8b0000')
cmap = mcolors.ListedColormap(colors)
norm = mcolors.BoundaryNorm(boundaries, cmap.N)
im = ax.imshow(dbz_reshaped, extent=extent, cmap=cmap, norm=norm, transform=ccrs.epsg(9391))

# Adjust colorbar size relative to figure height
divider = make_axes_locatable(ax)
cax = divider.new_horizontal(size="5%", pad=0.05, axes_class=plt.Axes)
fig.add_axes(cax)
cbar = fig.colorbar(im, cax=cax, orientation='vertical', label='DBZ')

plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.