Search code examples
pythonscatter-plotaltairvega-lite

Altair Scatter Plot tickMinStep


I have this DataFrame:

    x           y           term        s
0   0.000000    0.132653    matlab      0.893072
1   0.000000    0.142857    matrix      0.905120
2   0.012346    0.153061    laboratory  0.902610
3   0.987654    0.989796    be          0.857932
4   0.938272    0.959184    a           0.861948

And have generated this chart:

chart

Using this code:

chart = alt.Chart(scatterdata_df).mark_circle().encode(
        x = alt.X('x:Q', axis = alt.Axis(title = "⏪  less physical | more physical ⏩", tickMinStep = 0.05)),
        y = alt.Y('y:Q', axis = alt.Axis(title = "⏪  less data | more data ⏩", tickMinStep = 0.05)),
        color = alt.Color('s:Q', scale=alt.Scale(scheme='redblue')),
        tooltip = ['term']
    ).properties(
        width = 500,
        height = 500
    )

Having set tickMinStep = 0.05 for both the x and y axes, I'm not sure why the graph doesn't reflect this parameter.

Thank you.


Solution

  • tickMinStep specifies the minimum tick spacing for the automatically determined ticks; actual tick spacing may be larger than this. It sounds like you want more ticks than the default heuristic gives, so you should instead adjust tickCount directly:

    import altair as alt
    import pandas as pd
    import numpy as np
    
    rng = np.random.default_rng(0)
    
    data = pd.DataFrame({
        'x': rng.random(size=100),
        'y': rng.random(size=100)
    })
    
    alt.Chart(data).mark_point().encode(
        x=alt.X('x:Q', axis=alt.Axis(tickCount=20)),
        y=alt.Y('y:Q', axis=alt.Axis(tickCount=20)),
    )
    

    enter image description here

    If you want even finer control over tick values, you can pass a list of values directly to Axis.values. For more information, see the alt.Axis documentation.