PSAN KM
PSAN KM

Reputation: 33

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: enter image description here

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: enter image description here

Can anyboday give me some advices? Thanks in advance.

Upvotes: 2

Views: 5986

Answers (1)

joelostblom
joelostblom

Reputation: 48919

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))

Upvotes: 6

Related Questions