1 # Copyright 2020 Jordi Corbilla. All Rights Reserved. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ============================================================================== 15 import os 16 import matplotlib.pyplot as plt 17 18 19 class Plotter: 20 def __init__(self, blocking, project_folder, short_name, currency, stock_ticker): 21 self.blocking = blocking 22 self.project_folder = project_folder 23 self.short_name = short_name 24 self.currency = currency 25 self.stock_ticker = stock_ticker 26 27 def plot_histogram_data_split(self, training_data, test_data, validation_date): 28 print("plotting Data and Histogram") 29 plt.figure(figsize=(12, 5)) 30 plt.plot(training_data.Close, color='green') 31 plt.plot(test_data.Close, color='red') 32 plt.ylabel('Price [' + self.currency + ']') 33 plt.xlabel("Date") 34 plt.legend(["Training Data", "Validation Data >= " + validation_date.strftime("%Y-%m-%d")]) 35 plt.title(self.short_name) 36 plt.savefig(os.path.join(self.project_folder, self.short_name.strip().replace('.', '') + '_price.png')) 37 38 fig, ax = plt.subplots() 39 training_data.hist(ax=ax) 40 fig.savefig(os.path.join(self.project_folder, self.short_name.strip().replace('.', '') + '_hist.png')) 41 42 plt.pause(0.001) 43 plt.show(block=self.blocking) 44 45 def plot_loss(self, history): 46 print("plotting loss") 47 plt.plot(history.history['loss'], label='loss') 48 plt.plot(history.history['val_loss'], label='val_loss') 49 plt.xlabel('Epoch') 50 plt.ylabel('Loss') 51 plt.title('Loss/Validation Loss') 52 plt.legend(loc='upper right') 53 plt.savefig(os.path.join(self.project_folder, 'loss.png')) 54 plt.pause(0.001) 55 plt.show(block=self.blocking) 56 57 def plot_mse(self, history): 58 print("plotting MSE") 59 plt.plot(history.history['MSE'], label='MSE') 60 plt.plot(history.history['val_MSE'], label='val_MSE') 61 plt.xlabel('Epoch') 62 plt.ylabel('MSE') 63 plt.title('MSE/Validation MSE') 64 plt.legend(loc='upper right') 65 plt.savefig(os.path.join(self.project_folder, 'MSE.png')) 66 plt.pause(0.001) 67 plt.show(block=self.blocking) 68 69 def project_plot_predictions(self, price_predicted, test_data): 70 print("plotting predictions") 71 plt.figure(figsize=(14, 5)) 72 plt.plot(price_predicted[self.stock_ticker + '_predicted'], color='red', label='Predicted [' + self.short_name + '] price') 73 plt.plot(test_data.Close, color='green', label='Actual [' + self.short_name + '] price') 74 plt.xlabel('Time') 75 plt.ylabel('Price [' + self.currency + ']') 76 plt.legend() 77 plt.title('Prediction') 78 plt.savefig(os.path.join(self.project_folder, self.short_name.strip().replace('.', '') + '_prediction.png')) 79 plt.pause(0.001) 80 plt.show(block=self.blocking) 81