Search code examples
pythonplotbokehgeopandas

GeoPandas and Bokeh extract xs and ys from data - WorldMap


I found this post on Geopandas and bokeh extract xs and ys from data

What I need is basically the same thing but for the map of the whole world (extract XS and ys from GeoPandas and convert into bokeh readable format). I am struggling with the fact the world data has both polygons and multi polygons.

If anyone can help, that would be much appreciated. Thanks!


Solution

  • This is how you can run a GeoJSON using pandas GeoJSONDataSource like I mentioned in my comment.

    from bokeh.models import GeoJSONDataSource
    from bokeh.plotting import figure, show, output_notebook
    import geopandas as gp
    
    output_notebook()
    
    world = gp.read_file(gp.datasets.get_path('naturalearth_lowres'))
    geo_source = GeoJSONDataSource(geojson=world.to_json())
    
    p = figure(title='World', tooltips=[('Country', '@name')],
               x_range=(-180, 180), y_range=(-90, 90), 
               x_axis_location=None, y_axis_location=None,
               plot_width=1000, plot_height=500
              )
    p.patches('xs', 'ys', fill_alpha=0.4, fill_color='grey', 
              line_color='black', line_width=0.5, source=geo_source
             )
    show(p)
    

    Output

    world