绘制日期时间

Plotting datetime

我正在处理日期时间 end_date 和数字 trip_length 的数据集。

data = {'end_date': '2015-02-19 15:46:00', 'trip_length': 300}`)
data['end_date'] = pd.to_datetime(data['end_date'])`
data['end_just_date'] = data.end_date.dt.date

import seaborn as sns
sns.lmplot('end_just_date', 'trip_length', data=data)

我想为给定年份的所有天数绘制 trip_length 的散点图。我创建了一个只有日期的列,但是当我将它绘制为我的 x 变量时,出现以下错误:

TypeError: float() argument must be a string or a number

有没有办法将日期转换为基于月份的天数整数?

Edit2:M. Waskom 建议有更好的方法:

g = sns.FacetGrid(less_than_two_days)
g = (g.map(plt.scatter,'trip_length','end_date'))
plt.show()

编辑:您可以像这样在 x 轴上绘制年、月和日:

less_than_two_days['end_yyyymmdd'] = (
    less_than_two_days['end_date'].dt.year.astype(str) + 
    less_than_two_days['end_date'].dt.month.astype(str).str.zfill(2) + 
    less_than_two_days['end_date'].dt.day.astype(str).str.zfill(2))

less_than_two_days['end_yyyymmdd'] = less_than_two_days['end_yyyymmdd'].astype(int)

然后用日期标记 x 轴,执行:

ax = sns.lmplot('end_yyyymmdd', 'trip_length', data=df)
ax.set_xticklabels(df['end_date'])

顺便说一句,这感觉像是一种解决方法的原因是:https://github.com/mwaskom/seaborn/issues/257


旧答案:看起来您的 DataFrame 名为 less_than_two_days。如果是这样,您可以这样做以在自己的列中获取一年中的某一天:

less_than_two_days['end_day_of_year'] = less_than_two_days['end_date'].dt.dayofyear

然后开始你的剧情:

sns.lmplot('end_day_of_year', 'trip_length', data=less_than_two_days)