数据框根据日期索引绘制直线

2024-09-28 05:15:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据框,它在非营业时间内丢弃数据,但在绘制数据框时,因为日期是索引,所以该图显示了前一天最后一次读取和第二天第一次读取之间的长连接线(见图)。我需要避免这种情况,只绘制工作时间。
我使用下面的简单代码

df.plot()
plt.show()

数据帧输出

date                      NIFTY 50  AARTIIND  ...  DIVISLAB  GARFIBRES
                                           ...                     
2021-08-31 12:15:00+05:30  1.000000  1.000000  ...  1.000000   1.000000
2021-08-31 13:15:00+05:30  0.999627  0.996703  ...  1.002769   0.999557
2021-08-31 14:15:00+05:30  1.005706  0.996916  ...  1.005469   0.986966
2021-08-31 15:15:00+05:30  1.005078  0.997607  ...  1.004459   0.994337
2021-09-01 09:15:00+05:30  1.009123  1.003882  ...  1.006013   0.995697
2021-09-01 10:15:00+05:30  1.003989  0.990428  ...  1.005382   0.995413
2021-09-01 11:15:00+05:30  1.003241  0.993566  ...  1.021187   0.997517
2021-09-01 12:15:00+05:30  1.002904  0.986759  ...  1.018506   0.997184

enter image description here


Tags: 数据代码dfdateplotshow时间绘制
2条回答

如果您有这样一个数据框,索引上有日期,有些列有值:

df = pd.DataFrame({'date': pd.date_range(start = '2021-01-01', end = '2021-01-05', freq = 'H')})
df['value 1'] = np.random.random(len(df))
df['value 2'] = np.random.random(len(df))
df = df.set_index('date')
                      value 1   value 2
date                                   
2021-01-01 00:00:00  0.374540  0.427541
2021-01-01 01:00:00  0.950714  0.025419
2021-01-01 02:00:00  0.731994  0.107891
2021-01-01 03:00:00  0.598658  0.031429
2021-01-01 04:00:00  0.156019  0.636410
2021-01-01 05:00:00  0.155995  0.314356
2021-01-01 06:00:00  0.058084  0.508571
2021-01-01 07:00:00  0.866176  0.907566
2021-01-01 08:00:00  0.601115  0.249292
2021-01-01 09:00:00  0.708073  0.410383

您可以定义作业的开始和结束时间,并使用它们过滤数据帧;如果小时超出此界限,则将数据设置为None

start_working_hour = 8
end_working_hour = 17
filt = (df.index.hour < start_working_hour) | (df.index.hour > end_working_hour)
df.loc[filt] = None

完整代码

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


df = pd.DataFrame({'date': pd.date_range(start = '2021-01-01', end = '2021-01-05', freq = 'H')})
df['value 1'] = np.random.random(len(df))
df['value 2'] = np.random.random(len(df))
df = df.set_index('date')


start_working_hour = 8
end_working_hour = 17
filt = (df.index.hour < start_working_hour) | (df.index.hour > end_working_hour)
df.loc[filt] = None

df.plot()

plt.show()

enter image description here


如果你想从this answer中汲取灵感,消除线之间的白色间隙,从而获得连续的线,你应该使用range(df.index.size)作为x轴进行绘图,然后你需要调整x刻度

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import time


df = pd.DataFrame({'date': pd.date_range(start = '2021-01-01', end = '2021-01-05', freq = 'H')})
df['value 1'] = np.random.random(len(df))
df['value 2'] = np.random.random(len(df))
df = df.set_index('date')


start_working_hour = 8
end_working_hour = 16
hour_step = 2
filt = (start_working_hour <= df.index.hour) & (df.index.hour <= end_working_hour)
df = df.loc[filt]

fig, ax = plt.subplots(figsize = (15, 5))

ax.plot(range(df.index.size), df['value 1'], label = 'value 1')
ax.plot(range(df.index.size), df['value 2'], label = 'value 2')

ax.grid(axis='x', alpha=0.3)

ticks_date = df.index.indexer_at_time(time(start_working_hour).strftime('%H:%M'))
ticks_time = np.arange(df.index.size)[df.index.minute == 0][::hour_step]
ax.set_xticks(ticks_date)
ax.set_xticks(ticks_time, minor=True)

labels_date = [maj_tick.strftime('\n%d-%b').replace('\n0', '\n') for maj_tick in df.index[ticks_date]]
labels_time = [min_tick.strftime('%H:%M') for min_tick in df.index[ticks_time]]
ax.set_xticklabels(labels_date)
ax.set_xticklabels(labels_time, minor=True)
ax.figure.autofmt_xdate(rotation=0, ha='center', which='both')

ax.legend(frameon = True)

plt.show()

enter image description here

我找到了解决这个问题的办法。我已经更改了日期索引的格式,如图所示。这有助于我准确地得到图中数据框中的内容。谢谢你的帮助,如果有其他更好的方法,请建议

我修改过的代码

df.index = df.index.strftime('%y-%m-%d %H:%M')
print(df)
df.plot()
plt.show()

Updated plot

相关问题 更多 >

    热门问题