matplotlib - python - Pyton中日期时间类型的绘图

我尝试plot的"time"特性的分布,这是我的dataframe:


date amount


40 2018-12-31 104.588094


184 2019-01-01 2487.714459


80 2019-01-02 250.405264


10 2019-01-03 217.385350


170 2019-01-04 208.067521


110 2019-01-05 176.668830


73 2019-01-06 311.626595


145 2019-01-07 283.241003


139 2019-01-08 284.652009



日期列是date类型,这是我的代码:


#visualizations of time 


plt.figure(figsize=(10,8))


plt.title('Distribution of Time Feature')


sns.distplot(montant_df_pandas['dateTransmission'])



但我得到了这个错误:

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in 2 plt.figure(figsize=(10 ,8)) 3 plt.title('Distribution of Time Feature') ---- > 4 sns.distplot(montant_df_pandas ['dateTransmission'] )

~/anaconda3/envs/naboo-env/lib/python3.6/site-packages/seaborn/distributions.py in distplot(a ,bins ,hist ,kde ,rug ,fit ,hist_kws ,kde_kws ,rug_kws ,fit_kws ,color ,vertical ,norm_hist ,axlabel ,label ,ax) 196 line ,= ax.plot(0 ,a.mean()) 197 else : -- > 198 line ,= ax.plot(a.mean() ,0) 199 color = line.get_color() 200 line.remove( )

~/anaconda3/envs/naboo-env/lib/python3.6/site-packages/numpy/core/_methods.py in _mean(a ,axis ,dtype ,out ,keepdims) 73 is_float16_result = True 74 --- > 75 ret = umr_sum(arr ,axis ,dtype ,out ,keepdims) 76 if isinstance(ret ,mu.ndarray) : 77 ret =

TypeError : ufunc add cannot use operands with types dtype('

时间:

这似乎是numpy与其他库之间的日期转换的问题,看这个链接 ,我尝试了一个简单的解决问题的方法


from datetime import datetime, timedelta


import matplotlib.pyplot as plt


import pandas as pd


import numpy as np


date_today = datetime.now()


days = pd.date_range(date_today, date_today + timedelta(7), freq='D')



np.random.seed(seed=1111)


data = np.random.randint(1, high=100, size=len(days))


df = pd.DataFrame({'Date': days, 'Amount': data})



df.set_index('Date')['Amount'].plot(legend=True)




Image of the output is attached here

...