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

jhkqcmku  于 2022-12-25  发布在  其他
关注(0)|答案(2)|浏览(127)

我是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 ?

ktca8awb

ktca8awb1#

  • 'datetime'列设置为datetime64[ns] Dtype以解决错误:
  • 使用pandas.to_datetime转换'datetime'列,并记住将该列重新赋值给它自己,因为这不是就地更新。
  • pandas擅长于计算日期时间的格式,但是可能需要使用format=选项来 * 指定'datetime'列的当前格式 *。
  • 如果列名不包含特殊字符,并且不与内置属性/方法冲突(例如indexcount),则可以使用.访问列名。
  • df_google.datetime而不是df_google['datetime']
import pandas as pd
import matplotlib.pyplot as plt

# given the following data
data = {'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'],
        'price': [1079.22998, 1081.77002, 1078.589966, 1066.359985, 1079.579956, 1069.72998, 1079.689941, 1079.23999, 1075.660034, 1060.319946]}

df_google = pd.DataFrame(data)

# convert the datetime column to a datetime type and assign it back to the column
df_google.datetime = pd.to_datetime(df_google.datetime)

# display(df_google.head())
     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

验证'datetime'列是否为datetime64[ns]数据类型:

print(df_google.info())

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10 entries, 0 to 9
Data columns (total 2 columns):
 #   Column    Non-Null Count  Dtype         
---  ------    --------------  -----         
 0   datetime  10 non-null     datetime64[ns]
 1   price     10 non-null     float64       
dtypes: datetime64[ns](1), float64(1)
memory usage: 288.0 bytes

图:

  • 指定列作为要打印的x=...轴。
df_google.plot(x='datetime')
plt.show()
  • 也可以先用df.set_index('datetime', inplace=True)把要作为x轴的列设置为索引,然后用df.plot()来绘图,但不一定要设置索引,也与误差无关。
  • 有大量的替代绘图工具,但df.plot()是很好的获得数据的外观。
  • PyViz

注:

c8ib6hqw

c8ib6hqw2#

我刚才也遇到了同样的问题,输出是

view limit minimum 0.0 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

我为x值设置了int限制,尽管我的“Release_Date”列中有pandas._libs.tslibs.timestamps.Timestamp

with sns.axes_style('darkgrid'):
  ax = sns.scatterplot(data=data_clean,
                  # Release_Date column has pandas._libs.tslibs.timestamps.Timestamp in it
                  x='Release_Date',
                  y='USD_Worldwide_Gross',
                  )

  ax.set(ylim=(0, 3000000000),
        # xlim caused problem, because of int values
        xlim=(0, 450000000))

  plt.show()

相关问题