代码之家  ›  专栏  ›  技术社区  ›  evilmandarine

为什么FuncFormatter将字符串(或对象)转换为int?

  •  0
  • evilmandarine  · 技术社区  · 3 年前

    尝试在中将标签设置为大写 Seaborn violin 情节
    当这起作用时:

    test = pd.DataFrame(data={"one": [5, 6, 7, 8], "two": ["a", "b", "c", "d"]})
    ax = sns.violinplot(x=test["two"], y=test["one"])
    labels = [item.get_text().upper() for item in ax.get_xticklabels()]
    ax.set_xticklabels(labels)
    

    这不会:

    test = pd.DataFrame(data={"one": [5, 6, 7, 8], "two": ["a", "b", "c", "d"]})
    ax = sns.violinplot(x=test["two"], y=test["one"])
    fmt = lambda x, _: str(x).upper()
    ax.xaxis.set_major_formatter(mtick.FuncFormatter(fmt))
    

    第二个代码返回0、1、2、3,而不是预期的A、B、C、D。
    为什么?

    我正在按如下方式导入库:

    import pandas as pd  # v1.3.5
    import seaborn as sns  # v0.11.2
    import matplotlib.pyplot as plt  # v3.4.3
    import matplotlib.ticker as mtick  # v3.4.3
    
    0 回复  |  直到 3 年前