如何移除Cartopy投影外框?投影与经纬度刻度重叠问题求助
问题:移除Cartopy投影的0-1范围外框
运行代码后出现Cartopy投影默认的0-1范围外框与自定义经纬度刻度重叠的问题,需移除该多余外框。
原代码
import matplotlib.pyplot as plt import cartopy.crs as ccrs import cartopy.feature as cfeature import numpy as np import pandas as pd from matplotlib.colors import Normalize from matplotlib.cm import ScalarMappable # 假设years、ca_fires、cmap、norm已定义 fig, ax = plt.subplots(figsize=(10, 8)) projection = ccrs.PlateCarree() ax = plt.axes(projection=projection, frameon = False) ax.add_feature(cfeature.BORDERS, edgecolor='black') ax.add_feature(cfeature.STATES, edgecolor='black') # Set map limits for California ax.set_xlim(-125, -113) # Longitude bounds for California ax.set_ylim(32, 42) # Latitude bounds for California for i, year in enumerate(years): subset = ca_fires[ca_fires["FIRE_YEAR"] == year] color = cmap(norm(year)) # Get color from colormap # Convert GeoDataFrame to arrays for plotting x, y = subset.geometry.x, subset.geometry.y # Get longitude and latitude ax.scatter(x, y, s=5, c=[color], alpha=0.7, transform=projection) ax.set_title("Wildfires in California (Past 10 Years)", fontsize=14) ax.grid(True, linestyle='--') ax.set_xlabel('Longitude') ax.set_ylabel('Latitude') ax.set_xticks([]) # Remove the default ticks ax.set_yticks([]) # Remove the default ticks ax.set_xticks(np.arange(-125, -112, 1)) # Longitude ticks ax.set_yticks(np.arange(32, 43, 1)) # Latitude ticks plt.show()
问题根源
代码开头先通过plt.subplots()创建了一个默认Axes(范围0-1),随后又用plt.axes()重新创建带Cartopy投影的Axes,但原默认Axes未被清除,导致其外框叠加在地图上,出现0-1范围的多余框线。
解决方案
方案1:直接在创建子图时指定投影(推荐)
避免重复创建Axes,在plt.subplots()中通过subplot_kw参数直接设置投影和frameon=False:
import matplotlib.pyplot as plt import cartopy.crs as ccrs import cartopy.feature as cfeature import numpy as np import pandas as pd from matplotlib.colors import Normalize from matplotlib.cm import ScalarMappable # 假设years、ca_fires、cmap、norm已定义 fig, ax = plt.subplots(figsize=(10, 8), subplot_kw={'projection': ccrs.PlateCarree(), 'frameon': False}) ax.add_feature(cfeature.BORDERS, edgecolor='black') ax.add_feature(cfeature.STATES, edgecolor='black') # Set map limits for California ax.set_xlim(-125, -113) # Longitude bounds for California ax.set_ylim(32, 42) # Latitude bounds for California for i, year in enumerate(years): subset = ca_fires[ca_fires["FIRE_YEAR"] == year] color = cmap(norm(year)) # Get color from colormap # Convert GeoDataFrame to arrays for plotting x, y = subset.geometry.x, subset.geometry.y # Get longitude and latitude ax.scatter(x, y, s=5, c=[color], alpha=0.7, transform=ccrs.PlateCarree()) ax.set_title("Wildfires in California (Past 10 Years)", fontsize=14) ax.grid(True, linestyle='--') ax.set_xlabel('Longitude') ax.set_ylabel('Latitude') # 设置刻度时需指定transform,确保刻度对应经纬度 ax.set_xticks(np.arange(-125, -112, 1), transform=ccrs.PlateCarree()) ax.set_yticks(np.arange(32, 43, 1), transform=ccrs.PlateCarree()) plt.show()
方案2:清除原默认Axes
若需保留分步创建逻辑,可在创建新Axes前清除原Axes:
fig, ax = plt.subplots(figsize=(10, 8)) ax.remove() # 清除默认Axes projection = ccrs.PlateCarree() ax = plt.axes(projection=projection, frameon=False) # 后续代码同原代码(注意添加刻度的transform参数)
额外优化点
设置经纬度刻度时必须添加transform=ccrs.PlateCarree(),否则Cartopy会将刻度值视为投影坐标而非地理经纬度,导致刻度位置错误。
内容的提问来源于stack exchange,提问作者Katherine Jeanette Benison
相关产品推荐
相关产品推荐

