Reputation: 48
So, I am using plotly mapbox plotly.express.choropleth_mapbox
to display colored tiles.
Ultimately what I want to do, is to overlay a scatter plot, whether a circle, symbols, etc. Some 'Point' coordinates on top of the colored tiles.
I am not aware of a way to combine scatter_mapbox as a trace to choropleth, so I tried to update mapbox and use the circle layer, below.
Note that I've done this with an outline from another .geojson
file to draw an outline on the map (yay this part works), but the overlay of circles did not work. (Also tried symbol and did not work either.)
Was trying to troubleshoot if it was my geojson file or not, since I created the file on my computer. Tried creating both in python and with QGIS.
My goal: add markers/coordinates on top of a tiled geographic map.
Here is the code below which should allow you to see the same as me.
import pandas as pd
import plotly.express as px
point_geo = {'type': 'FeatureCollection',
'name': 'locations',
'features': [{'type': 'Feature',
'properties': {'description': 'xyz', 'name': 'A'},
'geometry': {'type': 'Point', 'coordinates': [43.58283, -79.61944]}},
{'type': 'Feature',
'properties': {'description': 'xyz','name': 'B '},
'geometry': {'type': 'Point', 'coordinates': [43.58294, -79.61244]}}
]}
abc = pd.DataFrame({'FID':['19','18'],'MEDIAN INCOME':[60000,70000]})
def_= requests.get('https://opendata.arcgis.com/datasets/3a90af0bfd034a48a1bee2f7ff4f105a_0.geojson').json()
ghi_ = requests.get('https://opendata.arcgis.com/datasets/0e149347e0b54724996d99080bed1874_0.geojson').json()
fig3 = px.choropleth_mapbox(abc, geojson=def_,
featureidkey='properties.FID', locations='FID', color='MEDIAN INCOME',
mapbox_style="carto-positron",
opacity=0.4,
color_continuous_scale = px.colors.sequential.Inferno_r ,
zoom=12, center = {"lat": 43.5887 , "lon": -79.64})
fig3.update_layout(
mapbox = {
'layers': [
## this part works
{'source': ghi_,
'type': "line", 'color': "royalblue", 'opacity':1, 'line':{'width':3} },
## this part does not work
{'source':point_geo, 'type':'circle', 'circle':{'radius':2} }
]},
)
fig3.show()
Upvotes: 2
Views: 6032
Reputation: 380
When you want to add more graphs to a figure that you create with plotly express, you add traces to that original figure. In this case, since your base figure is a mapbox, we would add a scatter_mapbox to the figure you created originally with px.choropleth_mapbox.
A plotly scatter_mapbox requires a list of latitudes and longitudes- I simply extracted that from your geojson and also added the text values in case you want to use the description as the hoverinfo.
Add this to the bottom of your code to add markers:
lats = [item['geometry']['coordinates'][0] for item in point_geo['features']]
lons = [item['geometry']['coordinates'][1] for item in point_geo['features']]
texts = [item['properties']['description'] for item in point_geo['features']]
fig3.add_scattermapbox(
lat = lats,
lon = lons,
mode = 'markers+text',
text = texts,
marker_size=12,
marker_color='rgb(235, 0, 100)'
)
Here is the full reference to the plotly express scatter_mapbox. class.
Upvotes: 4