Раскраска определенных стран с помощью геопанд - PullRequest
0 голосов
/ 04 ноября 2018

Я хочу нанести на карту местоположение определенных объектов на африканском континенте, и я могу сделать это, используя код ниже. Теперь я хочу раскрасить только конкретные страны (например, Намибию), чтобы сделать еще один анализ более четким. Кто-нибудь знает чистый способ сделать это? Заранее большое спасибо.

Лучший, Брэм

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')

1 Ответ

0 голосов
/ 13 ноября 2018

Это может быть " чистый способ раскраски только определенных стран (на примере Намибии) ". Дается только соответствующая часть кода.

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()

Полученный участок:

enter image description here

...