微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在我的代码中为线图实现标签?

如何解决如何在我的代码中为线图实现标签?

正如标题所述,我想在图线中添加标签,以显示最大值和最小值。

如您在图片上所见,如果可能的话,我只想在图形的橙色部分的最大值和最小值上实现标签,它表示能耗的预测(我需要这样做,以便我可以更好将其与实时数据进行比较)。 [1]:https://i.stack.imgur.com/YYAy2.png

这是我在Python程序中使用的代码行,用于进行预测和绘图。 请记住,此代码不是我的代码,我严格将其用于预测,这是我正在做的项目的一部分。我从这里得到了代码:[1]:https://towardsdatascience.com/energy-consumption-time-series-forecasting-with-python-and-lstm-deep-learning-model-7952e2f9a796

# Ploting packages
import matplotlib.pyplot as plt
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

# Date wrangling
from datetime import datetime,timedelta

# Data wrangling
import pandas as pd 

# The deep learning class
from deep_model import DeepModelTS

# Reading the configuration file
import yaml

# Directory managment 
import os

# Reading the hyper parameters for the pipeline
with open(f'{os.getcwd()}\\conf1.txt') as file:
    conf = yaml.load(file,Loader=yaml.FullLoader)

# Reading the data 
d = pd.read_csv('input/mepsofinalno.csv')
d['Datetime'] = [datetime.strptime(x,'%Y-%m-%d %H:%M:%s') for x in d['Datetime']]

# Making sure there are no duplicated data
# If there are some duplicates we average the data during those duplicated days
d = d.groupby('Datetime',as_index=False)['MEPSO_MW'].mean()

# Sorting the values
d.sort_values('Datetime',inplace=True)

# Initiating the class 
deep_learner = DeepModelTS(
    data=d,Y_var='MEPSO_MW',lag=conf.get('lag'),LSTM_layer_depth=conf.get('LSTM_layer_depth'),epochs=conf.get('epochs'),train_test_split=conf.get('train_test_split') # The share of data that will be used for validation
)

# Fitting the model 
model = deep_learner.LSTModel()

# Making the prediction on the validation set
# Only applicable if train_test_split in the conf.yml > 0
yhat = deep_learner.predict()

if len(yhat) > 0:

    # Constructing the forecast dataframe
    fc = d.tail(len(yhat)).copy()
    fc.reset_index(inplace=True)
    fc['forecast'] = yhat

    # Ploting the forecasts
    plt.figure(figsize=(12,8))
    for dtype in ['MEPSO_MW','forecast']:
        plt.plot(
            'Datetime',dtype,data=fc,label=dtype,alpha=0.8
        )
    plt.legend()
    plt.grid()
    plt.show()   
    
# Forecasting n steps ahead   

# Creating the model using full data and forecasting n steps ahead
deep_learner = DeepModelTS(
    data=d,lag=24,LSTM_layer_depth=64,epochs=2500,train_test_split=0
)

# Fitting the model 
deep_learner.LSTModel()

# Forecasting n steps ahead
n_ahead = 48
yhat = deep_learner.predict_n_ahead(n_ahead)
yhat = [y[0][0] for y in yhat]

# Constructing the forecast dataframe
fc = d.tail(400).copy() 
fc['type'] = 'original'

last_date = max(fc['Datetime'])
hat_frame = pd.DataFrame({
    'Datetime': [last_date + timedelta(hours=x + 1) for x in range(n_ahead)],'MEPSO_MW': yhat,'type': 'forecast'
})

fc = fc.append(hat_frame)
fc.reset_index(inplace=True,drop=True)

# Ploting the forecasts
plt.figure(figsize=(12,8))
for col_type in ['original','forecast']:
    plt.plot(
        'Datetime','MEPSO_MW',data=fc[fc['type']==col_type],label=col_type
        )

plt.legend()
plt.grid()
plt.show()

我通过几次搜索就在Google上找到了什么:https://queirozf.com/entries/add-labels-and-text-to-matplotlib-plots-annotation-examples 但是我不知道如何实现它,因为我使用的代码行比那里显示的示例复杂得多。

任何帮助将不胜感激。谢谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。