pandas 将索引显示为熊猫图的 xticks

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50805666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:41:22  来源:igfitidea点击:

Showing index as xticks for pandas plot

pythonpandasmatplotlib

提问by Nickpick

I have the following dataframe and am trying to plot it, so that it shows in the x-axis the index data from 8-19.

我有以下数据框并试图绘制它,以便它在 x 轴上显示 8-19 的索引数据。

If I do df.plot()no labels are shown at all. If I do df.plot(use_index=True), the behaviour is unchanged. Finally I tried df.plot(xticks=df.index)but I'm getting an error AttributeError: 'NoneType' object has no attribute 'seq'

如果我不显示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()

What is even more strange is that when I do this:

更奇怪的是,当我这样做时:

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

The xticks will show but they are wrong. Only numbers from 9-14 are snown and every other number only. I would expect 08-19 as xticks.

xticks 会显示,但它们是错误的。只有 9-14 的数字会下雪,并且每隔一个数字才会下雪。我希望 08-19 为 xticks。

Any suggestions are appreciated.

任何建议表示赞赏。

回答by rpanai

If you want to have string as xticks one possible solution is:

如果您想将字符串作为 xticks,一种可能的解决方案是:

df = df.reset_index()
df = df.rename(columns={"index":"hour"})
ax = df.plot(xticks=df.index)
ax.set_xticklabels(df["hour"]);

回答by L. K?rkk?inen

Until the bug in Pandas gets fixed, add this after df.plot(), no need for anything else:

在 Pandas 中的错误得到修复之前,在 df.plot() 之后添加这个,不需要其他任何东西:

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

回答by mapsa

Pandas version 0.24 for Python 3.6 (Windows) fixed this problem for me.

适用于 Python 3.6 (Windows) 的 Pandas 0.24 版为我解决了这个问题。