简体   繁体   中英

How to replace the axis label in altair?

I want to change the axis lable from [0,0.5,1] to ['infrequent','average','frequent'] like the following: 在此处输入图像描述

I tried to make the change as followed, but it does not work.

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
)

The chart of after change is as follow: 在此处输入图像描述

Can anyboday give me some advices? Thanks in advance.

You can use the same approach as in 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM