0

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))
)
some3128
  • 1,430
  • 1
  • 2
  • 8
  • The [edit](https://stackoverflow.com/revisions/76812518/5) should be an answer. – Trenton McKinney Aug 03 '23 at 15:27
  • 1
    I was using the edit before posting this question, and having to rely on the edit is what motivated my question (I posted the edit in case it was useful, but I don't consider it an answer to my query). I see my query more about what the technical issue is with `.scale(text=...`. – some3128 Aug 03 '23 at 15:38
  • 1
    Seaborn objects is a WIP, so some functionality isn’t available. If it was, @mwaskom would probably post a solution. – Trenton McKinney Aug 03 '23 at 15:51

0 Answers0