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
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? 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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With