Python ValueError:视图限制最小值-5.10000000000005小于1,并且是无效的Matplotlib日期值

Python ValueError:视图限制最小值-5.10000000000005小于1,并且是无效的Matplotlib日期值,python,pandas,matplotlib,Python,Pandas,Matplotlib,我有一个pandas数据框,其中包含一些sar输出,我想在matplotlib中绘制。样本数据如下 >>> 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

我有一个pandas数据框,其中包含一些sar输出,我想在matplotlib中绘制。样本数据如下

>>> 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
但是我得到了以下错误

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'>
2018-09-30 00:10:01
2018-09-30 00:20:01     
2018-09-30 00:30:01     
2018-09-30 00:40:01     
2018-09-30 00:50:01     
2018-09-30 01:00:01     
我不知道如何解决这个问题。我曾尝试使用
plt.xlim(cpu\u data[cpu\u data.columns[0]]].iloc[0])
手动将x轴设置为从datetime对象值开始,但这会产生相同的错误。我在这里真的迷路了。任何指导都将不胜感激。如果有帮助的话,我可以提供更多的信息

编辑:


我认为日期不是正确的数据类型(如错误所示)。熊猫似乎一直在将时间列(第0列)中的数据转换为
pandas.\u libs.tslibs.timestamps.Timestamp
类型的on对象。我认为它应该是matplotlib所抱怨的datetime对象。

对于那些感兴趣的人来说,这就是我最终使用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()

如果使用
cpu\u data.plot(ax=ax,x\u compat=True)
,会发生什么情况?不过,我只能重复我之前的评论:这里显示的数据并不容易提供帮助。看。那么,为什么当我从字符串“01:30:04”转换时间戳时,它不是一个
datetime
对象,而是一个
pandas.\u libs.tslibs.timestamps.Timestamp
对象?我想这就是原因。熊猫喜欢以自己的格式存储日期时间。这本身并没有问题,正如您已经发现的(例如,当使用
ax.plot
时),matplotlib完全能够将其转换为自己的目的。熊猫本身也可以通过
df.plot()
处理这些问题。遇到的问题是,要对使用标注创建的绘图使用matplotlib日期格式化程序。如果上面提到的解决方案(
x_compat
)没有帮助,我(或者我想其他人)将需要一个解决方案。不管怎样,我决定用老式的方式使用
matplotlib.pyplot
进行绘图。我不知道为什么使用熊猫时它不起作用,但它现在起作用了。一旦我完成了修补,我会在稍后发布代码。
# 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()