带有DateTime X轴的Matplotlib/Seaborn Heatmap显示了1970年,而不是带有自动效应的真实DateTime

发布于 2025-01-24 14:42:41 字数 1346 浏览 5 评论 0原文

我有一个带有以下枢轴表的热图:

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:

enter image description here

Any help appreciated! Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦魇绽荼蘼 2025-01-31 14:42:42

尽管我没有发现为什么自动构造法人不起作用,但我找到了另一种做我预期的方法:

x_labels = plot_df.index[::10].tolist()
plt.xticks(range(0,len(plot_df.index),10), x_labels)
fig.autofmt_xdate()

删除以下内容:

locator = mdates.AutoDateLocator()
formatter = mdates.AutoDateFormatter(locator)
axHM.xaxis.set_major_locator(locator)
axHM.xaxis.set_major_formatter(formatter)

Although I did not find out why AutoDateFormatter would not work, I find another way to do what I intended with:

x_labels = plot_df.index[::10].tolist()
plt.xticks(range(0,len(plot_df.index),10), x_labels)
fig.autofmt_xdate()

And removing the following:

locator = mdates.AutoDateLocator()
formatter = mdates.AutoDateFormatter(locator)
axHM.xaxis.set_major_locator(locator)
axHM.xaxis.set_major_formatter(formatter)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文