Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Formatting percentage tick labels in seaborn catplot returning values higher than expected

The x axis on my catplot currently consists of categorical decimal values as such:

[0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8. 0.9, 1.0]

I’m using the following to try to format to a percentage, but it’s returning value values 10x greater than expected:

for axis in ax.axes.flat:
    axis.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, p: f'{x:.0%}'))
    axis.yaxis.set_major_formatter(ticker.FuncFormatter(lambda y, p: f'{y:.2%}'))

currently yields:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[0, 100%, 200%, 300%, 400%, 500%, 600%, 700%, 800%, 900%, 1000%]

The y-axis is formatted correctly, not sure why the x-axis is returning values higher than expected.

plot after label change

>Solution :

Internally, seaborn plots categorical variables at x=[0, 1, 2, ..., 10], so that’s why the percentage formatter is returning x=[0, 100, 200, ..., 1000].

In this case, you can apply the % f-string directly with set_xticklabels:

xticks = [0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]

for axis in ax.axes.flat:
    axis.set_xticklabels([f'{x:.0%}' for x in xticks])
    axis.yaxis.set_major_formatter(ticker.FuncFormatter(lambda y, p: f'{y:.2%}'))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading