Datetime issue with matplotlib

前端 未结 5 1105
离开以前
离开以前 2021-01-18 02:09

I\'m pulling my hair to display a series with matplotlib.

I\'m working with python 2.7. I have a pandas Dataframe with dates. I converted dates to datetime and I\'m

相关标签:
5条回答
  • 2021-01-18 02:41

    I faced a similar issue. You could try this:

    df2bis.set_index('dateObs', inplace=True)
    df2bis.index = pd.to_datetime(df2bis.index)
    df2bis.plot()
    
    0 讨论(0)
  • 2021-01-18 02:42

    As i am not able to reproduce the error with the given code i can only guess what causing this issue. The most common one will be fixed as follows:

    One possible reason for your error could be the fact that matplotlib doesn't like the datetime64 datatype. if you replace:

    df2bis.plot()
    

    with

    plt.plot(df2bis.index.to_pydatetime(), df2bis.NbFluxEntrant)
    

    that should be fixed.

    The other option are NaN inside your DataFrame so drop all NaN before plotting

    plt.plot(df2bis.index.to_pydatetime().dropna(), df2bis.NbFluxEntrant.dropna())
    

    Another poitn that could cause the issue is that soemthing goes wrong with the default format. Just make sure you add the format to_datetime otherwise it could change up day and month

    df2bis.index = pd.to_datetime(df2bis.index, format="%Y-%m-%d")
    
    0 讨论(0)
  • 2021-01-18 02:47

    As others could not reproduce this ValueError, my guess is that you transformed df2bis to a pandas.Series somewhere and tried to plot the Series, that would throw:

    ValueError: 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
    

    To fix this, try

    pd.DataFrame(df2bis).plot()
    

    Good luck!

    0 讨论(0)
  • 2021-01-18 02:47

    There are missing values between the datetimes.

    idx = pd.date_range(pd.to_datetime(first_month).item(), pd.to_datetime(last_month).item(), freq='MS')
    
    data = data.reindex(idx)
    
    0 讨论(0)
  • Try clearing your previous plot settings with plt.close(). This solved my issue.

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