我试着用Python和牛郎星制作Vega/Vega图表。以下代码返回Javascript错误:
map_ = alt.topo_feature('https://raw.githubusercontent.com/ginseng666/GeoJSON-TopoJSON-Austria/master/2021/simplified-99.5/gemeinden_995_topo.json', 'gemeinden')
df = pd.DataFrame({
'population': {
10101: 3239, 10201: 1985, 10301: 1890, 10302: 1846, 10303: 2122, 10304: 3217,
10305: 1270, 10306: 1212, 10307: 2192, 10308: 1400, 10309: 3640, 10310: 1741,
10311: 1291, 10312: 2925, 10313: 2691, 10314: 1436, 10315: 3203, 10316: 2994,
10317: 2083, 10318: 1717, 10319: 1974, 10320: 501, 10321: 826, 10322: 990,
10323: 1114
}
}).reset_index()
df.columns = ['iso', 'population']
hist = alt.Chart(df).mark_bar().encode(
x = alt.X('population:Q', bin=True),
y = alt.Y('count()')
)
cmap = alt.Chart(map_).mark_geoshape().encode(
color=alt.Color('population:Q', scale=alt.Scale(scheme='spectral')),
).transform_lookup(
lookup='properties.iso',
from_=alt.LookupData(df, 'iso', ['population'])
)
cmap | hist上面的代码会产生以下错误:
Javascript Error: Undefined data set name: "data_0"
This usually means there's a typo in your chart specification. See the javascript console for the full traceback.然而,令人惊讶的是,当我反转HConcat的顺序时,显示的图表没有错误:
hist | cmap

发布于 2022-05-20 02:07:30
有趣的发现!我相信这是下一个Vega库中的一个bug,,它似乎对数据集进行了错误的排序,以便在定义数据集之前引用一个。
解决方法是反向查找,它适用于任何一个hconcat顺序:
cmap = alt.Chart(df).mark_geoshape().encode(
color=alt.Color('population:Q', scale=alt.Scale(scheme='spectral')),
).transform_lookup(
lookup='iso',
from_=alt.LookupData(map_, 'properties.iso', ['geometry', 'type'])
)
cmap | hist

https://stackoverflow.com/questions/72310288
复制相似问题