简体   繁体   中英

how to plot a map using geopandas and matplotlib

there seems to be an issue with my code. My goal is to plot a map that represents an outcome (population) accross the regions of Benin.

import pandas as pd
import matplotlib as mpl

database_path = "datafinalproject.csv"
database = pd.read_csv(database_path)
#Creating a geodataframe
points = gpd.points_from_xy(database["longitude"], database["latitude"], crs="EPSG:4326")
map = gpd.GeoDataFrame (database, geometry=points) 

I get this message when I type map.plot and I when I type map.plot(column='population') , I get an empty map.

Can you help me solve this problem? database.head() gives: 在此处输入图像描述

map.plot() will work in a Jupyter notebook but not in a normal Python environment.

You should import matplotlib.pyplot and add plt.show() at the end of your code:

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

database_path = "datafinalproject.csv"
database = pd.read_csv(database_path)
#Creating a geodataframe
points = gpd.points_from_xy(database["longitude"], database["latitude"], crs="EPSG:4326")
map = gpd.GeoDataFrame (database, geometry=points) 
map.plot()

plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM