Search code examples
python-3.xaltair

Remove repeating values from X axis label in Altair


I am having trouble with Altair repeating X axis label values.

Data:
  rule_abbreaviation  flagged_claim  bill_month
0         CONCIDPROC              1     Apr2022
1         CONTUSMAT1              1     Apr2022
2            COVID05              1     Jun2021
3         FILTROTUB2              1     Sep2021
4         MEPIARTRO1              1     Mar2022

#Code to generate Altair Bar Chart

bar = alt.Chart(Data).mark_bar().encode(
        x=alt.X('flagged_claim:Q', axis=alt.Axis(title='Flagged Claims', format= ',.0f'), stack='zero'),
        y=alt.Y('rule_abbreaviation:N', axis=alt.Axis(title='Component Abbreviation'), sort=alt.SortField(field=measure, order='descending')),
        tooltip=[alt.Tooltip('max(ClaimRuleName):N', title='Claim Component'), alt.Tooltip('flagged_claim:Q', title='Flagged Claims', format= ',.0f')],
        color=alt.Color('bill_month', legend=None)
    ).properties(width=485,
                 title = alt.TitleParams(text = 'Bottom Components', 
                                         font = 'Arial', 
                                         fontSize = 16, 
                                         color = '#000080', 
                                        )
                ).interactive()

X axis label generated by this chart contains repeated 0 and 1 Image of Visualization: https://i.sstatic.net/0XdWB.png


Solution

  • The reason this is happening is because you have format= ',.0f' which tells Altair to include 0 decimals in the axis labels. Remove it or change to 1f to see decimals in the labels. In general, a good way to troubleshoot problems like this is to remove part of your code at a time to identify which part is causing the unexpected behavior.

    To reduce the number of ticks you can use alt.Axis(title='Flagged Claims', format='d', tickCount=1) or alt.Axis(title='Flagged Claims', format='d', values=[0, 1]). See also Changing Number of y-axis Ticks in Altair