Altair: Change tick/label spacing

Created on 24 Jun 2019  路  4Comments  路  Source: altair-viz/altair

I am creating a mark_line chart and my data results in the x axis from 0-30, with the labels/ticks every 2 (0,2,4,6, etc....). I would like to change the labels/ticks to every 5 instead (0,5,10, etc.). Is there a convenient way to accomplish this?

Thank you in advance or the help!

question

Most helpful comment

tickMinStep and the like only apply to quantitative scales. For ordinal or categorical scales, you can specify values directly:

alt.Chart(df).mark_rect().encode(
    alt.X('x:O', axis=alt.Axis(values=list(range(1, 20, 2)))),
    alt.Y('y:Q'),
    alt.Color('x:Q'),
    tooltip=['x','y']
)

visualization (38)

Note also that if necessary, the renderer will automatically adjust tick labels to prevent overlap:

df = pd.DataFrame({
    'x': range(50),
    'y': range(50)
})

alt.Chart(df).mark_rect().encode(
    alt.X('x:O'),
    alt.Y('y:Q'),
    alt.Color('x:Q'),
    tooltip=['x','y']
).properties(width=400)

visualization (39)

All 4 comments

Ticks are controlled via the axis property of encodings. So, for example, you can explicitly set tick locations using the values setting:

import altair as alt
import pandas as pd

df = pd.DataFrame({
    'x': range(20),
    'y': range(20)
})

alt.Chart(df).mark_line().encode(
    alt.X('x', axis=alt.Axis(values=[0, 5, 10, 15, 20])),
    alt.Y('y')
)

visualization (31)

The full set of axis properties is documented here: https://altair-viz.github.io/user_guide/generated/core/altair.Axis.html The tickMinStep property might be useful if you don't want to specify the values explicitly, but want the minimum step to be larger.

Thank you, that works perfectly.

Much appreciated!

Is there a way to set the tick frequency for Ordinal data?

Adapted the above for a mark_rect() example:

Desired output: every 5th tick (for example)

import altair as alt
import pandas as pd

df = pd.DataFrame({
    'x': range(20),
    'y': range(20)
})

alt.Chart(df).mark_rect().encode(
    alt.X('y:O',
          axis=alt.Axis(tickMinStep = 2, labelAngle=0),

),
    alt.Y('x:Q', axis=alt.Axis(values=[0, 5, 10, 15, 20])),
    alt.Color('x:Q'),
    tooltip=['x','y']
)

visualization-2

tickMinStep and the like only apply to quantitative scales. For ordinal or categorical scales, you can specify values directly:

alt.Chart(df).mark_rect().encode(
    alt.X('x:O', axis=alt.Axis(values=list(range(1, 20, 2)))),
    alt.Y('y:Q'),
    alt.Color('x:Q'),
    tooltip=['x','y']
)

visualization (38)

Note also that if necessary, the renderer will automatically adjust tick labels to prevent overlap:

df = pd.DataFrame({
    'x': range(50),
    'y': range(50)
})

alt.Chart(df).mark_rect().encode(
    alt.X('x:O'),
    alt.Y('y:Q'),
    alt.Color('x:Q'),
    tooltip=['x','y']
).properties(width=400)

visualization (39)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maxgerma picture maxgerma  路  3Comments

morberg picture morberg  路  3Comments

fischcheng picture fischcheng  路  4Comments

tonylee3399 picture tonylee3399  路  3Comments

Juan-132 picture Juan-132  路  3Comments