Using Folium to display multiple maps.

huangapple go评论52阅读模式
英文:

Using Folium to display multiple maps

问题

我有一些GPS数据,我正在地图上绘制它们。我成功地使用了Folium和Jupyter一次绘制一组数据:

mapPoints = [ [lat1, lng1], [lat2, lng2], ... ]
m = folium.Map(location=location, zoom_start=15)
folium.PolyLine(mapPoints).add_to(m)
m

这会显示我想要的地图。但是当我尝试生成多个地图时:

maps = []
for item in clustersOfPoints:
  mapPoints = getListOfLatLngs(item)
  m = folium.Map(location=location, zoom_start=15)
  folium.PolyLine(mapPoints).add_to(m)
  maps.append(m)
  m
...
for map in maps:
  map

什么都不显示。循环内的mapPoints列表是正确的(通过检查值进行了验证),map变量包含一系列folium.folium.Map对象(也经过了检查验证)。

我如何才能让它生成我想要的一系列地图呢?

英文:

I have some GPS data that I am plotting on maps. I was able to successfully use Folium and Jupyter to plot this data one group at a time:

mapPoints = [ [lat1, lng1], [lat2, lng2], ... ]
m = folium.Map(location=location, zoom_start=15)
folium.PolyLine(mapPoints).add_to(m)
m

This will display exactly the map I want / expect it to. When I try to generate multiple maps:

maps = []
for item in clustersOfPoints:
  mapPoints = getListOfLatLngs(item)
  m = folium.Map(location=location, zoom_start=15)
  folium.PolyLine(mapPoints).add_to(m)
  maps.append(m)
  m
...
for map in maps:
  map

Nothing displays. The list of mapPoints inside the loop is correct (I've verified by inspecting the value), and the map variable contains a list of folium.folium.Map objects (also verified by inspection).

How can I get it to generate the series of map renderings I want?

答案1

得分: 2

你尝试调整代码块的最后部分,循环遍历地图以指定显示每个地图吗?像这样:

for map in maps:
  display(map)

(你可以在生成循环中的maps.append(m)下面删除m行,它在那里没有起作用。)

解释

你的第一个代码块有效,因为Jupyter笔记本单元格中的最后一行(或更具体地说,最后一个表达式或被调用的对象)是特殊的。你可能已经遇到过与每个单元格中的最后一行相关的REPL模型。不管最后一行是什么,它都会被评估,并以最适当该类型数据应该显示的方式显示出来,而无需你进行任何操作。这就是为什么如果你在一个单元格中输入2 + 2在一行上,然后在同一单元格的第二行输入3 + 3,当你运行该单元格时,你将只看到6作为输出。

不过,你可以将这两行更改为print(2 + 2)print(3 + 3),或者display(2 + 2)display(3 + 3),然后重新运行单元格,查看每个计算的结果都在连续的行上显示。

所以,根据我上面建议的更改,你明确表示你想要显示每个folium地图。而不是依赖于最后评估的内容由Jupyter的特殊显示处理来处理。经常让人们期望最后一行被处理的是for循环,或者任何缩进,比如if - then条件子句,使其不是最外层评估的东西,因此它不会被Jupyter对最后表达式的特殊处理处理。请参阅此处以获得更详细的解释。你的for循环示例更清楚,即该单元格中的最后评估是for循环耗尽地图列表,并且从中不返回任何内容,因此在这种情况下,Jupyter不会特别处理和显示任何内容。

为了使情况变得更加复杂一些,最近检测到单元格中的任何matplotlib代码都将尝试进行显示。不再需要在结尾处使用plot.show()来进行Jupyter的特殊显示处理。但这对于folium不适用。我认为Matplotlib是一个例外,因为它是唯一已经集成了更独特、方便的处理方式的地方。

英文:

Did you try to adjust the last section of your code block where you loop on your maps to specify to display each? Like this:

for map in maps:
  display(map)

(You can remove the m line below maps.append(m) in your generating loop. It doesn't do anything there.)

Explanation

Your first code block works because the last line (or more specifically the last expression or object called) in a Jupyter notebook cell is special.
You've probably come across the REPL model the is pertinent to the last line in each cell. Whatever the last line is it will be evaluated and the result displayed in a manner that is optimally how that type of data should be displayed. Without you needing to do anything. That is why if you put in a cell 2 + 2 on one line and then 3 + 3 on the second line of a single cell, when you run that cell, you'll just see 6 as the output.

You can though change the two lines to print(2 + 2) and print(3 + 3) or display(2 + 2) and display(3 + 3) and re-run the cell and see the result for each calculation displayed on successive lines.

So with the change I suggest above, you are saying explicitly you want to display each folium map. And not relying on the last thing evaluated to be handled by the special Jupyter display handling. What often trips people up expecting the last line to be handled is that the for loop, or any indentation, such as by an if - then conditional clause, makes it not the outermost thing evaluated and so it isn't handled by the special handling of Jupyter for the last expression, See here for a more full explanation of that. Your for loop example is even more clear that the last evaluation in that cell is the for loop exhausting the list of maps and nothing is returned from that and so nothing is specially handled & displayed by Jupyter in that case.

To make it a little more of a confusing situation though, lately any matplotlib code detected in a cell will try to be displayed. There is no longer a need for plot.show() at the end to get handled by the special Jupyter display handling. This isn't the case with folium though. I think Matplotlib is exception as it the only place that more unique, convenient handling has been integrated for now.

huangapple
  • 本文由 发表于 2023年5月14日 04:35:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定