带有DateTime X轴的Matplotlib/Seaborn Heatmap显示了1970年,而不是带有自动效应的真实DateTime
我有一个带有以下枢轴表的热图:
HCpredictedLabel B H R
datetime
2021-07-20 09:00:00 115.0 80.0 119.0
2021-07-20 09:05:00 69.0 38.0 149.0
2021-07-20 09:10:00 58.0 50.0 131.0
2021-07-20 09:15:00 71.0 31.0 162.0
2021-07-20 09:20:00 78.0 38.0 164.0
... ... ... ...
2021-07-21 07:35:00 3.0 10.0 14.0
2021-07-21 07:40:00 9.0 1.0 30.0
2021-07-21 07:45:00 8.0 3.0 31.0
2021-07-21 07:50:00 12.0 13.0 26.0
2021-07-21 07:55:00 25.0 35.0 97.0
[114 rows x 3 columns]
热图的X轴显示了DateTime,我希望自动缩放,具体取决于数据集中的范围(可能是几天,几周甚至几个月)。
这是我当前的脚本:
import seaborn as sns
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.dates import AutoDateFormatter, AutoDateLocator
plot_df.fillna(0,inplace=True)
fig, axHM = plt.subplots(1,1,figsize=(30,5))
axHM = sns.heatmap(plot_df.T, cmap='rocket')
locator = mdates.AutoDateLocator()
formatter = mdates.AutoDateFormatter(locator)
axHM.xaxis.set_major_locator(locator)
axHM.xaxis.set_major_formatter(formatter)
fig.autofmt_xdate()
我得到了这个图,x轴以一种不错的格式显示了日期,但从1970年开始,从表格上看,我的日期可以追溯到表格:
i.sstatic.net/n8kah.png“ rel =“ nofollow noreferrer 帮助赞赏!谢谢
I have a heatmap produced with the following pivot table:
HCpredictedLabel B H R
datetime
2021-07-20 09:00:00 115.0 80.0 119.0
2021-07-20 09:05:00 69.0 38.0 149.0
2021-07-20 09:10:00 58.0 50.0 131.0
2021-07-20 09:15:00 71.0 31.0 162.0
2021-07-20 09:20:00 78.0 38.0 164.0
... ... ... ...
2021-07-21 07:35:00 3.0 10.0 14.0
2021-07-21 07:40:00 9.0 1.0 30.0
2021-07-21 07:45:00 8.0 3.0 31.0
2021-07-21 07:50:00 12.0 13.0 26.0
2021-07-21 07:55:00 25.0 35.0 97.0
[114 rows x 3 columns]
The x-axis of the heatmap shows the datetime, which I would like to be automatically scaled, depending on the range in the dataset (can be days, weeks, or even months).
Here is my current script:
import seaborn as sns
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.dates import AutoDateFormatter, AutoDateLocator
plot_df.fillna(0,inplace=True)
fig, axHM = plt.subplots(1,1,figsize=(30,5))
axHM = sns.heatmap(plot_df.T, cmap='rocket')
locator = mdates.AutoDateLocator()
formatter = mdates.AutoDateFormatter(locator)
axHM.xaxis.set_major_locator(locator)
axHM.xaxis.set_major_formatter(formatter)
fig.autofmt_xdate()
I get this plot, with the x-axis showing dates in a nice format but at a completely wrong scale and from 1970, rather that my datetime dates from the table:
Any help appreciated! Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管我没有发现为什么自动构造法人不起作用,但我找到了另一种做我预期的方法:
删除以下内容:
Although I did not find out why AutoDateFormatter would not work, I find another way to do what I intended with:
And removing the following: