How to add annotation from lat/lon values?

How to add markers using add_annotation() using latitude and longitude values, since this function does not have lat , lon parameters. Is there any way to convert latitude and longitude to x and y ?

@ctrlspace Unforunately there is no way to add annotation at a geographical position. There is such a feature request from 2020, but not implemented yet: annotations for mapbox subplots Β· Issue #5142 Β· plotly/plotly.js Β· GitHub

image
@empet is there any way to do this without annotations?

fig.add_trace(go.Scattergeo(
  lat=lat_list, 
  lon=lon_list, 
  mode='text+markers', 
  textposition='bottom  right')
)

I just need the bgcolor parameter.
because in my case the text is hard to see in the background and i would like to add something like bgcolor in the annotations.
maybe there are other solutions?

@ctrlspace
Yes, you can define mode=β€œmarkers+text”, and set a convenient font color to get a visible text:

import plotly.graph_objects as go
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')

fig = go.Figure(data=go.Scattergeo(
        lon = df['long'][:5],
        lat = df['lat'][:5],
        text = [f"Text{k}" for k in range(1,6)],
        mode = 'markers+text',
        textposition='bottom right'
        ))

fig.update_layout(
        title_text = 'Scattergeo with markers and text', title_x=0.5,
        geo_scope='usa', font_color="black")

@empet No, I need a text background (orange color like the picture)

There is no text background for text defined like this.

@empet Are there any other ways to add text with background at coordinate points?

Hey @ctrlspace ,

Maybe you can write a function that takes points as an input and creates filled polygons around it.

import plotly.graph_objects as go
import pandas as pd

fig = go.Figure()

fig.add_trace(
    go.Scattergeo(
        lon=[10,10,15,15,10], #x1-x1-x2-x2-x1
        lat=[15,20,20,15,15], #y1-y2-y2-y1-y1

        fill="toself",
        mode='lines',
        name='',
        text='Custom shape',
        opacity=1
        )
    )


fig.add_trace(
    go.Scattergeo(
        lon = [12.5],
        lat = [17.5],
        text = ['A'],
        mode = 'markers+text',
        textposition='bottom right'
        )
    )

fig.update_layout(
        title_text = 'Scattergeo with markers and text', title_x=0.5,
         font_color="black")


fig.show()

1 Like

@ctrlspace You can’t find any color to set font_color or textfont, https://plotly.com/python/reference/scattergeo/#scattergeo-textfont, such that to contrast with the overall background of your map ?

@akroma suggested a solution, that adds more traces to the map. Depending on the length of your text array, it can overload your plot. But it should look good for a low length text array.

From now on when you want to know what are a trace properties, please visit https://plotly.com/python/reference/TRACENAME/, in your case https://plotly.com/python/reference/scattergeo/.

2 Likes

@akroma A good idea! I will try to do this. Thanks

1 Like

@empet
Anyway I need a background.
Annotations would be ideal because the rectangle would automatically take on the correct width.
I’m sorry for asking such questions, thank you :slightly_smiling_face: