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

Python ValueError:视图限制最小值-35738.3640567小于1,是无效的Matplotlib日期值,python,pandas,matplotlib,Python,Pandas,Matplotlib,我是matplotlib的初学者。我正在尝试使用matplotlib.pyplot绘制数据帧。问题是,每次我尝试绘制它时,都会出现以下错误: ValueError:视图限制最小值-35738.3640567小于1,是无效的Matplotlib日期值。如果将非日期时间值传递给具有日期时间单位的轴,则通常会发生这种情况。 根据错误,似乎datetime列中有一个非datetime值,但没有 我尝试使用pd.to_datetime()并尝试将时间戳的格式更改为pd.to_datetime(df_go

我是matplotlib的初学者。我正在尝试使用matplotlib.pyplot绘制数据帧。问题是,每次我尝试绘制它时,都会出现以下错误:

ValueError:视图限制最小值-35738.3640567小于1,是无效的Matplotlib日期值。如果将非日期时间值传递给具有日期时间单位的轴,则通常会发生这种情况。
根据错误,似乎datetime列中有一个非datetime值,但没有

我尝试使用pd.to_datetime()并尝试将时间戳的格式更改为
pd.to_datetime(df_google['datetime',format='%d/%m/%Y')

这是我试图使用的代码:

导入matplotlib.pyplot作为plt
df_google.plot()
plt.show()
df_google是一个数据框,包含列
['datetime','price']
,其中一些值如下:

datetime价格
0  2018-05-15  1079.229980
1  2018-05-16  1081.770020
2  2018-05-17  1078.589966
3  2018-05-18  1066.359985
4  2018-05-21  1079.579956
5  2018-05-22  1069.729980
6  2018-05-23  1079.689941
7  2018-05-24  1079.239990
8  2018-05-25  1075.660034
9  2018-05-29  1060.319946
有人能帮我理解这类错误吗?当每个值都是datetime类型的值时,为什么会说有一个非datetime值?如何绘制此数据帧?

'datetime'
列设置为
datetime64[ns]
类型:
  • 使用
    pandas.to_datetime
    转换
    'datetime'
    列,并记住将该列分配回自身,因为这不是就地更新
  • 如果列标题中没有空格,则可以使用
    访问列名
    • df_google.datetime
      而不是
      df_google['datetime']
将熊猫作为pd导入
将matplotlib.pyplot作为plt导入
#鉴于以下数据
数据={'datetime':['2018-05-15','2018-05-16','2018-05-17','2018-05-18','2018-05-21','2018-05-22','2018-05-23','2018-05-24','2018-05-25','2018-05-29'],
‘价格’:[1079.229981081.77002、1078.589966、1066.359985、1079.5799561069.72998、1079.6899411079.23991075.6600341060.319946])
df_google=pd.DataFrame(数据)
#将datetime列转换为datetime类型,并将其分配回该列
df_google.datetime=pd.to_datetime(df_google.datetime)
#显示(df_google.head())
日期时间价格
0  2018-05-15  1079.229980
1  2018-05-16  1081.770020
2  2018-05-17  1078.589966
3  2018-05-18  1066.359985
4  2018-05-21  1079.579956
5  2018-05-22  1069.729980
6  2018-05-23  1079.689941
7  2018-05-24  1079.239990
8  2018-05-25  1075.660034
9  2018-05-29  1060.319946
验证
'datetime'
是否为
datetime64[ns]
d类型:
print(df_google.info())
范围索引:10个条目,0到9
数据列(共2列):
#列非空计数数据类型
---  ------    --------------  -----         
0日期时间10非空日期时间64[ns]
1价格10非空浮动64
数据类型:datetime64[ns](1),float64(1)
内存使用:288.0字节
绘图:
df_google.plot(x='datetime')
plt.show()
  • 有大量可供选择的绘图工具,但是
    df.plot()
    可以查看数据