小编yng*_*waz的帖子

正确放置颜色条相对于地理轴(cartopy)

使用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)

python axes matplotlib colorbar cartopy

16
推荐指数
2
解决办法
4355
查看次数

Python matplotlib:数据坐标中的位置颜色条

我想通过指定数据坐标中的位置将颜色条放在散点图中.以下是指定图形坐标时的工作原理示例:

import numpy as np
import matplotlib.pyplot as plt    

#Generate some random data:
a = -2
b = 2
x = (b - a) * np.random.random(50) + a
y = (b - a) * np.random.random(50) + a
z = (b) * np.random.random(50)

#Do a scatter plot
fig = plt.figure()
hdl = plt.scatter(x,y,s=20,c=z,marker='o',vmin=0,vmax=2)
ax = plt.gca()
ax.set_xlim([-2,2])
ax.set_ylim([-2,2])

#Specifying figure coordinates works fine:
fig_coord = [0.2,0.8,0.25,0.05]
cbar_ax = fig.add_axes(fig_coord)

clevs = [0, 1 , 2]
cb1 = plt.colorbar(hdl, cax=cbar_ax, orientation='horizontal', ticks=clevs) …
Run Code Online (Sandbox Code Playgroud)

python matplotlib colorbar

6
推荐指数
1
解决办法
2522
查看次数

Cartopy:轴标签 - 解决方法

我正在寻找一种解决方法,在Lambert投影中将x和y轴刻度和标签添加到Cartopy贴图中.

我提出的解决方案只是一个近似值,对于较大的地图会产生更糟糕的结果:它涉及使用transform_points方法将所需的刻度位置转换为地图投影.为此,我使用y轴(或x轴)的中位数经度(或纬度)以及所需的纬度(或经度)刻度位置来计算地图投影坐标.见下面的代码.

因此,我假设沿y轴的恒定经度(沿x轴的纬度),这是不正确的,因此导致偏差.(注意附加结果图中的差异:在set_extent中设置46°并产生刻度位置).

有没有更精确的解决方案?有什么提示我怎么能解决这个问题呢?

感谢任何想法!

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import numpy as np

def main():
    #my desired Lambert projection:
    myproj = ccrs.LambertConformal(central_longitude=13.3333, central_latitude=47.5,
                                   false_easting=400000, false_northing=400000,
                                   secant_latitudes=(46, 49))

    arat = 1.1 #just some factor for the aspect ratio
    fig_len = 12
    fig_hig = fig_len/arat
    fig = plt.figure(figsize=(fig_len,fig_hig), frameon=True)
    ax = fig.add_axes([0.08,0.05,0.8,0.94], projection = myproj)

    ax.set_extent([10,16,46,49])
    #This is what is not (yet) working in Cartopy due to Lambert projection:
    #ax.gridlines(draw_labels=True) #TypeError: Cannot label gridlines on a LambertConformal plot. …
Run Code Online (Sandbox Code Playgroud)

python matplotlib cartopy

5
推荐指数
1
解决办法
2069
查看次数

用 cartopy 制作透明图

我想用 Cartopy 创建一个透明地图(然后可以用作 Web 应用程序的叠加层)。

曾尝试使用多种设置、投影、绘图类型等,但从未设法获得透明的 Cartopy 地图。

这是一个简单的例子,说明透明度如何在 Cartopy 中不起作用,以及它如何在不使用 Cartopy 的情况下工作。

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np

#some data:
shape=(20, 30)
scale = 30
x = np.linspace(-scale, scale, shape[1])
y = np.linspace(-scale, scale, shape[0])

x2d, y2d = np.meshgrid(x, y)
u = 10 * np.cos(2 * x2d / scale + 3 * y2d / scale)
v = 20 * np.cos(6 * x2d / scale)

#cartopy
ef, ax = plt.subplots(1,1,figsize=(10,8), subplot_kw={'projection': ccrs.GOOGLE_MERCATOR})    
ef.subplots_adjust(hspace=0.0,wspace=0,bottom=0,top=1,left=0,right=1)

ax.quiver(x2d,y2d, …
Run Code Online (Sandbox Code Playgroud)

python transparency matplotlib cartopy

5
推荐指数
1
解决办法
1725
查看次数

Python cartopy 地图,国外的剪辑区域(多边形)

我创建了一个带有来自 NaturalEarth 的国家边界的雄蕊地形图。现在我想从国家边界之外删除所有数据(在这种情况下是地形)。我该怎么做?

我在瑞士境内外可见地形的示例:

from cartopy.io import shapereader
import cartopy.io.img_tiles as cimgt
import cartopy.crs as ccrs
import geopandas
import matplotlib.pyplot as plt


resolution = '10m'
category = 'cultural'
name = 'admin_0_countries'

shpfilename = shapereader.natural_earth(resolution, category, name)

df = geopandas.read_file(shpfilename)

poly = [df.loc[df['ADMIN'] == 'Switzerland']['geometry'].values[0]]

stamen_terrain = cimgt.Stamen('terrain-background')

fig = plt.figure(figsize=(8,6))

ax = fig.add_subplot(1, 1, 1, projection=stamen_terrain.crs)
ax.add_geometries(poly, crs=ccrs.PlateCarree(), facecolor='none', edgecolor='r')
exts = [poly[0].bounds[0], poly[0].bounds[2], poly[0].bounds[1], poly[0].bounds[3]]
ax.set_extent(exts, crs=ccrs.Geodetic())

ax.add_image(stamen_terrain, 8)
fig.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我怎样才能只显示边界内的地形,而其余的则显示为白色/透明?

尝试朝这个方向发展,但到目前为止没有成功:https : //scitools.org.uk/cartopy/docs/latest/gallery/logo.html

python dictionary clip cartopy

4
推荐指数
1
解决办法
1051
查看次数

标签 统计

python ×5

cartopy ×4

matplotlib ×4

colorbar ×2

axes ×1

clip ×1

dictionary ×1

transparency ×1