简体   繁体   English

Python seaborn catplot - 如何将 y 轴比例更改为百分比

[英]Python seaborn catplot - How do I change the y-axis scale to percentage

In the figure the y-axis labels are in decimals from (0 to 1) ie (0.1, 0.2, 0.4 etc).在图中,y 轴标签是从(0 到 1)的小数,即(0.1、0.2、0.4 等)。 How can I convert this into a % format (10%, 20%, 40% etc).如何将其转换为 % 格式(10%、20%、40% 等)。 Just 10, 20, 40 also will do.只需 10、20、40 个也可以。

Thanks, John谢谢,约翰

g = sns.catplot(x="who", y="survived", col="class",
...                 data=titanic, saturation=.5,
...                 kind="bar", ci=None, aspect=.6)

You may use a PercentFormatter on the axes of the grid.您可以在网格的轴上使用PercentFormatter

import seaborn as sns
import matplotlib.pyplot as plt
from  matplotlib.ticker import PercentFormatter
titanic = sns.load_dataset("titanic")

g = sns.catplot(x="who", y="survived", col="class",
                 data=titanic, saturation=.5,
                 kind="bar", ci=None, aspect=.6)

for ax in g.axes.flat:
    ax.yaxis.set_major_formatter(PercentFormatter(1))
plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM