#! /usr/bin/env python3 # def drug_dosage_plots ( ): #*****************************************************************************80 # ## least_squares_plots() compares data, a formula, and a least squares approximation. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 03 October 2026 # # Author: # # John Burkardt # import matplotlib import numpy as np import platform print ( '' ) print ( 'least_squares_plots():' ) print ( ' numpy version: ' + np.version.version ) print ( ' python version: ' + platform.python_version ( ) ) print ( ' On a single picture, plot:' ) print ( ' * 15 data points (X,Y).\n' ); print ( ' * the least squares line Y=A*X+B.\n' ); print ( ' * the exact formula Y=1/2*X^2 + 1.\n' ); # # Set filenames. # header = 'least_squares' command_filename = header + '_commands.txt' data1_filename = header + '1_data.txt' data2_filename = header + '2_data.txt' data3_filename = header + '3_data.txt' graphic_filename = header + '_plots.png' # # Create commands. # command_unit = open ( command_filename, 'wt' ) command_unit.write ( '# ' + command_filename + '\n' ) command_unit.write ( '#\n' ) command_unit.write ( '# Usage:\n' ) command_unit.write ( '# gnuplot < ' + command_filename + '\n' ) command_unit.write ( '#\n' ) command_unit.write ( 'set term png\n' ) command_unit.write ( 'set output "' + graphic_filename + '"\n' ) command_unit.write ( 'set xlabel "<--- X --->"\n' ) command_unit.write ( 'set ylabel "<--- Y --->"\n' ) command_unit.write ( 'set title "Formula (green), Data (blue), least squares fit (red)"\n' ) command_unit.write ( 'set grid\n' ) command_unit.write ( 'set timestamp\n' ) command_unit.write ( 'set key on\n' ) command_unit.write ( 'set style data lines\n' ) command_unit.write ( 'plot "' + data1_filename \ + '" using 1:2 with points lw 3 lt rgb "blue" title "Data", \\\n' ) command_unit.write ( ' "' + data2_filename \ + '" using 1:2 with lines lw 3 lt rgb "red" title "Least Squares Line", \\\n' ) command_unit.write ( ' "' + data3_filename \ + '" using 1:2 with lines lw 3 lt rgb "green" title "Exact Formula"\n' ) command_unit.write ( 'quit\n' ) command_unit.close ( ) print ( ' Created command file "' + command_filename + '"' ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 August 2019 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == '__main__' ): timestamp ( ) drug_dosage_plots ( ) timestamp ( )