简体   繁体   English

如何使用颜色图在X轴上设置时间范围和在Y轴上设置日期范围

[英]How to set a time range on the X axis and date range in the Y axis with colormap

I have created a code, which shows a heatmap of the data in the CSV file. 我创建了一个代码,该代码显示CSV文件中数据的热图。 The code is as follows: 代码如下:

import pandas as pd
import matplotlib.pyplot as plt
data= pd.read_csv("data.csv" , sep=';', header=0, 
index_col='Date')
fig=plt.imshow(data, cmap='YlOrBr', interpolation='nearest')
plt.colorbar()
plt.xlabel("Time (UTC)")
plt.ylabel("Date")
plt.show()

在此输入图像描述

The dataset is as follows: 数据集如下:

在此输入图像描述

The time range varies from 00:00 till 23:50 with steps of 10 minutes. 时间范围从00:00到23:50,以10分钟为步长。 I want the x axis to show the time from 00:00 till 23:50 in steps per hour. 我希望x轴以小时为单位显示从00:00到23:50的时间。

The index is set as date. 索引设置为日期。 The date range is from 29-Oct-2017 till 24-Mar-2018. 日期范围是从2017年10月29日到2018年3月24日。 I want the Y axis to show the date range in steps of months. 我希望Y轴以月为单位显示日期范围。

You can stack columns, then groupby month and hour and then unstack it back (I'm taking mean values here when aggregating, but you can change to sum or whatever aggregation should be done there): 您可以stack列,然后groupby月,小时,然后unstack回来(我以mean汇总值时这里,但你可以改变,以sum或任何聚合应该在那里进行):

df = pd.DataFrame(np.nan,
                  columns=pd.date_range('00:00', '23:50', freq='10min'),
                  index=pd.date_range('2017-10-29', '2018-03-24'))
df[df.columns] = np.random.randint(0, 100, df.shape)

fig, ax = plt.subplots(2, figsize=(10,6))
ax[0].imshow(df, cmap='YlOrBr')

ix = df.stack().index
l1 = ix.get_level_values(0).month
l2 = ix.get_level_values(1).hour

df2 = df.stack().groupby([l1,l2], sort=False).mean().unstack(1)

ax[1].imshow(df2, cmap='YlOrBr')

Output (original DataFrame above, processed below): 输出(上面的原始DataFrame,下面进行处理):

产量

Update: 更新:

If the goal is just to put monthly and hourly labels on the same plot, please see below: 如果目标只是在同一图上放置每月和每小时标签,请参见以下内容:

df = pd.DataFrame(np.nan,
                  columns=pd.date_range('00:00', '23:50', freq='10min').astype(str),
                  index=pd.date_range('2017-10-29', '2018-03-24').astype(str))

df[df.columns] = np.random.randn(*(df.shape))

fig, ax = plt.subplots(1, figsize=(10,6))

l1 = pd.to_datetime(df.index).month
l2 = pd.to_datetime(df.columns).hour
x = pd.Series(l2).drop_duplicates()
y = pd.Series(l1).drop_duplicates()

ax.imshow(df, cmap='YlOrBr')

ax.set_xticks(x.index)
ax.set_xticklabels(x)

ax.set_yticks(y.index)
ax.set_yticklabels(y)

Output: 输出:

(2)输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM