Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

How can we change y axis to percent, instead of a fraction using Plotnine library in Python?

A MWE of a barplot is as follows:

from plotnine import *

from plotnine.data import mpg

p = ggplot(mpg) + geom_bar(aes(x='manufacturer', fill='class'), position='fill')

print(p)

Which gives the following figure:

Stacked bar chart with y axis as fraction not percent

With ggplot2 in R it is simple, just need to add:

+ scale_y_continuous(labels = scales::percent)

However I have not been able to find how to do this in Plotnine.

Any advise?

1 Answer

0 votes
by (41.4k points)

What label parameter does is, it accepts a callable that takes the list of breakpoint as input.So, you have to convert each item in the list manually.

scale_y_continuous(labels=lambda l: ["%d%%" % (v * 100) for v in l])

If you want to know more about Python visit this Python Course.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 18, 2019 in Python by Sammy (47.6k points)

Browse Categories

...