Matplotlib的日期时间问题 [英] Datetime issue with matplotlib

查看:127
本文介绍了Matplotlib的日期时间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 matplotlib 展示一个系列.

我正在使用python 2.7.我有一个带日期的熊猫数据框.我将日期转换为日期时间,并试图显示未成功的图.

将pandas导入为pd导入matplotlib.pyplot作为plt从datetime导入datetime,日期#我之前导入了df类型(df)<class 'pandas.core.frame.DataFrame'># 数据框AnneeObs MoisObs JourObs NbFluxEntrant0 2019 8 19 7631 2019年8 1 1098………………655 2017 11 1 428656 2017 11 13 1530[657 行 x 4 列]#将日期转换为日期时间对于范围(0,len(df))中的i:df.loc [i,"dateObs"] = date(year = df.iloc [i,0],month = df.iloc [i,1],天=df.iloc[i,2])df['dateObs'] = pd.DatetimeIndex(df['dateObs'])# 按日期排序df = df.sort_values("dateObs", 升序=真)df2 = df[["dateObs", "NbFluxEntrant"]]df2bis = df2.copy()df2bis = df2bis.set_index('dateObs')df2bis#新的数据框NbFlux 入口日期对象2017-11-01 4282017-11-02 931……2019-08-18 2432019-08-19 763[657行x 1列]#也许太多了...df2bis.index = pd.to_datetime(df2bis.index)类型(df2bis.index)<class 'pandas.core.indexes.datetimes.DatetimeIndex'># 我试图显示它但是...df2bis.plot()

但是,它不起作用:

<块引用>

第一行执行失败:df2bis.plot()
ValueError:视图限制最小值0.0小于1,并且是无效的Matplotlib日期值.如果您将非日期时间值传递给具有日期时间单位的轴,通常会发生这种情况

我尝试了其他代码,但没有任何好处:

df2bis.plot(x="dateObs", y="NbFluxEntrant")df2bis.plot(x=df2bis.index.values, y="NbFluxEntrant")

你有什么建议吗?

预先感谢

编辑:我尝试了另一个无效的代码:

  import matplotlib日期 = matplotlib.dates.date2num(df2bis.index)日期

<块引用>

数组([736634., 736635.,..., 737289., 737290.])

  matplotlib.pyplot.plot_date(dates,df2bis ["NbFluxEntrant"])

<块引用>

ValueError:视图限制最小值0.0小于1,并且是无效的Matplotlib日期值.如果您将非日期时间值传递给具有日期时间单位的轴,通常会发生这种情况

嗯...

解决方案

尝试使用plt.close()清除以前的绘图设置.这解决了我的问题.

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 trying to display a plot without success.

import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime, date

#I imported df before

type(df)
<class 'pandas.core.frame.DataFrame'>

# The dataframe
AnneeObs  MoisObs  JourObs  NbFluxEntrant
0        2019        8       19            763
1        2019        8        1           1098
...        ...      ...      ...            ...
655      2017       11        1            428
656      2017       11       13           1530

[657 rows x 4 columns]

# To convert dates to datetime
for i in range(0,len(df)):
     df.loc[i,"dateObs"] = date(year=df.iloc[i,0], month=df.iloc[i,1], 
day=df.iloc[i,2])

df['dateObs'] = pd.DatetimeIndex(df['dateObs'])

# To order by dates
df = df.sort_values("dateObs", ascending=True)

df2 = df[["dateObs", "NbFluxEntrant"]]

df2bis = df2.copy()
df2bis = df2bis.set_index('dateObs')

df2bis

# The new dataframe
            NbFluxEntrant
dateObs                  
2017-11-01            428
2017-11-02            931
...                   ...
2019-08-18            243
2019-08-19            763

[657 rows x 1 columns]

# Maybe it's too much...
df2bis.index = pd.to_datetime(df2bis.index)

type(df2bis.index)
<class 'pandas.core.indexes.datetimes.DatetimeIndex'>

# I tried to display it but...
df2bis.plot()

However, it doesn't work :

Fail to execute line 1: df2bis.plot()
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

I tried others code but nothing good :

df2bis.plot(x="dateObs", y="NbFluxEntrant")
df2bis.plot(x=df2bis.index.values, y="NbFluxEntrant")

Do you have any suggestions ?

Thanks in advance

EDIT : I tried another code that doesn't work :

import matplotlib

dates = matplotlib.dates.date2num(df2bis.index)
dates

array([736634., 736635.,..., 737289., 737290.])

matplotlib.pyplot.plot_date(dates, df2bis["NbFluxEntrant"])

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

Hmmm...

解决方案

Try clearing your previous plot settings with plt.close(). This solved my issue.

这篇关于Matplotlib的日期时间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆