【发布时间】:2020-10-09 21:06:25
【问题描述】:
我正在尝试将 x-tick 标签的格式修改为日期格式 (%m-%d)。
我的数据包含特定日期期间的每小时数据值。我正在尝试绘制 14 天的数据。但是,当我跑步时,我得到 x 个标签完全混乱。
有什么方法可以只显示日期并跳过 x 轴上的每小时值。 ?有没有办法修改 x 刻度,我可以跳过几个小时的标签并只显示日期的标签?我正在使用 seaborn。
根据评论的建议,我编辑了我的代码以绘制如下:
fig, ax = plt.pyplot.subplots()
g = sns.barplot(data=data_n,x='datetime',y='hourly_return')
g.xaxis.set_major_formatter(plt.dates.DateFormatter("%d-%b"))
但我收到以下错误:
ValueError: DateFormatter found a value of x=0, which is an illegal
date; this usually occurs because you have not informed the axis that
it is plotting dates, e.g., with ax.xaxis_date()
检查日期时间列后,我得到以下输出,该列的数据类型类型为:
0 2020-01-01 00:00:00
1 2020-01-01 01:00:00
2 2020-01-01 02:00:00
3 2020-01-01 03:00:00
4 2020-01-01 04:00:00
...
307 2020-01-13 19:00:00
308 2020-01-13 20:00:00
309 2020-01-13 21:00:00
310 2020-01-13 22:00:00
311 2020-01-13 23:00:00
Name: datetime, Length: 312, dtype: datetime64[ns]
我怀疑 x 刻度,所以当我运行 g.get_xticks() [获取 x 轴上的刻度] 时,我得到了序数形式的输出。谁能说出为什么会这样?
【问题讨论】:
标签: python data-visualization seaborn