I'm trying to use the function "scatter_mapbox" from plotly.express and everything I get back are blank maps. I'm having this issue in Jupyter Notebook with Python 3.9.12. I have tried to generate maps with other functions such as "line_geo" with sucess, but it will be really good for my purposes if i could get this function properly running.
This is a example of a code that runs ok:
fig = px.line_geo(lat=[0,15,20,35], lon=[5,10,25,30])
fig.update_geos(fitbounds="locations")
fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
This is a example of a code that do not run as intended:
import pandas as pd
import plotly.express as px
data = pd.read_csv('kc_house_data.csv')
houses = data[['id','lat','long', 'price']].copy()
fig = px.scatter_mapbox(houses,
lat='lat',
lon='long',
hover_name='id',
hover_data=['price'],
zoom=3,
height=300
)
fig.show()
I also tried to generate the map only with the database and lat/lon paramaters also without sucess.
The databese in from here: https://www.kaggle.com/datasets/shivachandel/kc-house-data
I also enable the use of Open GL and that didnt work
I have been checking your problem and maybe your error is that you are using a function that plot using mapbox. For that you would need to import the mapbox token, like below:
import pandas as pd
import plotly.express as px
data = pd.read_csv('kc_house_data.csv')
houses = data[['id','lat','long', 'price']].copy()
px.set_mapbox_access_token(open(".mapbox_token").read())
fig = px.scatter_mapbox(houses,
lat='lat',
lon='long',
hover_name='id',
hover_data=['price'],
zoom=3,
height=300
)
fig.show()
Here you can find info about how to obtain it.
By the way, you can plot it without the token using a public USGS imagery map:
data = pd.read_csv('kc_house_data.csv')
houses = data[['id','lat','long', 'price']]
fig = px.scatter_mapbox(houses,
lat='lat',
lon='long',
hover_name='id',
hover_data=['price'],
zoom=3,
height=300
)
fig.update_layout(
mapbox_style="white-bg",
mapbox_layers=[
{
"below": 'traces',
"sourcetype": "raster",
"sourceattribution": "United States Geological Survey",
"source": [
"https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}"
]
}
])
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
Output: