Raphadasilva Raphadasilva - 1 year ago 211
Python Question

Geopandas : displaying several layers on iPython notebook

I'm testing geopandas library for a simple exercise : displaying several points on a map, and then superimpose a large circle above to delete a part of them with the difference method.

To check that the transformation works fine, I'm using an iPython notebook to see my different layers.

So, here's the begining of my manipulation :

%matplotlib inline
# this line is just for a correct plotting in an iPython nb
import pandas as pd
import geopandas as gp
from shapely.geometry import Point

df = pd.read_csv("historical_monuments.csv", sep = ",")
geometry = [Point(xy) for xy in zip(fichier.Longitude, fichier.Latitude)]
# I convert two columns of my csv for geographic information displaying
df = df.drop(['Longitude', 'Latitude'], axis = 1)
# just delete two columns of my first df to avoid redundancy
geodf = gp.GeoDataFrame(file, crs=None, geometry=geometry)

Then, to see my points, I just wrote :

geodf.plot(marker='o', color='red', markersize=5)

Here's the result :

enter image description here

That's super fine. Now I just want to add in this layer a point with a large radius. I tried this :

base = gdf.plot(marker='o', color='red', markersize=5)
# the first plotting becomes a variable to reuse it
center_coord = [Point(6.18, 48.696000)]
center = gp.GeoDataFrame(crs=None, geometry=center_coord)
circle = center.buffer(0.001)

Then, I just thought that these command would be enough :

circle.plot(ax=base, color = 'white')

But instead of a graphical displaying, my notebook returns :

<matplotlib.axes._subplots.AxesSubplot at 0x7f763bdde5c0>
<matplotlib.figure.Figure at 0x7f763be5ef60>

And I didn't find what could be wrong so far...

Answer Source

Well my best guess is you didn't execute your code within one Cell... for some strange behaviour the plot does not show up if executed over multiple cells... I could replicate your problem, however when i executed the Code in one cell the plot showed up.

%matplotlib inline
import pandas as pd
import geopandas as gp
import matplotlib.pyplot as plt
from shapely.geometry import Point

# Create Fake Data
df = pd.DataFrame(np.random.randint(10,20,size=(10, 3)), columns=['Longitude','Latitude','data'])

# create Geometry series with lat / longitude
geometry = [Point(xy) for xy in zip(df.Longitude, df.Latitude)]

df = df.drop(['Longitude', 'Latitude'], axis = 1)

# Create GeoDataFrame
geodf = gp.GeoDataFrame(df, crs=None, geometry=geometry)

# Create Matplotlib figure
fig, ax = plt.subplots()

# Set Axes to equal (otherwise plot looks weird)

# Plot GeoDataFrame on Axis ax
geodf.plot(ax=ax,marker='o', color='red', markersize=5)
# Create new point
center_coord = [Point(15, 13)]
center = gp.GeoDataFrame(center,crs=None, geometry=center_coord)
# Plot new point
center.plot(ax=ax,color = 'blue',markersize=5)
# Buffer point and plot it
circle = center.buffer(10)
circle.plot(color = 'white',ax=ax)

Final plot

ps: Btw you've got some variables mixed up

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download