【问题标题】:Folium map not displaying叶地图不显示
【发布时间】:2016-05-01 17:56:59
【问题描述】:

在 Canopy 版本 1.5.5.3123 上运行 与;

Folium 版本:0.1.2,内部版本:1

以下代码;

import folium  
import pandas as pd
LDN_COORDINATES = (51.5074, 0.1278)  
from IPython.display import HTML
import shapefile
#create empty map zoomed in on London
LDN_COORDINATES = (51.5074, 0.1278) 
map = folium.Map(location=LDN_COORDINATES, zoom_start=12)
display(map)  

返回

<folium.folium.Map at 0x10c01ae10>

但没有别的。

如何在 ipython 笔记本中显示地图?

【问题讨论】:

    标签: python canopy folium


    【解决方案1】:

    您也可以将地图保存为html,然后用浏览器打开。

    import folium
    import webbrowser
    
    
    class Map:
        def __init__(self, center, zoom_start):
            self.center = center
            self.zoom_start = zoom_start
        
        def showMap(self):
            #Create the map
            my_map = folium.Map(location = self.center, zoom_start = self.zoom_start)
    
            #Display the map
            my_map.save("map.html")
            webbrowser.open("map.html")
    
    
    #Define coordinates of where we want to center our map
    coords = [51.5074, 0.1278]
    map = Map(center = coords, zoom_start = 13)
    map.showMap()
    

    【讨论】:

      【解决方案2】:

      _build_map() 不再存在。以下代码对我有用

      import folium
      from IPython.display import display
      LDN_COORDINATES = (51.5074, 0.1278)
      myMap = folium.Map(location=LDN_COORDINATES, zoom_start=12)
      display(myMap)
      

      【讨论】:

        【解决方案3】:

        考虑到上述答案,另一种简单的方法是与Jupiter Notebook一起使用。

        例如(在木星笔记本上):

        import folium
        
        london_location = [51.507351, -0.127758]
        
        m = folium.Map(location=london_location, zoom_start=15)
        m
        

        并在调用 'm' 时查看结果。

        【讨论】:

        • 问题恰恰不在Jupiter Notebook中
        • @Joachim 是的,我提到过:考虑到上述答案,另一种简单的方法是使用 Jupiter notebook。
        【解决方案4】:

        我发现this tutorial on Folium in iPython Notebooks 很有帮助。您创建的原始 Folium 实例不足以让 iPython 显示地图 - 您需要做更多工作才能获得 iPython 可以呈现的一些 HTML。

        要在 iPython 笔记本中显示,您需要使用 myMap._build_map() 方法生成 html,然后将其包装在带有 iPython 样式的 iFrame 中。

        import folium  
        from IPython.display import HTML, display
        LDN_COORDINATES = (51.5074, 0.1278) 
        myMap = folium.Map(location=LDN_COORDINATES, zoom_start=12)
        myMap._build_map()
        mapWidth, mapHeight = (400,500) # width and height of the displayed iFrame, in pixels
        srcdoc = myMap.HTML.replace('"', '&quot;')
        embed = HTML('<iframe srcdoc="{}" '
                     'style="width: {}px; height: {}px; display:block; width: 50%; margin: 0 auto; '
                     'border: none"></iframe>'.format(srcdoc, width, height))
        embed
        

        通过返回 embed 作为 iPython 单元格的输出,iPython 将在返回的 iFrame 上自动调用 display.display()。在这种情况下,您只需要在之后渲染其他内容或在循环或函数中使用它时调用display()

        另外,请注意,使用 map 作为变量名可能会与多个类的 .map() 方法混淆。

        【讨论】:

        • myMap._build_map() _build_map() 不再退出。他们把它从树叶里拿出来。如果没有 _build_map() 怎么解决?
        【解决方案5】:

        您使用过时版本的 Folium 是否有原因?

        这个 ipython notebook 阐明了 1.2 和 2 之间的一些差异,并解释了如何将 folium 地图放入 iframe。 http://nbviewer.jupyter.org/github/bibmartin/folium/blob/issue288/examples/Popups.ipynb

        并且代码看起来像这样(在上面的笔记本中找到,它添加了一个标记,但可以将其取出):

        m = folium.Map([43,-100], zoom_start=4)
        
        html="""
            <h1> This is a big popup</h1><br>
            With a few lines of code...
            <p>
            <code>
                from numpy import *<br>
                exp(-2*pi)
            </code>
            </p>
            """
        iframe = folium.element.IFrame(html=html, width=500, height=300)
        popup = folium.Popup(iframe, max_width=2650)
        
        folium.Marker([30,-100], popup=popup).add_to(m)
        
        m
        

        文档也已启动并运行,http://folium.readthedocs.io/en/latest/

        【讨论】:

          【解决方案6】:

          2022 年不需要使用 iframe。要显示地图,只需使用

          {{ map | safe }} html 中的标签和_repr_html_() 方法在你的视图中。也不需要将地图保存到模板中

          sample.py

          @app.route('/')
          def index():
              start_coords = (46.9540700, 142.7360300)
              folium_map = folium.Map(location=start_coords, zoom_start=14)
              return folium_map._repr_html_()
          

          template.html

          <!DOCTYPE html>
          <html lang="en">
          <head>
              <meta charset="UTF-8">
              <title>Title</title>
          </head>
          <body>
          {{ folium_map | safe }}
          </body>
          </html>
          

          【讨论】:

            【解决方案7】:

            我有同样的错误,没有什么对我有用 最后我找到了 打印(目录(folium.Map)) 见方法保存剂量不存在,而是使用

            【讨论】:

              猜你喜欢
              • 2022-06-14
              • 2018-03-02
              • 1970-01-01
              • 2022-07-19
              • 1970-01-01
              • 2012-11-30
              相关资源
              最近更新 更多