python - 将多个 Vega/vincent 图表添加到 Folium 弹出窗口

标签 python folium vega

我想使用 Vincent/Vega 将两个图表添加到 Folium map 弹出窗口。我用图表创建了两个 vincent 对象,并尝试通过链接两个 add_child() 方法将它们都添加到 Popup 实例。但只有第二个图表被渲染。

这是我使用的代码,但没有数据,只是对用于图表的数据的描述。

from folium import Map, CircleMarker, Vega, Popup
from vincent import Bar
import math

map = Map(zoom_start=8, tiles='cartodbpositron',
             location = [-73.5, 45.2]))

# zones is a pandas dataframe with lat/lng pairs of points.

for i, zone in zones.iterrows():
    # weekday_pings: a pandas groupby aggregation of most frequent day of week in a dataframe
    # hour_pings: a pandas groupby aggregation of most frequent hour of day in a dataframe

    dayvega = Bar(weekday_pings, width=300,
               height=150).axis_titles(x='Weekday', y='Pings')
    daychart = Vega(dayvega.to_json(), width=vega.width+50, height=vega.height+50)


    timevega = Bar(hour_pings, width=300,
               height=150).axis_titles(x='Hour', y='Pings')
    timechart = Vega(timevega.to_json(), width=vega.width+50, height=vega.height+50)


    map.add_child( CircleMarker(
        location = [zone['latitudeE7'], zone['longitudeE7']],
        radius = int(math.sqrt(zone['cluster_size'])/10 + 2),
        fill_opacity = 0.8, color=None,
        fill_color = ('#274cc9'),
        popup = Popup(max_width=chart.width[0]).add_child(daychart).add_child(timechart)
    ) )

结果如下:只渲染了第二个 child 。

folium popup in Jupyter

最佳答案

最后,我没有找到解决方案,所以我改用 Altair 来制作图表,使用 compound chart constructor在一张图中制作多个图表。


关于python - 将多个 Vega/vincent 图表添加到 Folium 弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59651106/

相关文章:

Python 抓取和读取带有请求的加密返回

javascript - iPython 中的 Folium map 不显示 map 。抛出错误 : 'Uncaught ReferenceError: L is not defined'

python - Folium 自定义弹出窗口

python - altair 删除或抑制自动生成的绘图图例

python - 具有不同 x 和 y 编码的 Altair 选择

python - 不能在 Django 模板标签中使用 Python 字典

python - Flask:对单个 URL 使用 [GET, POST] 函数

python - 通过 boto 传入的 Amazon SQS 消息出现乱码

pandas - 如何通过一些标签从大叶 map 组上的 Pandas 数据帧绘制纬度和经度

powerbi - 如何垂直对齐符号,以便它们与 y 轴标签对齐?