Black Lives Matter. Please consider donating to Black Girls Code today.

Plot coordinates on scatterbox

Hi All,

I have a data frame with a column that contains coordinates stored as tuples. Sample data:

28

I need to plot create the data dictionary and plot the locations on the map. Here’s my code:

@app.callback(Output("map-graph", "figure"),
          [
              Input("company-select", "value")
          ]
         )

def update_graph(selected_value):

# Plot home location and map commute to work. 
# Create a data dictionary of lat and lon to plot


return {
        
    "data": [
            {
                "type": "scattermapbox",
                "lat": hlat,
                "lon": hlong,
                #"text": ,
                "mode": "markers",
                "marker": {
                    "size": 3,
                    "opacity": 1.0
                }
            }
        
          for i, row in Comp_data.iterrows() 
              
        ],
    "layout": {
        "autosize": True,
        "hovermode": "closest",
        "mapbox": {
            "accesstoken": MAPBOX_KEY,
            "bearing": 0,
            "center": {
                "lat": 37.8272,
                "lon": 122.2913
            },
            "pitch": 0,
            "zoom": 8,
            "style": "outdoors"
        }
    }
}  

Something else that I have also tried is, creating a list of all lat and long to pass to the function.

  # Filter the dataframe with selected value
Comp_data = EmpComm1[EmpComm1['Company'] == selected_value]

hlat = []
hlong = []

for i, row in Comp_data.iterrows():
    
    coords = Comp_data['HomeLocation'].iloc[i]
    
    # List of coordinates 
    hlat.append(coords[0])
    hlong.append(coords[1])

My question is: How do I pick the lat and long from each tuple pair (37.710602289422475, -121.55962388557604) in the data frame and create the data dictionary. Thanks in advance for the help!