我正在尝试构造一个简单的函数,它接受一个subplot instance(matplotlib.axes._subplots.AxesSubplot)并将其投影转换为另一个投影,例如,转换为其中一个cartopy.crs.CRS投影.
这个想法看起来像这样
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
def make_ax_map(ax, projection=ccrs.PlateCarree()):
# set ax projection to the specified projection
...
# other fancy formatting
ax2.coastlines()
...
# Create a grid of plots
fig, (ax1, ax2) = plt.subplots(ncols=2)
# the first subplot remains unchanged
ax1.plot(np.random.rand(10))
# the second one gets another projection
make_ax_map(ax2)
Run Code Online (Sandbox Code Playgroud)
当然,我可以使用fig.add_subplot()功能:
fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(121)
ax1.plot(np.random.rand(10))
ax2 = fig.add_subplot(122,projection=ccrs.PlateCarree())
ax2.coastlines()
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有一个适当的matplotlib方法来改变定义后的子图轴投影.不幸的是,阅读matplotlib API没有帮助.
使用Cartopy,我想完全控制我的colorbar去哪里.通常我通过将当前轴位置作为基础然后为颜色条创建新轴来实现此目的.这适用于标准matplotlib轴,但不适用于使用Cartopy和geo_axes时,因为这会扭曲轴.
所以,我的问题是:如何获得geo_axes的确切位置?
以下是基于Cartopy文档http://scitools.org.uk/cartopy/docs/latest/matplotlib/advanced_plotting.html的代码示例:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import os
from netCDF4 import Dataset as netcdf_dataset
from cartopy import config
def main():
fname = os.path.join(config["repo_data_dir"],
'netcdf', 'HadISST1_SST_update.nc'
)
dataset = netcdf_dataset(fname)
sst = dataset.variables['sst'][0, :, :]
lats = dataset.variables['lat'][:]
lons = dataset.variables['lon'][:]
#my preferred way of creating plots (even if it is only one plot)
ef, ax = plt.subplots(1,1,figsize=(10,5),subplot_kw={'projection': ccrs.PlateCarree()})
ef.subplots_adjust(hspace=0,wspace=0,top=0.925,left=0.1)
#get size and extent of axes:
axpos = ax.get_position()
pos_x = axpos.x0+axpos.width + 0.01# …Run Code Online (Sandbox Code Playgroud) 我收到了以下电子邮件,并希望确保每个人都能获得这个问题的答案:
嗨,
我想设置一个简单的纬度经度地图,使用纸板,横跨日期线,并在左侧显示东亚,右侧显示北美西部.以下谷歌地图大致是我所追求的:
https://maps.google.co.uk/?ll=56.559482,-175.253906&spn=47.333523,133.066406&t=m&z=4

这可以用Cartopy完成吗?
我使用MatplotLib和Cartopy生成一些数据图像.问题是,当我将帧大小设置为全屏并使用plt.show()时,图像是完美的,分辨率很好.
但是,当我使用'plt.savefig()'保存此图时,保存的图像保持原始大小(不是全屏).
显示结果图像:


我的代码如下:
def plot_tec_cartopy(descfile):global matrixLon,matrixLat,matrixTec
ax = plt.axes(projection=cartopy.crs.PlateCarree())
v = np.linspace(0, 80, 46, endpoint=True)
cp = plt.contourf(matrixLon, matrixLat, matrixTec, v, cmap=plt.cm.rainbow)
plt.clim(0, 80)
plt.colorbar(cp)
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
ax.set_extent([-85, -30, -60, 15])
# Setting X and Y labels using LON/LAT format
ax.set_xticks([-85, -75, -65, -55, -45, -35])
ax.set_yticks([-60, -55, -50, -45, -40, -35, -30, -25, -20, -15, -10, -5, 0, 5, 10, 15])
lon_formatter = LongitudeFormatter(number_format='.0f',
degree_symbol='',
dateline_direction_label=True)
lat_formatter = LatitudeFormatter(number_format='.0f',
degree_symbol='')
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
plt.title('Conteúdo Eletrônico Total', style='normal', fontsize='12') …Run Code Online (Sandbox Code Playgroud) 我在这里试过这个例子,效果很好.但我如何专注于另一个国家,即只展示德国?
这个例子的来源
http://scitools.org.uk/cartopy/docs/latest/examples/hurricane_katrina.html 我们的示例地图http://scitools.org.uk/cartopy/docs/latest/_images/hurricane_katrina_01_00.png
我在extend()方法上尝试了一些坐标,但我没有设法像美国地图一样.或者我必须修改形状文件?
离线数据存储在纸板中的哪个位置?它是存储在site-packages下的数据文件夹中吗?有没有办法触发下载所有可用数据?我想将其复制到未连接到互联网的Linux机器上.我目前正在使用连接到互联网的Windows机器,所以我希望从那里下载数据.谢谢.
Google通过正常的API访问(即使使用API密钥)也不支持直接获取Google磁贴.因此,cartopy.io.img_tiles.GoogleTiles效果不佳,因为它只能在一次或两次试验中获取磁贴,之后Google会拒绝请求.
将Google地图与Cartopy一起使用的推荐方法是什么?
谢谢
我正在尝试运行一个简单的 Cartopy 示例:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
plt.show()
Run Code Online (Sandbox Code Playgroud)
但我收到这个错误:
几何图形必须是点或线串
python: geos_ts_c.cpp:4179: int GEOSCoordSeq_getSize_r(GEOSContextHandle_t, const geos::geom::CoordinateSequence*, unsigned int*): Assertion0 != cs' failed`。
我使用 miniconda3 安装了 Cartopy:conda install -c conda-forge cartopy
我还尝试使用 pip (在虚拟环境中)安装 Cartopy,但出现了相同的错误。我的操作系统是 Debian Buster。
任何想法?
使用 python3 和 cartopy,有这个代码:
import matplotlib.pyplot as plt
import cartopy
import cartopy.io.shapereader as shpreader
import cartopy.crs as ccrs
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(cartopy.feature.LAND)
ax.add_feature(cartopy.feature.OCEAN)
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle='-', alpha=.5)
ax.add_feature(cartopy.feature.LAKES, alpha=0.95)
ax.add_feature(cartopy.feature.RIVERS)
ax.set_extent([-150, 60, -25, 60])
shpfilename = shpreader.natural_earth(resolution='110m',
category='cultural',
name='admin_0_countries')
reader = shpreader.Reader(shpfilename)
countries = reader.records()
for country in countries:
if country.attributes['SOVEREIGNT'] == "Bulgaria":
ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=(0, 1, 0), label = "A")
else:
ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=(1, 1, 1), label = country.attributes['SOVEREIGNT'])
plt.rcParams["figure.figsize"] = (50,50)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我明白了:
问题: 我应该写什么,以便在保加利亚(或我在 中提到的任何其他国家/地区)上空得到一个红色的“ A ” …
cartopy ×10
python ×8
matplotlib ×6
axes ×1
colorbar ×1
dataframe ×1
geo ×1
numpy ×1
pandas ×1
python-2.7 ×1
python-3.x ×1
subplot ×1