【问题标题】:ValueError: view limit minimum -5.1000000000000005 is less than 1 and is an invalid Matplotlib date valueValueError: view limit minimum -5.1000000000000005 小于 1 并且是无效的 Matplotlib 日期值
【发布时间】:2019-03-05 20:08:53
【问题描述】:

我有一个 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

这没有任何意义,因为我手动将所有时间戳字符串转换为日期时间对象

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'>

我不知道如何解决这个问题。我尝试使用 plt.xlim(cpu_data[cpu_data.columns[0]].iloc[0]) 手动设置 x 轴以从日期时间对象值开始,但这会产生相同的错误。我真的迷路了。任何指导将不胜感激。如果有帮助,我可以提供更多信息。

编辑:

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

【问题讨论】:

  • 如果你使用cpu_data.plot(ax=ax, x_compat=True)会发生什么?不过,我只能重复我之前的评论:此处显示的数据并不容易提供帮助。见How to make good reproducible pandas examples
  • 那么,为什么当我从字符串 '01:30:04' 转换时间戳时,它不会变成 datetime 对象,而是变成 pandas._libs.tslibs.timestamps.Timestamp 对象?我想这是造成它的原因。
  • Pandas 喜欢以自己的格式存储日期时间。 本身并没有错,正如您已经发现的那样(例如,当使用ax.plot 时),matplotlib 完全有能力将其转换为自己的目的。 pandas 本身也会通过df.plot() 处理这些问题。您遇到的问题是您想对使用 pandas 定位器创建的绘图使用 matplotlib 日期格式化程序。如果上述解决方案 (x_compat) 没有帮助,我(或我想的其他人)将需要一个 minimal reproducible example 的问题。
  • 没关系,我决定只使用matplotlib.pyplot 老式的方式来绘制。我不确定为什么它不能使用 pandas 但它现在可以工作了。修改完成后,我会稍后发布代码。

标签: python pandas matplotlib


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-11-28
    • 2019-08-27
    • 2019-03-22
    • 2021-07-11
    • 1970-01-01
    相关资源
    最近更新 更多