我有一个包含date_time、date、time和VALUE1列的数据集,该列显示每个时间点的测量值。对于同一个ID,一天内有多个测量值。此外,对于一个ID,有6个不同的24小时测量值,显示在INSPECTION列中。
import random
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.ticker as ticker
random.seed(0)
df = pd.DataFrame({'DATE_TIME': pd.date_range('2022-11-01', '2022-11-06 23:00:00', freq='20min'),
'ID': [random.randrange(1, 3) for n in range(430)]})
df['VALUE1'] = [random.uniform(110, 160) for n in range(430)]
df['VALUE2'] = [random.uniform(50, 80) for n in range(430)]
df['INSPECTION'] = df['DATE_TIME'].dt.day
# df['INSPECTION'] = df['INSPECTION'].replace(6, 1)
# df['INSPECTION'] = df['INSPECTION'].replace(3, 1)
df['MODE'] = np.select([df['INSPECTION'] == 1, df['INSPECTION'].isin([2, 3])], ['A', 'B'], 'C')
df['TIME'] = df['DATE_TIME'].dt.time
df['TIME'] = df['TIME'].astype('str')
df['TIMEINTERVAL'] = df.DATE_TIME.diff().astype('timedelta64[m]')
df['TIMEINTERVAL'] = df['TIMEINTERVAL'].fillna(0)
def to_day_period(s):
bins = ['0', '06:00:00', '13:00:00', '18:00:00', '23:00:00', '24:00:00']
labels = ['Nighttime', 'Daytime', 'Daytime', 'Nighttime', 'Nighttime']
return pd.cut(
pd.to_timedelta(s),
bins=list(map(pd.Timedelta, bins)),
labels=labels, right=False, ordered=False
)
df['TIME_OF_DAY'] = to_day_period(df['TIME'])
df_monthly = df
# ++++++++++++++++++++++++++++++++ sns plot ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
df_id = df[df.ID==1]
sns.set_style('darkgrid')
sns.set(rc={'figure.figsize':(14,8)})
#print(df_id.INSPECTION.unique())
ax = sns.lineplot(data=df_id, x ='TIME', y = 'VALUE1',
hue='INSPECTION', palette='viridis',
legend='full', lw=3)
ax.xaxis.set_major_locator(ticker.MultipleLocator(10))
plt.legend(bbox_to_anchor=(1, 1))
plt.ylabel('VALUE1')
plt.xlabel('TIME')
plt.show()
如何在x轴上显示一天24小时的周期而不重复时间?为了清晰地表达,x轴从00:40:00开始,然后它再次显示00:00:00。是否也有方法处理这个问题?我想在x轴上只显示从00:00:00到23:59:00的时间,而不重复时间。
1条答案
按热度按时间slhcrj9b1#
'INSPECTION'
的每个点都正确定位。.total_seconds()
方法。df.DATE_TIME.apply(lambda row: (row - row.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds())
ax.xaxis.set_major_locator(tkr.MultipleLocator(3600))
['']
是第二天'00:00'
的最后一个刻度。hours = [dtime(i).strftime('%H:%M') for i in range(24)] + ['']
fig, (ax1, ax2) = plt.subplots(2, 1)
来完成,但这只是表面上的改变,与问题无关。sns.move_legend
移动,而不是plt.legend
。ax
(matplotlib.axes.Axes
的别名)的面向对象接口比交替使用ax
和plt
更一致。*在
python 3.11.2
、pandas 2.0.0
、matplotlib 3.7.1
、seaborn 0.12.2
中测试df.head()