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

将索引显示为熊猫图的Xticks

  •  5
  • Nickpick  · 技术社区  · 7 年前

    如果我愿意的话 df.plot() df.plot(use_index=True) df.plot(xticks=df.index) 但我有个错误 AttributeError: 'NoneType' object has no attribute 'seq'

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    null = np.nan
    
    df = pd.DataFrame.from_dict({"today sensor 1": {"08": 22.9, "09": 22.7, "10": 22.8, "11": 23.6, "12": 24.1, "13": 24.9,
                                               "14": 25.0, "15": 25.2, "16": 25.7, "17": 26.1, "18": 26.0, "19": 25.8},
                            "today sensor 2": {"08": 24.5, "09": 24.5, "10": 24.8, "11": 25.3, "12": 26.4, "13": 26.7,
                                               "14": 27.1, "15": 27.6, "16": 28.0, "17": 28.0, "18": 28.2, "19": 28.0},
                            "yesterday sensor 1": {"08": null, "09": null, "10": null, "11": null, "12": null, "13": null,
                                                   "14": null, "15": null, "16": 23.0, "17": 23.6, "18": 23.5, "19": 23.5},
                            "yesterday sensor 2": {"08": null, "09": null, "10": null, "11": null, "12": null, "13": null,
                                                   "14": null, "15": null, "16": 24.8, "17": 24.9, "18": 24.9, "19": 24.8}})
    
    # df.plot(use_index=True)  # does not work
    df.plot(xticks=df.index)
    
    plt.show()
    

    ax = df.plot(use_index=True, style=['bs-', 'go-', 'b:', 'g:'])
    ax.set_xticklabels(df.index)
    plt.show()
    

    Xticks会显示,但它们是错误的。只有9-14的数字是下雪的,其他数字都是。我希望08-19能成为Xticks。

    3 回复  |  直到 6 年前
        1
  •  7
  •   rpanai    7 年前

    df = df.reset_index()
    df = df.rename(columns={"index":"hour"})
    ax = df.plot(xticks=df.index)
    ax.set_xticklabels(df["hour"]);
    
        2
  •  1
  •   mapsa    6 年前

    python 3.6(windows)的pandas 0.24版为我解决了这个问题。

        3
  •  0
  •   L. Kärkkäinen    6 年前

    plt.xticks(range(len(df.index)), df.index)