我是matplotlib的初学者。我试图使用matplotlib.pyplot绘制一个 Dataframe 。问题是每次我试图绘制它时,我都会得到以下错误:
ValueError: view limit minimum -35738.3640567 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.
根据错误,似乎在日期时间列中有一个非日期时间值,但实际上没有。
我试过使用pd.to_datetime(),并尝试将时间戳的格式更改为pd.to_datetime(df_google['datetime'], format = '%d/%m/%Y')
,但没有任何变化。
这是代码IM尝试用途:
import matplotlib.pyplot as plt
df_google.plot()
plt.show()
df_google是一个包含['datetime','price']
列的 Dataframe ,其中一些值如下所示:
datetime price
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
有人能帮我理解这种错误吗?为什么每个值都是日期时间类型的值,但它说有一个非日期时间值?我该如何绘制这个 Dataframe ?
2条答案
按热度按时间ktca8awb1#
'datetime'
列设置为datetime64[ns] Dtype
以解决错误:pandas.to_datetime
转换'datetime'
列,并记住将该列重新赋值给它自己,因为这不是就地更新。pandas
擅长于计算日期时间的格式,但是可能需要使用format=
选项来 * 指定'datetime'
列的当前格式 *。index
、count
),则可以使用.
访问列名。df_google.datetime
而不是df_google['datetime']
验证
'datetime'
列是否为datetime64[ns]
数据类型:图:
x=...
轴。df.set_index('datetime', inplace=True)
把要作为x轴的列设置为索引,然后用df.plot()
来绘图,但不一定要设置索引,也与误差无关。df.plot()
是很好的获得数据的外观。注:
pandas
和matplotlib
已更新。conda upate --all
更新。不要使用pip
更新随Anaconda一起安装的软件包。c8ib6hqw2#
我刚才也遇到了同样的问题,输出是
我为x值设置了
int
限制,尽管我的“Release_Date”列中有pandas._libs.tslibs.timestamps.Timestamp
值