本文介绍了ValueError: view limit minimum -35738.3640567 小于 1 并且是无效的 Matplotlib 日期值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

ValueError: view limit minimum -35738.3640567 小于 1 并且是无效的 Matplotlib 日期值.如果您将非日期时间值传递给具有日期时间单位的轴,则通常会发生这种情况.

根据错误提示,日期时间列中好像有非日期时间值,但没有.

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

这是我尝试使用的代码:

将 matplotlib.pyplot 导入为 pltdf_google.plot()plt.show()

df_google 是一个包含 ['datetime','price'] 列的数据框,其中一些值如下:

 日期时间价格0 2018-05-15 1079.2299801 2018-05-16 1081.7700202 2018-05-17 1078.5899663 2018-05-18 1066.3599854 2018-05-21 1079.5799565 2018-05-22 1069.7299806 2018-05-23 1079.6899417 2018-05-24 1079.2399908 2018-05-25 1075.6600349 2018-05-29 1060.319946

有人可以帮我理解这种类型的错误吗?当每个值都是日期时间类型值时,为什么它说存在非日期时间值?如何绘制此数据框?

解决方案

'datetime' 列设置为 datetime64[ns] 类型:

  • 使用

    im a beginner in matplotlib. Im trying to plot a dataframe using matplotlib.pyplot. The problem is that everytime I try to plot it i get the following error:

    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.
    
    

    According to the error, it seems to be like theres a non-datetime value in the datetime column, but there isnt.

    Ive tried using pd.to_datetime() and try to change the format of the timestamp to pd.to_datetime(df_google['datetime'], format = '%d/%m/%Y') but nothing changes.

    This is the code im trying to use:

    import matplotlib.pyplot as plt
    
    df_google.plot()
    plt.show()
    

    df_google is a dataframe with columns ['datetime','price'] and some of the values are the following:

         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
    

    Can someone try to help me understand this type of error? Why does it says theres a non-datetime value when every value is a datetime type value? How can I plot this dataframe?

    解决方案

    Set the 'datetime' column to a datetime64[ns] type:

    • Use pandas.to_datetime to convert the 'datetime' column, and remember to assign the column back to itself, because this is not an inplace update.
    • Column names can be accessed with a ., if they do not contain special characters and do not clash with built-in attributes/methods (e.g., index, count).
      • df_google.datetime instead of 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
    

    Verify the 'datetime' column is a datetime64[ns] Dtype:

    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
    

    Plot:

    df_google.plot(x='datetime')
    plt.show()
    

    • There's a substantial ecosystem of alternative plotting tools, but df.plot() is fine for getting a look at the data.

    这篇关于ValueError: view limit minimum -35738.3640567 小于 1 并且是无效的 Matplotlib 日期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 19:57