python – 将索引显示为pandas plot的xticks
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 将索引显示为pandas plot的xticks,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1513字,纯文字阅读大概需要3分钟。
内容图文
我有以下数据框,我试图绘制它,以便它在x轴显示8-19的索引数据.
如果我这样做df.plot()根本没有显示任何标签.如果我执行df.plot(use_index = True),则行为不变.最后我尝试了df.plot(xticks = df.index),但是我得到一个错误AttributeError:’NoneType’对象没有属性’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.
任何建议表示赞赏.
解决方法:
如果你想将字符串作为xticks,一个可能的解决方案是:
df = df.reset_index()
df = df.rename(columns={"index":"hour"})
ax = df.plot(xticks=df.index)
ax.set_xticklabels(df["hour"]);
内容总结
以上是互联网集市为您收集整理的python – 将索引显示为pandas plot的xticks全部内容,希望文章能够帮你解决python – 将索引显示为pandas plot的xticks所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。