我有以下数据框并尝试绘制它,以便它在 x 轴上显示 8-19 的索引数据。
If I do 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。
任何建议表示赞赏。