如何更改y轴以在Python Plotnine barplot中显示百分比(%)?
如何使用Python中的Plotnine库将y轴更改为百分比而不是分数 条形图的MWE如下所示:如何更改y轴以在Python Plotnine barplot中显示百分比(%)?,python,bar-chart,visualization,data-science,plotnine,Python,Bar Chart,Visualization,Data Science,Plotnine,如何使用Python中的Plotnine库将y轴更改为百分比而不是分数 条形图的MWE如下所示: from plotnine import * from plotnine.data import mpg p = ggplot(mpg) + geom_bar(aes(x='manufacturer', fill='class'), position='fill') print(p) 它给出了下图: 在R中使用ggplot2非常简单,只需添加: + scale_y_continuous(lab
from plotnine import *
from plotnine.data import mpg
p = ggplot(mpg) + geom_bar(aes(x='manufacturer', fill='class'), position='fill')
print(p)
它给出了下图:
在R中使用ggplot2非常简单,只需添加:
+ scale_y_continuous(labels = scales::percent)
然而,在Plotnine中,我还没有找到如何做到这一点
任何建议?参数接受一个以断点列表为输入的可调用项。您只需手动转换列表中的每个项目:
scale_y_continuous(labels=lambda l: ["%d%%" % (v * 100) for v in l])
这里提出了类似的问题: 其他预定义过滤器可在此处找到:
from plotnine import *
from plotnine.data import mpg
from mizani.formatters import percent_format
p = ggplot(mpg) + geom_bar(aes(x='manufacturer', fill='class'), position='fill')
p = p + scale_y_continuous(labels=percent_format())
print(p)