goeopandas情节形状并应用不透明的外部形状

发布于 2025-01-21 03:26:57 字数 747 浏览 1 评论 0原文

我正在绘制城市边界(geopandas 数据框),并使用 contextily 添加了底图。 我想对城市范围之外的地图区域应用不透明度。 下面的示例显示了与预期效果相反的效果,因为不透明度应该应用于除城市范围之外的任何地方。

import osmnx as ox
import geopandas as gpd
import contextily as cx

berlin = ox.geocode_to_gdf('Berlin,Germany')
fig, ax = plt.subplots(1, 1, figsize=(10,10))
_ = ax.axis('off')

berlin.plot(ax=ax,
            color='white',
            edgecolor='black',
            alpha=.7,
           )

# basemap 
cx.add_basemap(ax,crs=berlin.crs,)

plt.savefig('stackoverflow_question.png',
            dpi=100,
            bbox_inches='tight',
           )

显示与期望结果相反的图:
输入图片此处描述

I am plotting a city boundary (geopandas dataframe) to which I added a basemap using contextily.
I would like to apply opacity to the region of the map outside of the city limits.
The below example shows the opposite of the desired effect, as the opacity should be applied everywhere except whithin the city limits.

import osmnx as ox
import geopandas as gpd
import contextily as cx

berlin = ox.geocode_to_gdf('Berlin,Germany')
fig, ax = plt.subplots(1, 1, figsize=(10,10))
_ = ax.axis('off')

berlin.plot(ax=ax,
            color='white',
            edgecolor='black',
            alpha=.7,
           )

# basemap 
cx.add_basemap(ax,crs=berlin.crs,)

plt.savefig('stackoverflow_question.png',
            dpi=100,
            bbox_inches='tight',
           )

Plot showing opposite of desired result:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇妻 2025-01-28 03:26:57

您可以创建一个新的多边形,该多边形是几何形状总界限的缓冲区,减去几何

import osmnx as ox
import geopandas as gpd
import contextily as cx
import matplotlib.pyplot as plt
from shapely.geometry import box


berlin = ox.geocode_to_gdf("Berlin,Germany")
notberlin = gpd.GeoSeries(
    [
        box(*box(*berlin.total_bounds).buffer(0.1).bounds).difference(
            berlin["geometry"].values[0]
        )
    ],
    crs=berlin.crs,
)


fig, ax = plt.subplots(1, 1, figsize=(10, 10))
_ = ax.axis("off")

notberlin.plot(
    ax=ax,
    color="white",
    edgecolor="black",
    alpha=0.7,
)

# basemap
cx.add_basemap(
    ax,
    crs=berlin.crs,
)

# plt.savefig('stackoverflow_question.png',
#             dpi=100,
#             bbox_inches='tight',
#            )

“ https://i.sstatic.net/rstpa.jpg” alt =“在此处输入图像说明”>

You can create a new polygon that is a buffer on the total bounds of your geometry minus your geometry

import osmnx as ox
import geopandas as gpd
import contextily as cx
import matplotlib.pyplot as plt
from shapely.geometry import box


berlin = ox.geocode_to_gdf("Berlin,Germany")
notberlin = gpd.GeoSeries(
    [
        box(*box(*berlin.total_bounds).buffer(0.1).bounds).difference(
            berlin["geometry"].values[0]
        )
    ],
    crs=berlin.crs,
)


fig, ax = plt.subplots(1, 1, figsize=(10, 10))
_ = ax.axis("off")

notberlin.plot(
    ax=ax,
    color="white",
    edgecolor="black",
    alpha=0.7,
)

# basemap
cx.add_basemap(
    ax,
    crs=berlin.crs,
)

# plt.savefig('stackoverflow_question.png',
#             dpi=100,
#             bbox_inches='tight',
#            )

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文