Search code examples
pythonseabornxticksseaborn-objects

How to show all x-tick labels with seaborn.objects


How do I make it so that it shows all x ticks from 0 to 9?

 bin      diff
1       4 -0.032748
3       9  0.106409
13      7  0.057214
17      3  0.157840
19      0 -0.086567
...   ...       ...
1941    0  0.014386
1945    4  0.049601
1947    9  0.059406
1957    1  0.045282
1959    6 -0.033853

    (
    so.Plot(x='bin', y='diff', data=diff_df) 
    .theme({**axes_style("whitegrid"), "grid.linestyle": ":"})
    .add(so.Dots())
    .add(so.Range(color='orange'), so.Est())
    .add(so.Dot(color='orange'), so.Agg())
    .add(so.Line(color='orange'), so.Agg())
    .label(
        x="Image Similarity Bin", y="Difference",
        color=str.capitalize,
    )
  )

I tried to set xticks in .label, but it doesn't do anything.

Now it only shows 0, 2, 4...


Solution

  • import pandas as pd
    import numpy as np
    import seaborn.objects as so
    
    # sample data
    np.random.seed(365)
    rows = 1100
    data = {'diff': np.random.random(size=rows), 'bin': np.random.choice(range(10), size=rows)}
    df = pd.DataFrame(data)
    
    # plot
    (so.Plot(x='bin', y='diff', data=df)
     .add(so.Dots())
     .add(so.Range(color='orange'), so.Est())
     .add(so.Dot(color='orange'), so.Agg())
     .add(so.Line(color='orange'), so.Agg())
     .label(x="Image Similarity Bin", y="Difference", color=str.capitalize)
     .scale(x=so.Continuous().tick(every=1)))  # adjust the ticks here
    

    enter image description here