Search code examples
pythongraphaltair

How do I reduce the number of ticks on an Altair graph?


I am using Altair to create a graph, but for some weird reason it's seems to be generating a tick for each of the points. Creating a graph like this Altair Graph

If I filter the dataframe, it produces weird axis values. Altair graph

Is there a way to reduce the amount of ticks? I tried tickCount in the y axis paramater and it didn't work since it seems to require integers.I also tried setting the axis value parameter to a list [0,0.2,0.4,0.6,0.8,1] and that didn't work either. Here is my code (sorry it's so lengthy!). Thank you in advance!

    a = alt.Chart(df_filtered).mark_point().encode(x =alt.X('Process_Time_(mins)', axis = alt.Axis(title='Process Time (mins)')),
    y = alt.Y('Heavy_Phase_%SS',axis=alt.Axis(title='Heavy Phase %SS', tickCount = 10),sort = 'descending'),
    color = alt.Color('DSP_Lot', legend = alt.Legend(title = 'DSP_Lot')),
    shape = alt.Shape('Strain', scale = alt.Scale(range = ["circle", "square", "cross", "diamond", "triangle-up", "triangle-down", "triangle-right", "triangle-left"])),
    tooltip = [alt.Tooltip('DSP_Lot',title  = 'Lot'), alt.Tooltip('Heavy_Phase_%SS', title = 'Heavy Phase %SS'),
    alt.Tooltip('Process_Time_(mins)', title = 'Process Time (mins)'), alt.Tooltip('Purpose', title = 'Purpose'), alt.Tooltip('Strain', title = 'Strain'),
    alt.Tooltip('Trial', title = 'Trial')]).properties(width = 1000, height = 500)

Solution

  • It's hard to tell without a reproducible example but I suspect the issue is that your y axis is defaulting to a nominal encoding type, in which case you get one tick mark per unique value. If you specify a quantitative type in the Y encoding, it may improve things:

    y = alt.Y('Heavy_Phase_%SS:Q', ...)
    

    The reason it defaults to nominal is probably because the associated column in the pandas dataframe has a string type rather than a numerical type.