# Berendsen's code 2.5 for Fig.2.7 import matplotlib.pyplot as plt import numpy as np fname = 'tab2.5.csv' time = [] conc = [] err = [] def readMyFile(fname): dx = np.arange(4,dtype='f') file = open(fname, 'r', encoding='utf-8') a_line = file.readline() l = len(a_line) a_line = a_line[:l-1] result = a_line.split(',') print("Header of the file '" + fname + "': " + a_line) for x in reversed(result): # remove "" if len(x)>0: break else: result.remove(x) n_item = len(result) # print(result, ', ', n_item, ' items') i = 0 while (True): a_line = file.readline() l = len(a_line) a_line = a_line[:l-1] if len(a_line)==0: # end of file break result = a_line.split(',') # print(i+1,') ',result) print('(',i+1,') ', end='') for j in range(n_item): # print(j,n_item) dx[j] = float(result[j]) print(f"{dx[j]:,.2f} ", end="") print() i += 1 time.append(dx[0]) conc.append(dx[1]) err.append(dx[2]) file.close() print('data =',i,', item=',n_item,', file =',fname) #--------- Main program starts here ----------- readMyFile(fname) # vectors are read from file fig, ax = plt.subplots() ax.plot(time,conc,lw=2,ls='-',marker='o',ms=5,mec='r',mew=2,mfc='r') ax.set_yscale('log') pos = [1, 10, 100] # ax.set_yticks(pos) # ticks = ['1', '10', '100'] # ax.set_yticklabels(ticks) # plt.semilogy(time, conc)') plt.errorbar(time, conc, err) plt.grid(axis='y', which='both') plt.grid(axis='x', which='major') font1 = {'family':'serif','color':'blue','size':13} plt.xlabel('time $t$/s',fontdict = font1 ) plt.ylabel('concentration $c$/mmol L$^{-1}$',fontdict = font1) plt.title('Code 2.5 for Fig.2.7') plt.show() # fig.savefig('plot of fig.2.7.png')