Geopandas:在 iPython 笔记本上显示多个图层

Geopandas : displaying several layers on iPython notebook

我正在测试 geopandas 库的一个简单练习:在地图上显示几个点,然后在上面叠加一个大圆圈以删除其中的一部分 with the difference method

为了检查转换是否正常,我正在使用 iPython 笔记本来查看我的不同图层。

所以,我的操作开始了:

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

然后,为了看我的观点,我只写了:

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

这是结果:

太好了。现在我只想在这一层中添加一个半径较大的点。我试过了:

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)

然后,我只是认为这些命令就足够了:

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

但我的笔记本没有图形显示 returns :

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

到目前为止我还没有发现什么问题...

嗯,我最好的猜测是你没有在一个单元格中执行你的代码......对于一些奇怪的行为,如果在多个单元格上执行,情节不会显示......我可以复制你的问题,但是当我在情节显示的一个单元格中执行代码。

%matplotlib inline
import pandas as pd
import geopandas as gp
import numpy as np
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)
ax.set_aspect('equal')


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

ps:顺便说一句,你混淆了一些变量

命令

%matplotlib inline

生成静态图。一旦它出现在您的笔记本中,就不能再更改。这就是为什么你必须像 schlump 所说的那样将你的代码放在一个单元格中。

另一种方法是切换到笔记本后端,它是交互式的,允许您修改多个单元格上的绘图。要激活它,只需使用

%matplotlib notebook

而不是内联。