Python seaborn catplot - 如何将 y 轴比例更改为百分比 [英] Python seaborn catplot - How do I change the y-axis scale to percentage

查看:108
本文介绍了Python seaborn catplot - 如何将 y 轴比例更改为百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在图中,y 轴标签是从(0 到 1)的小数,即(0.1、0.2、0.4 等).如何将其转换为 % 格式(10%、20%、40% 等).只需10、20、40也可以.

In the figure the y-axis labels are in decimals from (0 to 1) i.e (0.1, 0.2, 0.4 etc). How can I convert this into a % format (10%, 20%, 40% etc). Just 10, 20, 40 also will do.

谢谢,约翰

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

推荐答案

您可以在网格的轴上使用 PercentFormatter .

You may use a PercentFormatter on the axes of the grid.

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()

这篇关于Python seaborn catplot - 如何将 y 轴比例更改为百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆