修改mark_text()的背​​景颜色

Modify background color of mark_text()

我正在使用 Altair 显示等值线图。尝试使用 mark_text() 向地图添加标签时,我无法更改这些文本框的背景颜色。我正在关注伦敦地铁线路 listed on the Altair website:

上的代码示例
import altair as alt
from vega_datasets import data

boroughs = alt.topo_feature(data.londonBoroughs.url, 'boroughs')
centroids = data.londonCentroids.url

background = alt.Chart(boroughs).mark_geoshape(
    stroke='white',
    strokeWidth=2
).encode(
    color=alt.value('#eee'),
).properties(
    width=700,
    height=500
)

labels = alt.Chart(centroids).mark_text().encode(
    longitude='cx:Q',
    latitude='cy:Q',
    text='bLabel:N',
    size=alt.value(8),
    opacity=alt.value(0.6)
).transform_calculate(
    "bLabel", "indexof (datum.name,' ') > 0  ? substring(datum.name,0,indexof(datum.name, ' ')) : datum.name"
)

background + labels

原则上,在Vega Lite中应该可以设置文本框的背景颜色,如图here

感谢任何提示。

类似问题已有答案,这是否回答了您的问题?

交互式更改 Altair 标记文本:背景颜色的可能性?

I don't think there is a way to highlight the background of the text via a CSS-like property and the workarounds I can think of are not applicable in this scenario. For example, I tried adding a rectangle mark behind it but it doesn't work with the double click. [...]

你可以找到它here