无法将altair可视化作为弹出图插入大叶贴图

问题描述

我对python比较陌生,这是我正在从事的第一个项目。我正在尝试使用altair将简单的折线图绘制到大叶草标记的弹出窗口上。我遵循了此页面https://github.com/python-visualization/folium/blob/master/examples/Popups.ipynb上的示例。但是,当我单击标记时,它只是纯白色。除了弹出窗口外,地图和标记的绘制都很好。 这就是我的整个数据集的样子:

enter image description here

代码如下:

import pandas as pd
import folium
import altair as alt
import json

chart = alt.Chart(df).mark_line().encode(
        x='index',y='Ambala',color='Variable')

chart_2 = json.loads(chart.to_json())
m = folium.Map([30,-80],zoom_start=2)
popup = folium.Popup(max_width=650)
folium.Vega(chart_2,height=350,width=650).add_to(popup)
folium.Marker([30,popup=popup).add_to(m)
m

请注意,这只是代码示例。我将在我的项目中更大规模地实现这一点。 我试图绘制可视化效果(我在这里找到:https://github.com/python-visualization/folium/blob/master/examples/data/vis1.json),这似乎很好用。我只是不明白为什么即使altair从我的代码生成一个漂亮的图表,为什么相同的代码也适用于他们的绘图却不适用于我的绘图。抱歉,这听起来很傻,我仍然是初学者,而python是我的母语。谢谢

解决方法

如果要在叶片地图内使用Altair图表,则需要使用VegaLite对象而不是Vega对象。您只需要像这样更改与Vega对象的行:folium.features.VegaLite(chart_2,height=350,width=650).add_to(popup) folium map