# histogram, code2.2.py # import plotsvg <-- not accessible import matplotlib import matplotlib.pyplot as plt import numpy import numpy as np def linesFile(fname): count_line = 0 with open(fname, 'r', encoding='utf-8') as file: a_line = file.readline() while a_line: count_line += 1 a_line = file.readline() # count_line += 1 return count_line def RdFile1(fname, x): with open(fname, 'r', encoding='utf-8') as file: a_line = file.readline() count_line = 0 if not a_line.isnumeric(): print(a_line,' is not a number') a_line.encode('utf-8') x[count_line] = float(a_line) print("1st data: x[",count_line+1,"]= ",f"{x[count_line]:,.02f}") while a_line: a_line = file.readline() if len(a_line)>0: if not a_line.isnumeric(): # print(a_line,' is not a number') a_line.encode('utf-8') count_line += 1 x[count_line] = float(a_line) # print("x[",count_line+1,"]= ",f"{x[count_line]:,.02f}") count_line += 1 print("last data: x[",count_line,"]= ",f"{x[count_line-1]:,.02f}") # print(count_line,' lines have been read') if file.closed: print(fname + ' is closed') #--- Main program starts here. --- print('--- Read a file and make a cumulative plot ---') print("MatPlotLib Version." + str(matplotlib.__version__)) print("NumPy Version." + str(numpy.__version__)) fname = 'tab2.1.csv' ndata = linesFile(fname) xmax = 12 print(ndata, ' lines in ' + fname) x = np.arange(ndata, dtype = 'f') RdFile1(fname, x) bins = np.array([6.,7.,8.,9.,10.,11.,12.]) plt.grid() plt.hist(x, bins, histtype='bar', rwidth=0.8, color='green') font1 = {'family':'serif','color':'blue','size':11} plt.title("Figure 2.2 Histogram", fontdict = font1) font2 = {'family':'serif','color':'darkred','size':12} plt.title("Figure 2.3 Histogram", fontdict = font1) plt.xlabel("Data value", fontdict = font2) plt.ylabel("Number of observation", fontdict = font2) plt.show()