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

倾然丶 夕夏残阳落幕 提交于 2021-02-07 08:23:22

问题


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.

Thanks, John

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

回答1:


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



来源:https://stackoverflow.com/questions/52512790/python-seaborn-catplot-how-do-i-change-the-y-axis-scale-to-percentage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!