# -*- coding: utf-8 -*- """ Created on Sat Mar 7 10:41:51 2020 @author: Johan Rene van Dorp """ import pandas as pd import numpy as np import matplotlib.pyplot as plt import Dist_Library as dl df = pd.read_csv('AustralianTimber.csv') Density = df["Density"] Hardness = df["Hardness"] n_d = len(Density) # Plotting a scatter plot and a regression lines plt.rc('font', family='serif', size='10') plt.figure(figsize=(10, 5)) Scatter_Figure = plt.figure() Scatter_Figure.set_figwidth(9) Scatter_Figure.set_figheight(4.5) off_set_x = 10 off_set_y = 10 LB_d = 20 UB_d = 80 LB_h = 250 UB_h = 3500 # Plotting the scatter plot x_lims = (LB_d-off_set_x,UB_d+off_set_x) y_lims = (LB_h-off_set_y,UB_h+off_set_y) Panel = Scatter_Figure.add_subplot(1,1,1) plt.scatter(Density,Hardness,color ='indianred', s = 10) plt.xlim(x_lims) plt.ylim(y_lims) plt.xlabel('Wood Density') plt.ylabel('Wood Hardness') plt.axvline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) plt.axhline(0, lw=2, ls = '-',color = 'lightgray',alpha=0.5) a, b = np.polyfit(Density, Hardness, 1) f_linear = np.poly1d((a, b)) plt.plot((LB_d,UB_d), f_linear((LB_d,UB_d)), '-', lw=1, color = 'royalblue') forecast_d = 65 forecast_h = f_linear(forecast_d) plt.vlines(forecast_d, 0, forecast_h, color='black', linestyles='--', lw=1) plt.hlines(forecast_h, 0, forecast_d, color='black', linestyles='--', lw=1) text_str = str(forecast_d) plt.text(forecast_d+1,40,text_str,color = 'red',size = 10) text_str = f'{forecast_h:5.2f}' plt.text(15,forecast_h+50,text_str,color = 'red',size = 10) R_Squared = 100*dl.rsquared(Density, Hardness) abs_b = abs(b) text_str = '$y = $' + f'{a:4.2f}' + '$ \cdot x - $' + f'{abs_b:4.1f}' + \ ', $R^2 =$' + f'{R_Squared:4.1f}' +'%' plt.text(15,3000,text_str,color = 'royalblue',size = 10) text_str = '$n = $' + str(n_d) plt.text(15,3250,text_str,color = 'red',size = 10) Scatter_Figure.suptitle('Australian Timber Data Example',size='14') plt.savefig('AustralianTimber_Scatter.png', dpi=1200)