python - 创建 sns.pointplot 时仅从 datetimeindex 提取小时:分钟到 x 轴
问题描述
我有一个看起来像这样的数据框:
deploy deployed_today_rent total_rent cum_deploy hourly percent cum_percent
10min
2019-10-01 05:30:00 6 0 0 6 0.000000 0.000000
2019-10-01 05:40:00 0 0 0 6 0.000000 0.000000
2019-10-01 05:50:00 6 0 0 12 0.000000 0.000000
2019-10-01 06:00:00 13 0 0 25 0.000000 0.000000
2019-10-01 06:10:00 0 0 0 25 0.000000 0.000000
2019-10-01 06:20:00 0 1 1 25 0.040000 0.040000
2019-10-01 06:30:00 0 0 0 25 0.000000 0.040000
2019-10-01 06:40:00 0 1 1 25 0.040000 0.080000
2019-10-01 06:50:00 1 1 1 26 0.038462 0.118462
从这里我试图创建一个点图,其中 x 轴是日期时间,y 轴是 deploy_today_rent。
我创建可视化的代码:
fig,(ax1, ax2)= plt.subplots(nrows=2)
fig.set_size_inches(22,17)
sns.pointplot(data=test, x=test.index, y="total_rent", ax=ax1,color="blue")
sns.pointplot(data=test, x=test.index, y="deployed_today_rent", ax=ax1, color="green")
ax1.set_xticklabels(test.index, rotation=90,
fontdict={
"fontsize":16,
"fontweight":30
})
我在一个图中有两个轴,现在因为 x 轴刻度是完整的日期时间,并且它旋转了 90 度,整个刻度名称没有显示,我只想从中提取05:30:00
并2019-10-01 05:30:00
在 x 刻度上使用它。我怎样才能做到这一点?
同样在上面的ax1.set_xticklabels
font_weight 不起作用。
解决方案
而不是test.index
在你的情节线中,使用test.index.strftime('%H:%M:%S')
它应该让你只Hours:Minutes:Seconds
从索引中获取。
你的代码应该是
sns.pointplot(data=test, x=test.index.strftime('%H:%M:%S'), y="total_rent", ax=ax1,color="blue")
sns.pointplot(data=test, x=test.index.strftime('%H:%M:%S'), y="deployed_today_rent", ax=ax1, color="green")