pandasgeopandasgraph-coloring

Coloring specific countries with geopandas


I want to map location of certain facilities on the African continent and I am able to do so using the code below. Now I want to color only specific countries (take Namibia as an example) to make another analysis more clear. Does anybode know a clean way of doing this? Thank you very much in advance.

Best, Bram

import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

data = pd.read_csv("Repair_yards_africa.csv")

base = world[world.continent == 'Africa'].plot(color='white', edgecolor='black')

data['Coordinates'] = list(zip(data.lon, data.lat))
data['Coordinates'] = data['Coordinates'].apply(Point)
geodata = gpd.GeoDataFrame(data, geometry='Coordinates')
geodata.plot(ax=base, color='red', markersize=11)
plt.ylabel('Lattitude')
plt.xlabel('Longitude')

Solution

  • This might be a "clean way of coloring only specific countries (take Namibia as an example)". Only relevant part of code is given.

    import matplotlib.pyplot as plt
    import geopandas as gpd
    from descartes import PolygonPatch
    
    world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
    
    def plotCountryPatch( axes, country_name, fcolor ):
        # plot a country on the provided axes
        nami = world[world.name == country_name]
        namigm = nami.__geo_interface__['features']  # geopandas's geo_interface
        namig0 = {'type': namigm[0]['geometry']['type'], \
                  'coordinates': namigm[0]['geometry']['coordinates']}
        axes.add_patch(PolygonPatch( namig0, fc=fcolor, ec="black", alpha=0.85, zorder=2 ))
    
    # plot the whole world
    #ax2 = world.plot( figsize=(8,4), edgecolor=u'gray', cmap='Set2' )
    
    # or plot Africa continent
    ax2 = world[world.continent == 'Africa'].plot(figsize=(8,8), edgecolor=u'gray', cmap='Pastel1')
    
    # then plot some countries on top
    plotCountryPatch(ax2, 'Namibia', 'red')
    plotCountryPatch(ax2, 'Libya', 'green')
    
    # the place to plot additional vector data (points, lines)
    
    plt.ylabel('Latitude')
    plt.xlabel('Longitude')
    
    #ax2.axis('scaled')
    plt.show()
    

    The resulting plot:

    enter image description here

    Edit

    Alternatively, the plot can be produced by this shorter version of code.

    import matplotlib.pyplot as plt
    import geopandas as gpd
    # from descartes import PolygonPatch
    
    world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
    
    # or plot Africa continent
    ax2 = world[world.continent == 'Africa'].plot(figsize=(8,8), edgecolor=u'gray', cmap='Pastel1')
    
    world[world.name == "Libya"].plot(edgecolor=u'gray', color='green', ax=ax2)
    world[world.name == "Namibia"].plot(edgecolor=u'gray', color='red', ax=ax2)
    
    # the place to plot additional vector data (points, lines)
    
    plt.ylabel('Latitude')
    plt.xlabel('Longitude')
    
    #ax2.axis('scaled')
    plt.show()