Search code examples
pythonopenstreetmapipyleaflet

How to plot data over OpenStreetMap (Python)


Thank you in advance for all your answers.

I am trying to plot a dataframe over an interactive map (OpenStreetMap), and what I get is what is in the picture, but I would like the map outlines to be on top of my data and not the data to be like a blob over the map. My code is:

import plotly.express as px
import numpy as np
from plotly.offline import plot

fig = px.scatter_mapbox(df, lat="lat", lon="lon",
                        color="hot days", zoom=4,
                        color_continuous_scale=px.colors.cyclical.Twilight,
                        mapbox_style='open-street-map')

plot(fig, auto_open=True)

And the result I get is this:

OpenStreetMap


Solution

  • The issue is likely the structure of your DataFrame. It appears that "no data" areas are getting mapped to zero or something similar, and the DF including a record for every point on the map - including the null / zero data bits you're not interested in plotting. Try filtering out the records with zero data. Something like

    df = df[df['hot days']>0]
    

    Check one or two of the points in the ocean or other no-data areas to confirm they're at 0, and set the threshold accordingly. If they're null instead, try

    df = df[df['hot days'].notna()]