Formatting text labels in seaborn.objects

263 views Asked by At

Formatting of the so.Text() mapping doesn't seem to have an effect on text labels. In the example below, I am trying to format using the text= scale such that the text labels are rounded to 1dp and are suffixed with "%". The numbers on the bars should look like "88.5%", "61.6%", etc.

A code example with my attempts is below.

enter image description here

#Data
np.random.seed(3)
df = pd.DataFrame({'Day': range(1, 31), 'Count': np.random.randn(30)*50 + 100})

#Bar plot
(
    so.Plot(df, y='Day', x='Count', text=100 * df.Count / df.Count.max())
    .add(so.Bar(), legend=False, orient='y')
    .add(so.Text(color='k', halign='right'))
    .scale(color='viridis',
           #None of these attempts are making a difference to the formatting:
           
           text=so.Continuous().label(like='.0f')
           #text=so.Continuous().label(like='{x:.0f}$'.format)
           #text=so.Continuous().label(like=lambda x: str(round(x)) + '%')
           #text=so.Continuous().label(like='{y:.0f}$'.format)
           #text=so.Continuous().label(like='{pos:.0f}$'.format)
           #text=so.Continuous().tick(at=range(0, 101), every=1)
           )
    .layout(size=(10, 10))
)

I have also tried (not shown) setting unit='%' with the hope that it would do the percent calculation for me, but it gives me values > 100% so I've just computed the percentages manually in this example.

Is the failure of the formatting a bug? Please advise if there are other things I could try.


For reference, here is the workaround I am using which I wanted to avoid. Manually formatting the data mapped to text=:

#Bar plot
(
    so.Plot(df,
            y='Day',
            x='Count',
            #Manually format text:
            text=(100 * df.Count / df.Count.max()).apply(lambda x: str(round(x, 1)) + '%')
    )
    .add(so.Bar(), legend=False, orient='y')
    .add(so.Text(color='k', halign='right'))
    .scale(color='viridis')
    .layout(size=(10, 10))
)

enter image description here

0

There are 0 answers