【问题标题】:Modify background color of mark_text()修改mark_text()的背景颜色
【发布时间】:2021-09-09 18:04:00
【问题描述】:

我正在使用 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 所示。

感谢任何提示。

【问题讨论】:

  • 您是否尝试将填充选项传递给mark_text
  • 不幸的是,填充选项改变了字体颜色,而不是文本框的背景。
  • 在这两种情况下,我都可以改变字体颜色,color=alt.value('blue'),fill=alt.value('red'),但是没有参数可以改变背景颜色。

标签: python altair vega-lite


【解决方案1】:

有一个类似问题的答案,这能回答你的问题吗?

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

我认为没有办法突出显示文本的背景 通过类似 CSS 的属性,我能想到的解决方法不是 适用于这种场景。例如,我尝试添加一个矩形 标记在它后面,但它不适用于双击。 [...]

你可以找到它here

【讨论】:

  • 也可以使用mark_square(),以防更适合您的目的?
  • 非常感谢,解决方案在使用 mark_square()mark_circle() 时有效。但是,mark_rect() 不接受经度/纬度作为将矩形放置在地图上的参数。
  • 只是为了让您对它的外观有一个印象:h2888712.stratoserver.net:8080/#daily-prices 不幸的是,这些圆圈覆盖了地图中大部分较小的区域。但是标签的背景需要确保文本的可读性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-22
  • 2022-01-03
  • 2021-11-10
  • 2012-12-14
  • 2022-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多