Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何更改y轴以在Python Plotnine barplot中显示百分比(%)?_Python_Bar Chart_Visualization_Data Science_Plotnine - Fatal编程技术网

如何更改y轴以在Python Plotnine barplot中显示百分比(%)?

如何更改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

如何使用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(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)