[英]Bold some of the x-labels with alt.condition in Altair plot
How shall I make some of the x-labels bold with alt.condition?我应该如何使用 alt.condition 使一些 x-labels 加粗? For example with the following code, I try to make the x-label 'C' bold, which is corresponding to the max value 9 on the y-axis.例如,使用以下代码,我尝试将 x 标签“C”设为粗体,这对应于 y 轴上的最大值 9。
df = pd.DataFrame({'Name': ['A', 'B', 'C', 'D'], 'Value': [1, 5, 9, 2]})
alt.Chart(df).mark_line().encode(
x = alt.X(
'Name:N',
axis = alt.Axis(
labelFontWeight = alt.condition(alt.datum.Value == df.Value.max(), alt.value(800), alt.value(300))
)
),
y = 'Value:Q'
)
It's weird that it always goes to the if-false value, not matter how I change the predict.奇怪的是它总是达到 if-false 值,无论我如何更改预测。
I am not sure if you can do that directly in Altair/Vega-Lite, but you could compute the Name for the max Value and compare similar to what was suggested in the comments but slightly more automated using f-strings:我不确定您是否可以直接在 Altair/Vega-Lite 中执行此操作,但您可以计算最大值的名称并与评论中建议的内容进行比较,但使用 f-strings 的自动化程度更高:
import pandas as pd
import altair as alt
df = pd.DataFrame({'Name': ['A', 'B', 'C', 'D'], 'Value': [1, 5, 9, 2]})
max_name = df['Name'][df['Value'].argmax()]
alt.Chart(df).mark_line().encode(
x=alt.X(
'Name:N',
axis = alt.Axis(
# datum.label also works
labelFontWeight=alt.condition(f"datum.value == '{max_name}'", alt.value(800), alt.value(300))
)
),
y='Value:Q'
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.