GeoDataFrameとしてのパリのレストラン
この章の最初のコーディング演習では、csvファイルからパリのレストランの位置情報を読み込みました。
GeoPandas の地理空間機能を使えるようにするため、pandas の DataFrame を GeoDataFrame に変換します。これは GeoDataFrame() コンストラクタと geopandas.points_from_xy() 関数で行え、この演習ではすでに用意されています。
GeoDataFrame になったので、ジオメトリのプロットなど、空間データ向けの機能が使えるようになります。この演習では、最初の演習と同じレストランの図を、今度は GeoDataFrame の plot() メソッドを使って作成します。
Pandas は pd、GeoPandas は geopandas、matplotlib の pyplot は plt としてインポート済みです。
この演習はコースの一部です
Pythonで扱う地理空間データ
演習の手順
restaurantsGeoDataFrame の先頭行を確認します。- GeoDataFrame の
plot()メソッドでプロットします。戻り値は matplotlib の axes オブジェクトです。これをaxと呼びます。 - マーカーサイズは再び 1 に設定してください。
contextilyを使ってベースマップレイヤーを追加します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Read the restaurants csv file into a DataFrame
df = pd.read_csv("paris_restaurants.csv")
# Convert it to a GeoDataFrame
restaurants = geopandas.GeoDataFrame(df, geometry=geopandas.points_from_xy(df.x, df.y))
# Inspect the first rows of the restaurants GeoDataFrame
print(restaurants.____)
# Make a plot of the restaurants
ax = restaurants.____
import contextily
contextily.____(____)
plt.show()