File size: 3,558 Bytes
7ff2d8b
 
 
 
 
d91dd02
 
7ff2d8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d91dd02
7ff2d8b
 
 
d91dd02
 
 
7ff2d8b
 
 
8735b50
7ff2d8b
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from datetime import datetime
import matplotlib.pyplot as plt


# function for plot prediction and rolling window(previous price)
def combine_plot_function(dataframe_instance, predictions_instance, buffer):
    try:
        plt.figure(figsize=(13, 4))

        # Scatter plot for the existing DataFrame
        plt.plot(dataframe_instance['Date'],
                 dataframe_instance['Price'],
                 marker='o',
                 label='Previous Rolling Window Prices')

        # Adding a vertical dashed line at the end of the existing DataFrame
        plt.axvline(x=dataframe_instance['Date'].max(),
                    color='black',
                    linestyle='--',
                    label=None)

        # Scatter plots for the prediction DataFrame after the dashed line
        plt.plot(predictions_instance['Date'],
                 predictions_instance['Lower_Bound'],
                 marker='o',
                 label='Predicted Lower Bound',
                 color='red')

        plt.plot(predictions_instance['Date'],
                 predictions_instance['Upper_Bound'],
                 marker='o',
                 label='Predicted Upper Bound',
                 color='green')

        plt.plot(predictions_instance['Date'],
                 predictions_instance['Mean_Price'],
                 marker='o',
                 label='Mean Price',
                 color='blue')

        # Shading the region between Lower Bound and Mean Price in red
        plt.fill_between(predictions_instance['Date'],
                         predictions_instance['Lower_Bound'],
                         predictions_instance['Mean_Price'],
                         color='red', alpha=0.3)

        # Shading the region between Mean Price and Upper Bound in green
        plt.fill_between(predictions_instance['Date'],
                         predictions_instance['Mean_Price'],
                         predictions_instance['Upper_Bound'],
                         color='green', alpha=0.3)

        # Connecting the last point of df_existing to the first point of df_new
        plt.plot([dataframe_instance['Date'].max(), predictions_instance['Date'].min()],
                 [dataframe_instance['Price'].iloc[-1], predictions_instance['Lower_Bound'].iloc[0]],
                 linestyle='--', color='red')

        plt.plot([dataframe_instance['Date'].max(), predictions_instance['Date'].min()],
                 [dataframe_instance['Price'].iloc[-1], predictions_instance['Upper_Bound'].iloc[0]],
                 linestyle='--', color='green')  # Separate line for Upper Bound

        plt.plot([dataframe_instance['Date'].max(), predictions_instance['Date'].min()],
                 [dataframe_instance['Price'].iloc[-1], predictions_instance['Mean_Price'].iloc[0]],
                 linestyle='--', color='blue')  # Separate line for Mean Price

        # Setting grid
        plt.grid(True)
        plt.legend()
        plt.xticks(rotation=45)

        # storing in buffer rather saving it
        plt.savefig(buffer, format='png', bbox_inches='tight')
        plt.close()

    except Exception as e:
        raise(f"Error in combine_plot_function: {e}")


# class function 3: For date and time
class DateTimeFunctionClass:
    def __init__(self):
        self.curr_date_time = datetime.now()
        self.curr_date = self.curr_date_time.date()
        self.curr_time = self.curr_date_time.strftime("%H_%M_%S")

    def current_date(self):
        return self.curr_date

    def current_time(self):
        return self.curr_time