我正在尝试在图中写一些文本以突出显示我的情节中的某些内容(相当于 matplotlib 中的“注释”)。任何想法?谢谢
请您参考如下方法:
您可以通过两个步骤将注释添加到您的 Altair 图中:
- 使用
mark_text()指定注释的位置、字体大小等。 - 使用
transform_filter()从datum中选择需要注释的点(数据子集)。请注意from altair import datum 行。
代码:
import altair as alt
from vega_datasets import data
alt.renderers.enable('notebook')
from altair import datum #Needed for subsetting (transforming data)
iris = data.iris()
points = alt.Chart(iris).mark_point().encode(
x='petalLength',
y='petalWidth',
color='species')
annotation = alt.Chart(iris).mark_text(
align='left',
baseline='middle',
fontSize = 20,
dx = 7
).encode(
x='petalLength',
y='petalWidth',
text='petalLength'
).transform_filter(
(datum.petalLength >= 5.1) & (datum.petalWidth < 1.6)
)
points + annotation
这些是静态注释。您还可以通过将 selections 绑定(bind)到绘图来获得交互式注释。
