[英]plotting a timeseries graph in python using matplotlib from a csv file
[英]label a point in graph using matplotlib for timeseries
我有3列的pandas數據框。 我在Y軸上繪制col1,在X軸上繪制time_stamps系列。 對於此系列,每當col2為-1時,我都希望在圖形上突出顯示該點為異常。 我嘗試使用ax.text獲取坐標並突出顯示,但由於X軸是時間序列,因此無法獲取正確的坐標。 在下面的示例中,由於col2 [2] ==-1,我試圖繪制第三行坐標。
import pandas
import matplotlib.pyplot as plt
df=df[["time_stamps","col1"]]
df.set_index("time_stamps",inplace=True)
ax=df.plot()
ticklabels = [l.get_text() for l in ax.xaxis.get_ticklabels()]
new_labels=[tick[-6:] for tick in ticklabels]
ax.xaxis.set_ticklabels(new_labels)
x1="16965 days 17:52:03"
y1=0.7
ax.text(x1, y1, "anaomly", fontsize=15)
plt.show()
樣本數據看起來像
time_stamp=[16965 days 17:52:00,16965 days 17:52:02
16965 days 17:52:03,16965 days 17:52:05
16965 days 17:52:06,16965 days 17:52:08
16965 days 17:52:09,16965 days 17:52:11
16965 days 17:52:12,16965 days 17:52:14]
col1=[0.02,0.01,0.7,0.019,0.019,0.017,0.023,0.04,0.072,0.05]
col2=[1,1,-1,1,1,1,1,1,1,1]
我發現可以將其轉換為秒,然后將這些點標記為異常。 這就是我所做的。
def changetotimedelta(row):
return pd.to_timedelta(row["time_stamps"])/ np.timedelta64(1,'D')
def main()
df=pd.read_csv(inputFile)
df["time"]=df.apply(changetotimedelta,axis=1)
new_df=df[["time","col1"]]
new_df.set_index("time",inplace=True)
ax=new_df.plot()
x1=pd.to_timedelta("16965 days 17:52:03")/ np.timedelta64(1,'D')
y1=0.7
ax.annotate('anomaly', xy=(x1, y1), xytext=(x2, 1),
arrowprops=dict(facecolor='red', shrink=0.01),)
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.