【问题标题】:Changing line width of Cartopy borders更改 Cartopy 边框的线宽
【发布时间】:2017-09-26 01:02:12
【问题描述】:

我正在尝试制作北美的基本地图,但我不知道如何使海岸线的线宽和行政边界更小。似乎没有内置选项。有一个简单的解决方法吗?以下是我到目前为止的代码。

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)

【问题讨论】:

    标签: matplotlib gis cartopy


    【解决方案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(':')
    

    【讨论】:

      【解决方案2】:

      可以使用linewidth 关键字控制这些行(或add_feature() 方法添加的任何行)的宽度。默认线宽为 1,使用较小的数字会产生更细的线条:

      ax.add_feature(cfeature.BORDERS, linewidth=0.5)
      

      【讨论】:

      • 谢谢。但是,我已经尝试过了,它对线宽没有影响。我尝试了 linewidth = 100 和 linewidth = 0.001,但似乎没有任何效果。可能是渲染问题?
      • 我对此进行了测试,它至少可以在 qt matplotlib 后端运行,但我想它可能无法在某些 matplotlib 后端正常运行。在这种情况下,您最好生成一个简单的可重现图,甚至不使用 cartopy 来检查您是否可以在您使用的任何后端设置 matplotlib 行的宽度。
      • 感谢您的帮助!
      • @jtam 如果对您有用,请“接受”答案。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2020-06-16
      • 2017-12-29
      • 2016-07-25
      • 2013-09-25
      • 2021-12-30
      • 1970-01-01
      • 2016-05-27
      • 2023-01-24
      相关资源
      最近更新 更多