我试图将我的源代码保持在PEP8建议的80字符指南宽度之下,但是无法弄清楚如何包装结果长于80个字符的doctest.
一个愚蠢的例子:
def long_string():
"""
Returns a string which is wider than the recommended PEP8 linewidth
>>> print long_string()
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
"""
return '0123456789' * 10
Run Code Online (Sandbox Code Playgroud)
我尝试了几种组合,包括使用# doctest: +NORMALIZE_WHITESPACE
和尝试用换行符简单地换行.
我收到了以下电子邮件,并希望确保每个人都能获得这个问题的答案:
嗨,
我想设置一个简单的纬度经度地图,使用纸板,横跨日期线,并在左侧显示东亚,右侧显示北美西部.以下谷歌地图大致是我所追求的:
https://maps.google.co.uk/?ll=56.559482,-175.253906&spn=47.333523,133.066406&t=m&z=4
这可以用Cartopy完成吗?
在matplotlib中绘制小补丁对象时,由于显示分辨率而引入了伪像.使用抗锯齿并不能解决问题.
有这个问题的解决方案吗?
import matplotlib.pyplot as plt
import matplotlib.patches as patches
ax = plt.axes()
for x in range(-10,11):
for y in range(-10,11):
rect = patches.Rectangle((x, y), width=0.1, height=0.1, color='k',aa=True)
ax.add_patch(rect)
plt.xlim([-30, 30])
plt.ylim([-30, 30])
plt.show()
Run Code Online (Sandbox Code Playgroud)
代码优先:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
ax = plt.axes(projection=ccrs.Mercator())
ax.set_extent([72, 135, 18, 53])
ax.annotate('hello', xy=(100, 49), xycoords='data',
transform=ccrs.PlateCarree(), zorder=12)
plt.show()
Run Code Online (Sandbox Code Playgroud)
结果不是预期的结果,我对我的方法有其他怀疑.所以我的问题是:
如果我想绘制地图看起来像网络地图(例如谷歌地图).地图区域可能与中国一样大,大部分都不是全球性的.谷歌搜索后,这些网站大多使用"网络墨卡托"投影.所以我想我应该plt.axes(projection=ccrs.Mercator()
在这里使用,我是对的吗?或者如果我错了我该怎么用?
我想绘制的坐标数据类似于121°E,49°N(在绘制航线之前将度数转换为十进制),未投影,WGS84坐标系统,可能来自GPS.我有权使用transform=ccrs.PlateCarree()
吗?或者如果我错了我该怎么用?
在annotate
上述说明不了什么.在对该ax.set_extent
行进行注释之后,将"hello"文本绘制在零(0,0)点处.我想要的是在点(100°E,49°N)如何纠正这个?
我真的希望这是一个简单的例子,让我想念复杂的Python2导入机制.我有以下设置:
$> ls -ltr pypackage1
total 3
-rw-r--r-- 1 pelson pelson 0 Aug 17 19:20 io.py
-rw-r--r-- 1 pelson pelson 0 Aug 17 19:20 __init__.py
-rw-r--r-- 1 pelson pelson 57 Aug 17 19:22 code.py
$> cat pypackage1/code.py
from __future__ import absolute_import
import zipfile
Run Code Online (Sandbox Code Playgroud)
即我只是一个存根包空__init__.py
和io.py
和2号线code.py
的文件.
我可以导入pypackage1
:
$> python -c "import pypackage1.code"
Run Code Online (Sandbox Code Playgroud)
但我无法运行该code.py
文件:
$> python pypackage1/code.py
Traceback (most recent call last):
File "pypackage1/code.py", line 3, in <module>
import zipfile
File "python2.7/zipfile.py", line …
Run Code Online (Sandbox Code Playgroud) 我有一个类,其中包含一个itertools.cycle
我希望能够复制的实例.一种方法(我能想出的唯一方法)是提取初始可迭代(这是一个列表),并存储循环所处的位置.
不幸的是我无法掌握我用来创建循环实例的列表,似乎也没有明显的方法来做到这一点:
import itertools
c = itertools.cycle([1, 2, 3])
print dir(c)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__',
'__hash__', '__init__', '__iter__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', 'next']
Run Code Online (Sandbox Code Playgroud)
我可以提出一些合理的理由,为什么对某些类型的输入迭代不允许这样做,但是对于一个元组甚至一个列表(可变性可能是一个问题),我不明白为什么它不会是可能.
任何人都知道是否有可能从itertools.cycle
实例中提取非无限可迭代.如果没有,任何人都知道为什么这个想法很糟糕?
我有一些多边形(加拿大省份),读入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) 我正在使用cartopy来生成具有立体投影的北极地图,然后在顶部绘制一条线(以显示横截面的位置).如果我使用下面的代码,那么这条线不是直线穿过极点,而是沿着一条纬线.
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
x=[180,0]
y=[50,50]
ax = plt.axes(projection=ccrs.NorthPolarStereo())
ax.set_extent([0, 360, 50, 90], crs=ccrs.PlateCarree())
ax.plot(x,y,transform=ccrs.PlateCarree())
plt.gca().stock_img()
plt.gca().coastlines()
plt.show()
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题,我必须将x和y更改为:
x=[180,180,0,0]
y=[50,90,90,50]
Run Code Online (Sandbox Code Playgroud)
所以北极有两个数据点.有更好的解决方案吗?
编辑:附加图像
谢谢,
蒂姆
我想用一种颜色绘制中国的轮廓,同时在另一种颜色中显示全球海岸线.我这样做的第一次尝试如下:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as feature
import cartopy.io.shapereader as shapereader
countries = shapereader.natural_earth(resolution='110m',
category='cultural',
name='admin_0_countries')
# Find the China boundary polygon.
for country in shapereader.Reader(countries).records():
if country.attributes['su_a3'] == 'CHN':
china = country.geometry
break
else:
raise ValueError('Unable to find the CHN boundary.')
plt.figure(figsize=(8, 4))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_extent([50, 164, 5, 60], ccrs.PlateCarree())
ax.add_feature(feature.LAND)
ax.add_feature(feature.OCEAN)
ax.add_feature(feature.COASTLINE, linewidth=4)
ax.add_geometries([china], ccrs.Geodetic(), edgecolor='red',
facecolor='none')
plt.show()
Run Code Online (Sandbox Code Playgroud)
我把海岸线做得很厚,这样你就可以看到它们与国界重叠.
我的问题是:有没有办法去除国家大纲旁边的海岸线,这样我就没有两条线在视觉上相互交互?
注意:这个问题是通过电子邮件直接向我询问的,我选择在此发布我的回复,以便其他人可以从解决方案中学习/受益.
我正在尝试使用EuroPP()
cartopy中的投影来编辑地图的边界.我写了以下代码,但希望放大斯堪的纳维亚地区.有什么建议?谢谢!
import matplotlib.pyplot as plt
%matplotlib inline
import cartopy
import cartopy.crs as ccrs
import cartopy.io.img_tiles as cimgt
plt.figure(figsize=(5, 6))
ax = plt.axes(projection=ccrs.EuroPP())
ax.coastlines(resolution='50m', linewidth = 0.5)
ax.gridlines()
ax.add_feature(cartopy.feature.BORDERS, linestyle='-', alpha=.5)
ax.add_feature(cartopy.feature.OCEAN)
tile = cimgt.StamenTerrain()
ax.add_image(tile,5)
plt.show()
Run Code Online (Sandbox Code Playgroud)
代码生成以下图像:
cartopy ×6
python ×6
matplotlib ×4
python-2.7 ×2
doctest ×1
geopandas ×1
gis ×1
pep8 ×1
python-iris ×1
shapely ×1