【问题标题】:How to format the y- or x-axis labels in a seaborn FacetGrid如何在 seaborn FacetGrid 中格式化 y 轴或 x 轴标签
【发布时间】:2020-12-21 22:03:38
【问题描述】:

我想在 seaborn FacetGrid 图中格式化 y 轴标签,带有多个小数,和/或添加一些文本。

import seaborn as sns
import matplotlib.pyplot as plt
sns.set(style="ticks")

exercise = sns.load_dataset("exercise")

g = sns.catplot(x="time", y="pulse", hue="kind", col="diet", data=exercise)
#g.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: '{:,.2f}'.format(x) + 'K'))
#g.set(xticks=['a','try',0.5])
g.yaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: '{:,.2f}'.format(x) + 'K'))
plt.show()

灵感来自How to format seaborn/matplotlib axis tick labels from number to thousands or Millions? (125,436 to 125.4K)

ax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: '{:,.2f}'.format(x) + 'K'))

这会导致以下错误。

AttributeError: 'FacetGrid' object has no attribute 'xaxis'

【问题讨论】:

    标签: python seaborn facet-grid


    【解决方案1】:
    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    import matplotlib.ticker as tkr
    
    sns.set(style="ticks")
    
    # load data
    exercise = sns.load_dataset("exercise")
    
    # plot data
    g = sns.catplot(x="time", y="pulse", hue="kind", col="diet", data=exercise)
    
    # format the labels with f-strings
    for ax in g.axes.flat:
        ax.yaxis.set_major_formatter(tkr.FuncFormatter(lambda y, p: f'{y:.2f}: Oh baby, baby'))
        ax.xaxis.set_major_formatter(tkr.FuncFormatter(lambda x, p: f'{x}: Is that your best'))
    

    # format the labels with f-strings
    for ax in g.axes.flat:
        ax.yaxis.set_major_formatter(lambda y, p: f'{y:.2f}: Oh baby, baby')
        ax.xaxis.set_major_formatter(lambda x, p: f'{x}: Is that your best')
    

    【讨论】:

    • 感谢您的明确回答,并花时间解释:非常有用。
    猜你喜欢
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 2019-08-02
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多