<jupyter_start><jupyter_text>Visualizing spatial data with Python<jupyter_code>%matplotlib inline

import pandas as pd
import geopandas

import matplotlib.pyplot as plt

pd.options.display.max_rows = 10
countries = geopandas.read_file("zip://./data/ne_110m_admin_0_countries.zip")
cities = geopandas.read_file("zip://./data/ne_110m_populated_places.zip")
rivers = geopandas.read_file("zip://./data/ne_50m_rivers_lake_centerlines.zip")<jupyter_output><empty_output><jupyter_text>GeoPandas visualization functionality  Basic plot<jupyter_code>countries.plot()<jupyter_output><empty_output><jupyter_text>Adjusting the figure size<jupyter_code>countries.plot(figsize=(15, 15))<jupyter_output><empty_output><jupyter_text>Removing the box / x and y coordinate labels<jupyter_code>ax = countries.plot(figsize=(15, 15))
ax.set_axis_off()<jupyter_output><empty_output><jupyter_text>Coloring based on column valuesLet's first create a new column with the GDP per capita:<jupyter_code>countries = countries[(countries['pop_est'] >0 ) & (countries['name'] != "Antarctica")]
countries['gdp_per_cap'] = countries['gdp_md_est'] / countries['pop_est'] * 100<jupyter_output><empty_output><jupyter_text>and now we can use this column to color the polygons:<jupyter_code>ax = countries.plot(figsize=(15, 15), column='gdp_per_cap')
ax.set_axis_off()
ax = countries.plot(figsize=(15, 15), column='gdp_per_cap', scheme='quantiles', legend=True)
ax.set_axis_off()<jupyter_output><empty_output><jupyter_text>Combining different dataframes on a single plotThe `.plot` method returns a matplotlib Axes object, which can then be re-used to add additional layers to that plot with the `ax=` keyword:<jupyter_code>ax = countries.plot(figsize=(15, 15))
cities.plot(ax=ax, color='red', markersize=10)
ax.set_axis_off()
ax = countries.plot(edgecolor='k', facecolor='none', figsize=(15, 10))
rivers.plot(ax=ax)
cities.plot(ax=ax, color='C1')
ax.set(xlim=(-20, 60), ylim=(-40, 40))<jupyter_output><empty_output><jupyter_text>Using `geoplot`The `geoplot` packages provides some additional functionality compared to the basic `.plot()` method on GeoDataFrames:- High-level plotting API (with more plot types as geopandas)- Native projection supporthttps://residentmario.github.io/geoplot/index.html<jupyter_code>import geoplot
import geoplot.crs as gcrs
fig, ax = plt.subplots(figsize=(10, 10), subplot_kw={
    'projection': gcrs.Orthographic(central_latitude=40.7128, central_longitude=-74.0059)
})
geoplot.choropleth(countries, hue='gdp_per_cap', projection=gcrs.Orthographic(), ax=ax,
                   cmap='magma', linewidth=0.5, edgecolor='white', k=None)
ax.set_global()
ax.outline_patch.set_visible(True)
#ax.coastlines()<jupyter_output><empty_output><jupyter_text>Using `cartopy`Cartopy is the base matplotlib cartographic library, and it is used by `geoplot` under the hood to provide projection-awareness.http://scitools.org.uk/cartopy/docs/latest/index.htmlThe following example is taken from the docs: http://geopandas.readthedocs.io/en/latest/gallery/cartopy_convert.htmlsphx-glr-gallery-cartopy-convert-py<jupyter_code>from cartopy import crs as ccrs
# Define the CartoPy CRS object.
crs = ccrs.AlbersEqualArea()

# This can be converted into a `proj4` string/dict compatible with GeoPandas
crs_proj4 = crs.proj4_init
countries_ae = countries.to_crs(crs_proj4)

# Here's what the plot looks like in GeoPandas
countries_ae.plot()<jupyter_output><empty_output><jupyter_text>Interactive web-based visualizationsThere are nowadays many libraries that target interactive web-based visualizations and that can handle geospatial data. Some packages with an example for each:- Bokeh: https://bokeh.pydata.org/en/latest/docs/gallery/texas.html- GeoViews (other interface to Bokeh/matplotlib): http://geo.holoviews.org- Altair: https://altair-viz.github.io/gallery/choropleth.html- Plotly: https://plot.ly/python/maps- ... Another popular javascript library for online maps is [Leaflet.js](https://leafletjs.com/), and this has python bindings in the [folium](https://github.com/python-visualization/folium) and [ipyleaflet](https://github.com/jupyter-widgets/ipyleaflet) packages. An example with folium:<jupyter_code>import folium
m = folium.Map([48.8566, 2.3429], zoom_start=6, tiles="OpenStreetMap")
folium.GeoJson(countries.to_json()).add_to(m)
m
m = folium.Map([48.8566, 2.3429], zoom_start=6, tiles="OpenStreetMap")
folium.GeoJson(cities.to_json()).add_to(m)
m<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12757385854968667, 'fraction_numerical': 0.021486123545210387, 'mean_word_length': 7.155109489051095, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 42, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '90358', 'license': 'MIT', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1564, 'n_words': 321, 'path': '04-more-on-visualization.ipynb', 'repo_name': 'leechr/myGeospatial', 'size': '4468'}