【问题标题】:How do I change the y axis on facetgrid python to percentage?如何将 facetgrid python 上的 y 轴更改为百分比?
【发布时间】:2018-07-29 08:56:43
【问题描述】:

Python matplotlib sns facetgrid y轴问题

你好!

我一直在学习使用 python 进行数据分析,但我遇到了一些我找不到答案的问题。

我想将我的 sns facegrid 图表上的 y 轴更改为百分比。下面是代码和图片...

g = sns.FacetGrid(data, col = 'Survived')
g.map(plt.hist, 'Age', bins=20)

这是输出:

【问题讨论】:

    标签: python charts histogram data-analysis yaxis


    【解决方案1】:

    你可以使用density=True,见docs

    然后将 y 标签更改为百分比,例如 example

    结果;

    import seaborn as sns
    import matplotlib.pyplot as plt
    tips = sns.load_dataset("tips")
    g = sns.FacetGrid(tips, col='smoker', col_order=['Yes','No'])
    g.map(plt.hist, 'total_bill', density=True, bins=20, color='m')
    
    from matplotlib.ticker import FuncFormatter
    def to_percent(y, position):
        s = str(100*y)
        return s + '%'
    formatter = FuncFormatter(to_percent)
    plt.gca().yaxis.set_major_formatter(formatter)
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2018-10-05
      • 2015-02-10
      • 1970-01-01
      • 2015-09-30
      • 2021-12-22
      • 2021-08-03
      相关资源
      最近更新 更多