python - ValueError: view limit minimum -5.1000000000000005 小于 1 并且是无效的 Matplotlib 日期值
问题描述
我有一个 pandas 数据框,其中包含一些我想在 matplotlib 中绘制的 sar 输出。示例数据如下。
>>> cpu_data.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 70 entries, 0 to 207
Data columns (total 8 columns):
00:00:01 70 non-null datetime64[ns]
CPU 70 non-null object
%user 70 non-null float64
%nice 70 non-null float64
%system 70 non-null float64
%iowait 70 non-null float64
%steal 70 non-null float64
%idle 70 non-null float64
dtypes: float64(6), object(2)
memory usage: 4.4+ KB
>>> cpu_data
00:00:01 CPU %user %nice %system %iowait %steal %idle
0 00:10:01 all 0.30 0.00 0.30 0.06 0.0 99.34
3 00:20:01 all 0.09 0.00 0.13 0.00 0.0 99.78
6 00:30:01 all 0.07 0.00 0.11 0.00 0.0 99.81
9 00:40:01 all 0.08 0.00 0.11 0.00 0.0 99.80
12 00:50:01 all 0.08 0.00 0.13 0.00 0.0 99.79
15 01:00:04 all 0.09 0.00 0.13 0.00 0.0 99.77
18 01:10:01 all 0.27 0.00 0.28 0.00 0.0 99.46
21 01:20:01 all 0.09 0.00 0.11 0.00 0.0 99.79
24 01:30:04 all 0.12 0.00 0.13 0.01 0.0 99.74
27 01:40:01 all 0.08 0.00 0.11 0.01 0.0 99.80
30 01:50:01 all 0.09 0.00 0.13 0.01 0.0 99.77
我想使用时间戳作为 x 轴进行绘图。我已经编写了以下代码。
import pandas as pd
import os
import matplotlib.pyplot as plt
import matplotlib.dates as md
import dateutil
import matplotlib.dates as mdates
cpu_data[cpu_data.columns[0]] = [dateutil.parser.parse(s) for s in cpu_data[cpu_data.columns[0]]]
plt.subplots_adjust(bottom=0.2)
plt.xticks( rotation=25 )
ax=plt.gca()
ax.xaxis_date()
xfmt = md.DateFormatter('%H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)
cpu_data.plot(ax=ax)
plt.show()
但我收到以下错误
ValueError: view limit minimum -5.1000000000000005 is less than 1 and is an invalid Matplotlib date value. This often happens if you pass a non-datetime value to an axis that has datetime units
这没有任何意义,因为我手动将所有时间戳字符串转换为 datetime 对象
cpu_data[cpu_data.columns[0]] = [dateutil.parser.parse(s) for s in cpu_data[cpu_data.columns[0]]]
但它们似乎不是正确的数据类型
2018-09-30 00:10:01 <class 'pandas._libs.tslibs.timestamps.Timestamp'>
2018-09-30 00:20:01 <class 'pandas._libs.tslibs.timestamps.Timestamp'>
2018-09-30 00:30:01 <class 'pandas._libs.tslibs.timestamps.Timestamp'>
2018-09-30 00:40:01 <class 'pandas._libs.tslibs.timestamps.Timestamp'>
2018-09-30 00:50:01 <class 'pandas._libs.tslibs.timestamps.Timestamp'>
2018-09-30 01:00:01 <class 'pandas._libs.tslibs.timestamps.Timestamp'>
我不知道如何解决这个问题。我尝试手动设置 x 轴以使用日期时间对象值开始,plt.xlim(cpu_data[cpu_data.columns[0]].iloc[0])
但这会产生相同的错误。我真的迷路了。任何指导将不胜感激。如果有帮助,我可以提供更多信息。
编辑:
我认为日期不是正确的数据类型(如错误所示)。似乎 pandas 一直在将时间列(第 0 列)中的数据转换为 on 类型的对象pandas._libs.tslibs.timestamps.Timestamp
。我认为它应该是 matplotlib 抱怨的日期时间对象。
解决方案
对于那些感兴趣的人,这就是我最终使用 matplotlib 绘制数据的方式
# Plot cpu
plt.figure(1)
plt.subplots_adjust(bottom=0.2)
plt.xticks(rotation=25)
ax=plt.gca()
ax.xaxis_date()
xfmt = md.DateFormatter('%H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)
plt.title(f'CPU usage on {remote_host}')
lines = plt.plot(dates, cpu_data[cpu_data.columns[2:]])
ax.legend(lines, [str(col) for col in list(cpu_data.columns[2:])])
plot.show()
推荐阅读
- c++ - C 文件与 C++ 文件的 Eclipse CDT 索引器结果不同
- latex - 基于 RMarkdown 中的输出运行块
- javascript - 比较两个 JavaScript 对象并提取匹配属性的键和值
- python - 将函数映射或应用到单个值 - Python 3 中的正确方法
- gradle - 如何发布和使用构建为 fatJar 的 gradle 插件?
- cocoa - 如何插入 NSBezierPath/UIBezierPath 并在位置检索正常向量?
- python - 测试一个整数是否是切片的索引值
- regex - 基于正则表达式批量修改密码查询中的节点
- javascript - 反应“超过最大更新深度。”
- sip - H323 视频与 Freeswitch mod_h323 可能吗?