如何在 python plotly express 中仅显示 shapefile 的边界(无填充)?
How to Show only boundaries (no fill) of a shapefile in python plotly express?
我有一个 areas/polygons 的 shapefile,我试图在 choropleth_mapbox 中以 plotly express 的形式显示它。我要显示的只是boundaries/borders。即填充颜色是透明的!
我一直在做这样的事情:
import geopandas as gpd
import plotly.express as px
from plotly.offline import plot
import pandas as pd
#read my geo dataframe
geodf = 'path/myShp.shp'
geodf = gpd.read_file(geodf)
# shape file is a different CRS, change to lon/lat GPS co-ordinates
geodf = geodf.to_crs("WGS84")
fig = px.choropleth_mapbox(
geodf.set_index("objectid"),
geojson=geodf.geometry,
locations=geodf.index,
opacity =0.1,
center=dict(lat=52.484, lon=-1.88141),
mapbox_style="carto-positron",
zoom=9,
)
fig.update_layout(coloraxis_showscale=False)
plot(fig)
上面的代码仍然显示了带有填充颜色的多边形。如何删除填充颜色并仅保留边框?
我试图调整不透明度,但这会同时影响填充颜色和边框!它们是否都使用相同的参数进行控制?可以对两者应用不同的属性吗?
为了帮助更好地解释我想要实现的目标,我使用 QGIS 生成了类似于我的代码当前正在做的事情 (1) 和我想要达到的目标 (2),请参见下图:
1-当前代码:
2-期望输出:
提前致谢!
如果你想要的只是边界线,那么你可以添加一个 geojson 图层。由于我无法访问您的几何图形,因此使用了其他几何图形。
import geopandas as gpd
import plotly.express as px
import plotly.graph_objects as go
from shapely.geometry import MultiPolygon
from plotly.offline import plot
import pandas as pd
# read my geo dataframe
geodf = "path/myShp.shp"
# geodf = gpd.read_file(geodf)
geodf = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
# shape file is a different CRS, change to lon/lat GPS co-ordinates
geodf = geodf.to_crs("WGS84")
# create an empty figure with a mapbox trace
fig = go.Figure(go.Scattermapbox())
# now add boundaries we want
fig.update_layout(
coloraxis_showscale=False,
mapbox={
"style":"carto-positron",
"layers": [
{
"source": geodf["geometry"].__geo_interface__,
"type": "line",
"color": "red"
}
]
},
)
我有一个 areas/polygons 的 shapefile,我试图在 choropleth_mapbox 中以 plotly express 的形式显示它。我要显示的只是boundaries/borders。即填充颜色是透明的!
我一直在做这样的事情:
import geopandas as gpd
import plotly.express as px
from plotly.offline import plot
import pandas as pd
#read my geo dataframe
geodf = 'path/myShp.shp'
geodf = gpd.read_file(geodf)
# shape file is a different CRS, change to lon/lat GPS co-ordinates
geodf = geodf.to_crs("WGS84")
fig = px.choropleth_mapbox(
geodf.set_index("objectid"),
geojson=geodf.geometry,
locations=geodf.index,
opacity =0.1,
center=dict(lat=52.484, lon=-1.88141),
mapbox_style="carto-positron",
zoom=9,
)
fig.update_layout(coloraxis_showscale=False)
plot(fig)
上面的代码仍然显示了带有填充颜色的多边形。如何删除填充颜色并仅保留边框? 我试图调整不透明度,但这会同时影响填充颜色和边框!它们是否都使用相同的参数进行控制?可以对两者应用不同的属性吗?
为了帮助更好地解释我想要实现的目标,我使用 QGIS 生成了类似于我的代码当前正在做的事情 (1) 和我想要达到的目标 (2),请参见下图:
1-当前代码:
提前致谢!
如果你想要的只是边界线,那么你可以添加一个 geojson 图层。由于我无法访问您的几何图形,因此使用了其他几何图形。
import geopandas as gpd
import plotly.express as px
import plotly.graph_objects as go
from shapely.geometry import MultiPolygon
from plotly.offline import plot
import pandas as pd
# read my geo dataframe
geodf = "path/myShp.shp"
# geodf = gpd.read_file(geodf)
geodf = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
# shape file is a different CRS, change to lon/lat GPS co-ordinates
geodf = geodf.to_crs("WGS84")
# create an empty figure with a mapbox trace
fig = go.Figure(go.Scattermapbox())
# now add boundaries we want
fig.update_layout(
coloraxis_showscale=False,
mapbox={
"style":"carto-positron",
"layers": [
{
"source": geodf["geometry"].__geo_interface__,
"type": "line",
"color": "red"
}
]
},
)