好吧,我一直在研究这个温度时间序列数据,我想知道是否有办法控制 x 轴上的日期。 Matplotlib 随机选择日期作为 x 轴。我的意思是我想显示异常下方的确切日期,然后将其命名为 EQ,同时将其他值设置为 EQ 之前和之后的天数。
例如,x 轴可能变为 [-25, -20, -15, -10, -5, "EQ", 5, 10]
这是我当前正在使用的数据:
这就是我想要实现的目标。
这是我迄今为止编写的代码!
fig, ax = plt.subplots(figsize=(8,4))
sns.scatterplot(data =at_day, x=at_day.index, y="AT_Day_Time_Values", hue=at_day["Legend"], palette="bright", s = 80);
sns.lineplot(at_day["AT_Day_Time_Values"], linewidth=1.5, linestyle = "--", color = "black", ax = ax, label="AT (Day)")
sns.rugplot(data =at_day, x=at_day.index, y="AT_Day_Time_Values", hue=at_day["Legend"], ax= ax)
ax.set_xticks(at_day.index.date)
ax.xaxis.set_major_locator(mdates.DayLocator(interval=5))
ax.set(ylim=[295, 307], ylabel="K");
ax.grid(axis="y")
请暂时忽略其他诸如色调之类的东西,我只对学习控制 xaxis 上的日期感兴趣
您可以使用这种方法自动生成报价。我假设您总是想要 5 天的差异,所以如果您愿意,您也可以稍后调整:
import pandas as pd
import numpy as np
%matplotlib notebook
import matplotlib.pyplot as plt
df = pd.read_csv("air_temp.csv")
df["Dates"] = pd.to_datetime(df["Dates"])
# get limits for prompt
minDate = df["Dates"].min()
maxDate = df["Dates"].max()
EQ = "" # init
while EQ not in df["Dates"].values: # check value, do until...
# prompt user to input dates between two values, liek t his yyyy-mm-dd
EQ = input(f"Choose a date between {minDate.date()} and {maxDate.date()}: ")
EQ = pd.to_datetime(EQ) # convert to date
df['DatesDiff'] = df['Dates'] - EQ # get time difference
plt.figure() # generate a figure
plt.plot(df['DatesDiff'].dt.days, df['AT_Day_Time_Values']) # plot
minVal = np.floor(min(df["DatesDiff"].dt.days) / 5) * 5 # get suitable limit in negative
maxVal = np.ceil(max(df["DatesDiff"].dt.days) / 5) * 5 # get suitable limit in positive
ticks = np.arange(minVal, maxVal + 1, 5) # define the ticks you want to set, they will include 0 and have 5 spacing
labels = [str(int(tick)) if tick != 0 else 'EQ' for tick in ticks] # generate labels as string, with "EQ" at 0
plt.xticks(ticks, labels) # set the ticks
plt.title("EQ = "+str(EQ.date())) # set title for OP
这是用EQ:2022-08-05
我猜你想自动识别峰值,所以这里是做到这一点的方法:
df = pd.read_csv("air_temp.csv")
df["Dates"] = pd.to_datetime(df["Dates"])
# get limits for prompt
minDate = df["Dates"].min()
maxDate = df["Dates"].max()
EQ = df["Dates"].loc[df["AT_Day_Time_Values"].idxmax()] # get maximum temp
df["DatesDiff"] = df["Dates"] - EQ # get time difference
plt.figure() # generate a figure
plt.plot(df["DatesDiff"].dt.days, df["AT_Day_Time_Values"]) # plot
minVal = np.floor(min(df["DatesDiff"].dt.days) / 5) * 5 # get suitable limit in negative
maxVal = np.ceil(max(df["DatesDiff"].dt.days) / 5) * 5 # get suitable limit in positive
ticks = np.arange(minVal, maxVal + 1, 5) # define the ticks you want to set, they will include 0
labels = [str(int(tick)) if tick != 0 else 'EQ' for tick in ticks] # generate labels as string, with "EQ" at 0
plt.xticks(ticks, labels) # set the ticks
plt.title("EQ = "+str(EQ.date())) # set title for OP
我想因为这次是最大的,所以这很容易。当数据变得更加嘈杂时,检测异常会变得更加复杂。结果如下: