小编Ian*_*ole的帖子

Python:使用多边形在给定的2d网格上创建蒙版

我有一些多边形(加拿大省份),读入GeoPandas,并希望使用这些来创建一个掩码,以应用于二维纬度 - 经度网格上的网格化数据(使用netcdf文件读取iris).最终目标是仅保留给定省份的数据,其余数据被掩盖.因此,对于省内的网格框,掩码为1,对于省外的网格框,为0或NaN.


可以从shapefile中获取多边形:https://www.dropbox.com/s/o5elu01fetwnobx/CAN_adm1.shp? dl = 0

我正在使用的netcdf文件可以在这里下载:https://www.dropbox.com/s/kxb2v2rq17m7lp7/t2m.20090815.nc?dl =0


我想这里有两种方法,但我在两个方面都在努力:

1)使用多边形在纬度 - 经度网格上创建一个蒙版,以便这可以应用于python之外的许多数据文件(首选)

2)使用多边形来屏蔽已读入的数据并仅提取感兴趣的省内的数据,以交互方式工作.

我的代码到目前为止:

import iris
import geopandas as gpd

#read the shapefile and extract the polygon for a single province
#(province names stored as variable 'NAME_1')
Canada=gpd.read_file('CAN_adm1.shp')
BritishColumbia=Canada[Canada['NAME_1'] == 'British Columbia']

#get the latitude-longitude grid from netcdf file
cubelist=iris.load('t2m.20090815.nc')
cube=cubelist[0]
lats=cube.coord('latitude').points
lons=cube.coord('longitude').points

#create 2d grid from lats and lons (may not be necessary?) …
Run Code Online (Sandbox Code Playgroud)

python gis cartopy python-iris geopandas

5
推荐指数
2
解决办法
2618
查看次数

熊猫散点图:如何制作未填充的符号

我正在尝试绘制未填充的符号,但是matplotlib facecolors ='none'参数似乎改变了填充颜色。注意,我正在使用matplotlib样式'ggplot'。

带有填充符号的原始代码段:

ax=df.plot(kind='scatter', x='clay', y='vf1',xlim=[0,0.6],ylim=[0,6.0e-06],
            color='DarkBlue', s=40, label='u*=1, w=0') 
Run Code Online (Sandbox Code Playgroud)

原始的填充轮廓

现在尝试使用facecolors ='none'(并将edgecolors设置为我想要的颜色)

ax=df.plot(kind='scatter', x='clay', y='vf1',xlim=[0,0.6],ylim=[0,6.0e-06],
            facecolors='none', edgecolors='DarkBlue', s=40, label='u*=1, w=0')     
Run Code Online (Sandbox Code Playgroud)

试图填补轮廓

事实证明,无论我为“ facecolors”输入哪种颜色,阴影始终为浅蓝色。有人知道这是怎么回事,我怎么能真正得到一个没有阴影的符号?

***更新****在下面的BrenBarn回答之后,我有以下内容,这正是我想要的。

ax=df.plot(kind='scatter', x='clay', y='vf1',xlim=[0,0.6],ylim=[0,6.0e-06],
            c='none', edgecolor='DarkBlue', s=40, label='u*=1, w=0')
Run Code Online (Sandbox Code Playgroud)

完善!

python matplotlib pandas

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

熊猫散点图:如何在辅助 y 轴上绘制数据?

我想将两个散点图放在同一个图上,其中 x 轴是共享的,但 y 轴是不同的。我不知道如何到达那里。要创建我的第一个散点图,我正在做:

ax=df.plot(kind='scatter', x='vf', y='hf_ratio', xlim=[2e-06,6e-06], ylim=[1,10],
        color='DarkBlue', s=40, label='a') 
ax.ticklabel_format(axis='x', style='sci', scilimits=(0,0))
Run Code Online (Sandbox Code Playgroud)

simple_scatterplot

现在我想在右手 y 轴上添加第二个。Pandas 文档提供了将辅助轴添加到线图 ( secondary_y=True) 的信息,因此我尝试过此操作,但似乎不起作用:

df.plot(kind='scatter', x='vf', y='hfall', secondary_y=True,
        color='Red', s=40, label='hfall', ax=ax);
Run Code Online (Sandbox Code Playgroud)

次要_y_失败

它似乎忽略了该secondary_y=True命令,只是在原始 y 轴上绘图,这不是很有用。只是为了在我的伤口上撒更多盐,它还去除了迷人的白色网格线......

如果有人能够对此提供帮助,将不胜感激。

python matplotlib pandas

3
推荐指数
1
解决办法
4311
查看次数

标签 统计

python ×3

matplotlib ×2

pandas ×2

cartopy ×1

geopandas ×1

gis ×1

python-iris ×1