Resizing the legend
We can also resize the legend using ax and cax arguments of plot().
- ax: matplotlib.pyplot. Artist (default None). axes on which to draw the plot.
- cax: matplotlib.pyplot Artist (default None). axes on which to draw the legend in case of color map.
For this, we need matplotlib library.
The axes_divider.make_axes_locatable function takes an existing axes, adds it to a new AxesDivider, and returns the AxesDivider. The append_axes method of the AxesDivider can then be used to create new axes on a given side (“top”, “right”, “bottom”, or “left”) of the original axes. To create axes at the given position with the same height (or width) of the main axes-
Syntax:
append_axes(self, position, size, pad=None, add_to_figure=True, **kwargs)
position can take any value from: “left”, “right”, “bottom” or “top”.
size and pad should be axes_grid.axes_size compatible.
Example:
Python3
import geopandas as gpd import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import make_axes_locatable # Reading the world shapefile world_data = gpd.read_file(r 'world.shp' ) world_data = world_data[[ 'NAME' , 'geometry' ]] # Calculating the area of each country world_data[ 'area' ] = world_data.area # Removing Antarctica from GeoPandas GeoDataframe world_data = world_data[world_data[ 'NAME' ] ! = 'Antarctica' ] world_data.plot() # Changing the projection current_crs = world_data.crs world_data.to_crs(epsg = 3857 , inplace = True ) world_data.plot(column = 'NAME' , cmap = 'hsv' ) # Re-calculate the areas in Sq. Km. world_data[ 'area' ] = world_data.area / 1000000 # Adding a legend world_data.plot(column = 'area' , cmap = 'hsv' , legend = True , legend_kwds = { 'label' : "Area of the country (Sq. Km.)" }, figsize = ( 7 , 7 )) # Resizing the legend fig, ax = plt.subplots(figsize = ( 10 , 10 )) divider = make_axes_locatable(ax) cax = divider.append_axes( "right" , size = "7%" , pad = 0.1 ) world_data.plot(column = 'area' , cmap = 'hsv' , legend = True , legend_kwds = { 'label' : "Area of the country (Sq. Km.)" }, ax = ax, cax = cax) |
Output:
Working with Geospatial Data in Python
Spatial data, also known as geospatial data, GIS data, or geodata, is a type of numeric data that defines the geographic location of a physical object, such as a building, a street, a town, a city, a country, or other physical objects, using a geographic coordinate system. You may determine not just the position of an object, but also its length, size, area, and shape using spatial data.
To work with geospatial data in python we need the GeoPandas & GeoPlot library
GeoPandas is an open-source project to make working with geospatial data in python easier. GeoPandas extends the data types used by pandas to allow spatial operations on geometric types. Geometric operations are performed shapely. Geopandas further depends on fiona for file access and matplotlib for plotting. GeoPandas depends on its spatial functionality on a large geospatial, open-source stack of libraries (GEOS, GDAL, and PROJ). See the Dependencies section below for more details.
Required dependencies:
- numpy
- pandas (version 0.24 or later)
- shapely (interface to GEOS)
- fiona (interface to GDAL)
- pyproj (interface to PROJ; version 2.2.0 or later)
Further, optional dependencies are:
- rtree (optional; spatial index to improve performance and required for overlay operations; interface to libspatialindex)
- psycopg2 (optional; for PostGIS connection)
- GeoAlchemy2 (optional; for writing to PostGIS)
- geopy (optional; For plotting, these additional for geocoding)
packages may be used:
- matplotlib (>= 2.2.0)
- mapclassify (>= 2.2.0)
Geoplot is a geospatial data visualization library for data scientists and geospatial analysts that want to get things done quickly. Below we’ll cover the basics of Geoplot and explore how it’s applied. Geoplot is for Python 3.6+ versions only.
Note: Please install all the dependencies and modules for the proper functioning of the given codes.
Contact Us