【问题标题】:Geopandas : displaying several layers on iPython notebookGeopandas:在 iPython 笔记本上显示多个图层
【发布时间】:2016-11-25 09:37:57
【问题描述】:

我正在测试 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')

但我的笔记本不是图形显示,而是返回:

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

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

【问题讨论】:

    标签: python pandas matplotlib ipython geopandas


    【解决方案1】:

    命令

    %matplotlib inline
    

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

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

    %matplotlib notebook
    

    而不是内联。

    【讨论】:

      【解决方案2】:

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

      %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:顺便说一句,你把一些变量搞混了

      【讨论】:

      • 您好,非常感谢您的回答。刚刚测试了解决方案,但得到了 NameError: name 'plt' is not defined for plt.subplots()... 我错过了什么模块?编辑:无论如何都可以正常工作!
      • 哦,是的,那是我的错,我忘了添加“import matplotlib.pyplot as plt”
      猜你喜欢
      • 2016-02-24
      • 2014-12-26
      • 2023-03-21
      • 2014-02-08
      • 2016-07-15
      • 2014-10-19
      • 2015-04-07
      • 2017-01-25
      • 1970-01-01
      相关资源
      最近更新 更多