Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mplleaflet: Add the plot legend and select initial zoom

Can anyone guide me on how to add a legend to the mplleaflet html plot. And second how to choose an initial zoom?

open shapefile in geopandas and plot property on interactive web map

Example Code:

import geopandas as gp
shapefile = 'shapefile/ne_50m_admin_0_countries.shp'
df_shapefile_countries = gpd.GeoDataFrame.from_file(shapefile)

import mplleaflet
ax = df_shapefile_countries .plot(column='pop_est')
mplleaflet.show(fig=ax.figure)

Example Image: I would like to immediately zoom for example to South East Asia

World map

like image 546
Philipp Schwarz Avatar asked May 07 '16 17:05

Philipp Schwarz


People also ask

What is mplleaflet?

mplleaflet is a Python library that converts a matplotlib plot into a webpage containing a pannable, zoomable Leaflet map. It can also embed the Leaflet map in an IPython notebook. The goal of mplleaflet is to enable use of Python and matplotlib for visualizing geographic data on slippy maps without having to write any Javascript or HTML.

How to add legend to imshow () in Matplotlib?

How to add legend to imshow () in Matplotlib? Set the figure size and adjust the padding between and around the subplots. Create random data using numpy. Initialize a color map. Get the unique data points from sample data, step 2. Plot each color with different labels and color, to place on the legend.

How do I install mplleaflet on PyPI?

Install mplleaflet from PyPI using $ pip install mplleaflet. If developing for mplleaflet, mplexporter is a git submodule with its Python package files placed under the mplleaflet package. The Makefile copies the files into the appropriate location.

What is the difference between mplleaflet and basemap?

However mplleaflet allows you to leverage all matplotlib capability without having to set up the background basemap. You can use plot () to style points and lines, and you can also use more complex functions like contour (), quiver (), etc. Furthermore, with mplleaflet you no longer have to worry about setting up the basemap.


1 Answers

I think, right now, the functionality you want is not implemented.

For the first question regarding the legend, have a look here: https://github.com/jwass/mplleaflet/issues/35

For the second question, being creative, you can hack a zoom creating a transparent plot with the coordinates you want. For instance, have a look to the code below:

import matplotlib.pyplot as plt
import mplleaflet

fig, ax = plt.subplots()

# Your plot
ax.plot([10, 20, 30], [10, 20, 30])

# A second plot with some coordinates you want to use as the limits
# For instance, I want a plot with  x between (-100, 100)
ax.plot([-100, 100], [10, 30], alpha = 0)

mplleaflet.show(fig = ax.figure)

This is far from perfect, only works if you want a further view (I don't know if in english is said like this) but it is better than nothing. ax.set_xlim and/or ax.set_ylim doesn't work so you can't have a zoom closer than what you want to plot in the map.

I hope it helps.

like image 149
kikocorreoso Avatar answered Sep 20 '22 16:09

kikocorreoso