Search code examples
pythonplotlycontourfolium

Plot cotour graph on folium map


I've drawn a contour plot using matplolib and i want to overly this plot over folium map or is there any way to draw contour plot on map using folium

This is my code


    import json
    
    import numpy as np
    from scipy.interpolate import griddata
    from matplotlib import pyplot as plt
    
    f = open('data.json', 'r')
    
    data = json.load(f)
    
    temp = data['temp']
    lon = data['lon']
    lat = data['lat']
    
    x = np.linspace(min(lon), max(lon), 100)
    y = np.linspace(min(lat), max(lat), 100)
    
    X,Y = np.meshgrid(x, y)
    Z = griddata((lon, lat), temp, (X, Y), method='cubic')
    
    plt.contour(X,Y, Z)
    plt.show()

My data file data.json, below is how my plot looks now i want to plot this over map

contour plot


Solution

  • import json
    import numpy as np
    from scipy.interpolate import griddata
    from matplotlib import pyplot as plt
    import geojsoncontour
    import geopandas as gpd
    
    f = open('data.json', 'r')
    
    data = json.load(f)
    
    temp = data['temp']
    lon = data['lon']
    lat = data['lat']
    
    y = np.linspace(min(lon), max(lon), 100)
    x = np.linspace(min(lat), max(lat), 100)
    
    X,Y = np.meshgrid(x, y)
    Z = griddata((lat, lon), temp, (X, Y), method='cubic')
        
    contour =    plt.contour(X,Y, Z)
    
    gdf = gpd.GeoDataFrame.from_features(json.loads(geojsoncontour.contour_to_geojson(
        contour=contour,
        min_angle_deg=3.0,
        ndigits=5,
        stroke_width=1))).set_crs("EPSG:4326")
    
    m = gdf.explore(color=gdf["stroke"])
    
    plt.show()
    m
    

    enter image description here