改变Cartopy边界线宽度

8

我正在尝试制作一个北美地图,但是我无法找到如何使海岸线和行政边界的线条宽度变小的方法。似乎没有内置选项可以实现这一点。有什么简单的解决方法吗?以下是我的代码。

import matplotlib.pyplot as plt
import cartopy.feature as cfeature
import cartopy.crs as ccrs

east = -63
west = -123
north = 55
south = 20
fig = plt.figure()
ax=fig.add_subplot(1,1,1,projection=ccrs.AlbersEqualArea)          
ax.set_extent([west, east, south, north])
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.LAND,color='grey')
ax.add_feature(cfeature.BORDERS)
ax.add_feature(cfeature.COASTLINE)
2个回答

9
这些线条的宽度(或任何由add_feature()方法添加的线条)可以使用linewidth关键字进行控制。默认线宽为1,使用较小的数字将产生更细的线条:
ax.add_feature(cfeature.BORDERS, linewidth=0.5)

谢谢。但是,我已经尝试过这个方法,它对行宽没有影响。我尝试了linewidth = 100和linewidth = 0.001,但似乎都没有效果。也许这是一个渲染问题? - jtam
我测试过了,在qt matplotlib后端中它确实有效,但可能在某些matplotlib后端上无法正确工作。如果是这种情况,最好生成一个简单的可重现的图形,甚至不使用cartopy来检查您是否可以在使用的任何后端中设置matplotlib线的宽度。 - ajdawson
感谢您的帮助! - jtam
@jtam请在答案有效时“接受”它。谢谢。 - pelson

1
您的地图轮廓是 ax.outline_patch 方法的一部分。如此处所述:

https://scitools.org.uk/cartopy/docs/latest/matplotlib/geoaxes.html

https://scitools.org.uk/cartopy/docs/latest/matplotlib/geoaxes.html#cartopy.mpl.geoaxes.GeoAxes.outline_patch

https://github.com/SciTools/cartopy/issues/1077

使用修改版的:

https://matplotlib.org/api/_as_gen/matplotlib.patches.Patch.html#matplotlib.patches.Patch

工作代码示例:

import matplotlib.pyplot as plt
import cartopy.feature as cfeature
import cartopy.crs as ccrs

east = -63
west = -123
north = 55
south = 20
fig = plt.figure()
ax = plt.subplot(1,1,1, projection=ccrs.AlbersEqualArea())          
ax.set_extent([west, east, south, north])
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.LAND,color='grey')
ax.add_feature(cfeature.BORDERS)
ax.add_feature(cfeature.COASTLINE)

#Add your line modifications here
ax.outline_patch.set_linewidth(5)
ax.outline_patch.set_linestyle(':')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接