如何替换 altair 中的轴标签?
How to replace the axis label in altair?
我想将轴标签从 [0,0.5,1] 更改为 ['infrequent','average','frequent'],如下所示:
我尝试如下进行更改,但它不起作用。
alt.Chart(df).mark_circle().encode(
alt.X('x:Q',
axis=alt.Axis(values=['infrequent','average','frequent']),title="A's frequency"),
alt.Y('y:Q',
axis=alt.Axis(values=['infrequent','average','frequent']),title="B's frequency"),
color=alt.Color('s:Q',scale=alt.Scale(domain=[0, 1],scheme="redyellowblue")),
tooltip=['term',
alt.Tooltip('cat:Q', title="Occurence in A"),
alt.Tooltip('ncat:Q', title="Occurence in B"),
alt.Tooltip('s:Q', title="Score close to A",format='.2')]
).properties(
width=300,
height=300
)
修改后的图表如下:
谁能给我一些建议吗?提前致谢。
您可以使用与 this VegaLite example with a labelExpr
string 中相同的方法:
axis_labels = (
"datum.label == 0 ? 'Infrequent'
: datum.label == 0.5 ? 'Average'
: 'Frequent'"
)
alt.X('x:Q',axis=alt.Axis(labelExpr=axis_labels))
我想将轴标签从 [0,0.5,1] 更改为 ['infrequent','average','frequent'],如下所示:
我尝试如下进行更改,但它不起作用。
alt.Chart(df).mark_circle().encode(
alt.X('x:Q',
axis=alt.Axis(values=['infrequent','average','frequent']),title="A's frequency"),
alt.Y('y:Q',
axis=alt.Axis(values=['infrequent','average','frequent']),title="B's frequency"),
color=alt.Color('s:Q',scale=alt.Scale(domain=[0, 1],scheme="redyellowblue")),
tooltip=['term',
alt.Tooltip('cat:Q', title="Occurence in A"),
alt.Tooltip('ncat:Q', title="Occurence in B"),
alt.Tooltip('s:Q', title="Score close to A",format='.2')]
).properties(
width=300,
height=300
)
修改后的图表如下:
谁能给我一些建议吗?提前致谢。
您可以使用与 this VegaLite example with a labelExpr
string 中相同的方法:
axis_labels = (
"datum.label == 0 ? 'Infrequent'
: datum.label == 0.5 ? 'Average'
: 'Frequent'"
)
alt.X('x:Q',axis=alt.Axis(labelExpr=axis_labels))