How to show all the city names automatcially in mapbox

I would like to show all the city names automatcially in the plot, still can’t find a way to figure it out。
Is there anyone can help? thx

There is a past answer that meets your objective. Please refer to Annotations on plotly Choropleth.

import plotly.express as px
import plotly.graph_objects as go
lons = [125.3245, 122.841114]
lats = [43.886841, 45.619026]
text= ["Changchun","Baicheng"]

fig = px.choropleth_mapbox(df, geojson=geoJSON, locations='name', color='pop',featureidkey="properties.name",
                           color_continuous_scale="Reds",
                           range_color=(0, 20),
                           mapbox_style="carto-positron",
                           zoom=3, center = {"lat": 42, "lon": 130},
                           #animation_frame =df["name"],
                           opacity=0.5,
                           labels={'pop':'unemployment rate'}
                          )
fig.add_scattermapbox(lat=lats,
                        lon=lons,
                         mode='text+markers',
                        text=text,   
                        textposition='top center',
                         marker_size=8, marker_color='red')
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})

fig.show()

I have updated coding and it can show the marker but not text, is there any solutions?

I tried to see if I could actually do it. geojosn got it from geojson.cn and I created the user data properly and used it for the map. Place name annotations were specified using go.Scattertermapbox. And if you annotate text after go.Choroplethmapbox, there are cases where it is hidden in the mapbox place name display. Therefore, the place name is annotated first, and the choropleth map is annotated after.

from urllib import request
import json

url='https://geojson.cn/api/data/220000.json'
with request.urlopen(url) as f:
    jilin = json.load(f)

names = []
for n in range(len(jilin['features'])):
    name = jilin['features'][n]['properties']['name']
    names.append(name)
names
['长春', '吉林', '四平', '辽源', '通化', '白山', '松原', '白城', '延边']
lats = []
lons = []
for n in range(len(jilin['features'])):
    lat_lon = jilin['features'][n]['properties']['center']
    lats.append(lat_lon[1])
    lons.append(lat_lon[0])
import random
import pandas as pd
import numpy as np

df = pd.DataFrame({'name':names, 'values': np.random.randint(0,100,9)})

import plotly.express as px
import plotly.graph_objects as go
lons = [125.3245, 122.841114]
lats = [43.886841, 45.619026]
text= ["Changchun","Baicheng"]

mapbox_token = open("mapbox_api_key.txt").read()
fig = go.Figure()

fig.add_trace(go.Scattermapbox(lat=lats,
                               lon=lons,
                               mode='text+markers',
                               text=text,#df['name'].to_list(),
                               textposition='top left',
                               textfont=dict(color='blue'),
                               marker_size=5,
                               marker_color='blue'
                                ))

fig.add_trace(go.Choroplethmapbox(geojson=jilin,
                                  locations=df['name'],
                                  z=df['values'],
                                  featureidkey="properties.name",
                                  colorscale="Reds",
                                  zmin=0,
                                  zmax=100,
                                  marker_opacity=0.8,
                                  marker_line_width=0
                          ))

fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})

fig.update_layout(mapbox=dict(
    center={"lat": 43.5, "lon": 126},
    accesstoken=mapbox_token,
    zoom=5,
    style="light"
))

fig.show()

Thanks a lot! Still can’t show the text. I’m wondering the reason is I’m using mapbox_ style instead of map box token. :smiling_face_with_tear:

Have you tried running my code, mapbox_token is required. In addition, the text display must be written first.

Yes, I have tried. Sicne I’m not very experienced in Dash and mapbox, I use the mapbox_style instead of mapbox_token, and the text doesn’t display.

By the way, can I take a few minutes of your time? How can i get the file like “mapbox_api_key.txt”? I spend some time but can’t figure out by myself. Thx!

Register a mapbox account to use the new style. Registration is free. After registering an account, you will get an API key and save it locally as a text file for use. Please refer to the reference for information on mapbox.

Thx!!!(10 characters)

@r-beginners

I’m using plotly==5.14.1

and get an error:

Javascript Error: Something went wrong with axis scaling

I have valid mapbox_api_key.

Your comment also answered the original question, so please review my answer.