ValueError: view limit minimum -35738.3640567 is less than 1 and is an invalid Matplotlib date value

前端 未结 1 921
没有蜡笔的小新
没有蜡笔的小新 2021-01-11 14:43

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:



        
1条回答
  •  说谎
    说谎 (楼主)
    2021-01-11 15:33

    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 there are no spaces in the column header
      • 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' is a datetime64[ns] Dtype:

    print(df_google.info())
    
    
    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.
    • PyViz

    0 讨论(0)
提交回复
热议问题