'How to Show only boundaries (no fill) of a shapefile in python plotly express?
I have a shapefile of areas/polygons and I'm trying to show that in a choropleth_mapbox in plotly express. What I want to show is only the boundaries/borders. i.e. the fill colour is transparent!
I've been doing something like this:
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)
The above code still shows polygons with filled colours. How can I remove the fill colour and keep only the border? I was trying to play with the opacity, but that affects both the fill colour and the border! Are they both controlled using the same parameters? Can one apply different properties to either?
To help better explain what I'm trying to achieve, I used QGIS to produce something similar to what my code is currently doing (1) and what I'm trying to get to (2), see images below:
1-Current Code:
2-Desired Output: 
Thanks in advance!
Solution 1:[1]
If all you want are boundary lines, then you can add a geojson layer. Have used other geometry as you geometry is not accessible to me.
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"
}
]
},
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
