Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def microcar(insts,acts):
- import numpy as np
- #defining final return lists
- x_exp_disp=[]
- y_exp_disp=[]
- x_act_disp=[]
- y_act_disp=[]
- exp_dist=[]
- act_dist=[]
- #determining expecting values for microcar
- for inst in insts:
- #variables declared at the start of the loop so when the next file is opened it will reset
- x_exp_disp=0
- y_exp_disp=0
- exp_dist=0
- imput=np.loadtxt(inst,delimiter=',',dtype=[('dir','U1'),('time',int),('speed',int)])
- for row in input:
- #N is positive virtical and E is positive horizontal with respect to displacement
- #check which direction the car is travelling
- if row[0]=='N':
- #calculating the new displacement from the origin and the distance travelled
- y_exp_disp+=round(float(row[1])*float(row[2]),10)
- exp_dist+=round(float(row[1])*float(row[2]),10)
- elif row[0]=='S':
- y_exp_dist-=round(float(row[1])*float(row[2]),10)
- exp_dist+=round(float(row[1])*float(row[2]),10)
- elif row[0]=='E':
- x_exp_dist+=round(float(row[1])*float(row[2]),10)
- exp_dist+=round(float(row[1])*float(row[2]),10)
- elif row[0]=='W':
- x_exp_dist-=round(float(row[1])*float(row[2]),10)
- exp_dist+=round(float(row[1])*float(row[2]),10)
- x_exp_disp_list.append(round(x_exp_disp,2))
- y_exp_disp_list.append(round(y_exp_disp,2))
- exp_dist_list.append(round(exp_dist,2))
- #determining the actual value of the micro cars
- for act in acts:
- x_exp_disp=0
- y_exp_disp=0
- exp_dist=0
- input=np.loadtxt(act,delimiter=',',dtype=[('dir','U1'),('time',int),('speed',int)])
- if row[0]=='N':
- #calculating the new actual displacement from the origin and the distance travelled
- y_act_disp+=round(float(row[1])*float(row[2]),10)
- act_dist+=round(float(row[1])*float(row[2]),10)
- elif row[0]=='S':
- y_act_dist-=round(float(row[1])*float(row[2]),10)
- act_dist+=round(float(row[1])*float(row[2]),10)
- elif row[0]=='E':
- x_act_dist+=round(float(row[1])*float(row[2]),10)
- act_dist+=round(float(row[1])*float(row[2]),10)
- elif row[0]=='W':
- x_act_dist-=round(float(row[1])*float(row[2]),10)
- act_dist+=round(float(row[1])*float(row[2]),10)
- x_act_disp_list.append(round(x_exp_disp,2))
- y_act_disp_list.append(round(y_exp_disp,2))
- act_dist_list.append(round(exp_dist,2))
- #convert lists to arrays
- exp_dist_list=np.array(exp_dist_list)
- act_dist_list=np.array(act_dist_list)
- x_exp_disp_list=np.array(x_exp_dist_list)
- y_exp_disp_list=np.array(y_exp_dist_list)
- x_act_disp_list=np.array(x_act_disp_list)
- y_act_disp_list=np.array(y_act_disp_list)
- return x_exp_disp_list, y_exp_disp_list, x_act_disp_list, y_act_disp_list, exp_dist_list, act_dist_list
- #Part two
- def plotmicrocar(insts,acts):
- #import required modules
- import numpy as np
- import matplotlib.pyplot as plot
- #retrieve calculated vales from the microcar function
- #expected values
- x_exp_disp_list=microcar(insts,acts)[0]
- y_exp_disp_list=microcar(insts,acts)[1]
- exp_dist_list=microcar(insts,acts)[4]
- #actual values
- x_act_disp_list=microcar(insts,acts)[2]
- y_act_disp_list=microcar(insts,acts)[3]
- act_dist_list=microcar(insts,acts)[5]
- #plotting the values
- plt.figure(1)
- #Graphing the bar graph
- #x-axis corresponds to the number of cars the function is considering
- x=np.arange(len(exp_dist_list))+1
- #added 1 to array because the car number starts at 0
- #the length of the list is the number of cars
- #place the bar graph at the top of the graphic
- plt.subplot(211)
- #graph of expected values
- plt.bar(x-0.125,exp_dist_list,0.125,0,align='edge',color='blue')
- #subtracted 0.125 both the expected and actual bars are plotted side by side
- #actual values
- plt.bar(x,act_dist_list,0.125,0,align='edge',color='red')
- #adding graph labels
- plt.xlabel('Microcar (number)', fontsize=9)
- plt.ylabel('Distance (m)', fontsize=9)
- plt.legend(['Expected','Actual'],loc='upper left',fontsize=8)
- plt.xticks(x) #whole number increases per mark on the x-axis
- plt.title('Microcar Distances Travelled',fontsize=10)
- #plotting displacements on a scatter graph
- #find the largest displacement to get the graph dimensions of both axis
- max_disp=np.amax([x_exp_disp_list,y_exp_disp_list,x_act_disp_list,y_act_disp_list])
- axis_lim=[-max_disp-0.25*max_disp,max_disp+0.25*max_disp]
- #the bottom left graph is the expected displacements
- plt.subplot(223)
- label=[] #which dot is which car
- n=0 #start lists at 0
- #counting the number of cars and then plotting a point for each car
- for value in x_exp_disp_list:
- plt.scatter(x_exp_disp_list[n],y_exp_disp_list[n],marker='x')
- label.append('Car '+str(n+1)) #n+1 because cars start at 1 not 0
- n+=1
- #making the scatter plot square
- plt.ylim(axis_lim)
- plt.xlim(axis_lim)
- #graph
- plt.xlabel('Horizontal Disp (m)',fontsize=9)
- plt.ylabel('Vertical Disp (m)',fontsize=9)
- plt.legend(label,fontsize=8)
- plt.title('Expected Microcar Displacement',fontsize=10)
- #actual displacements
- plt.subplot(224)
- n=0
- for value in x_act_disp_list:
- plt.scatter(x_act_disp_list[n],y_act_disp_list[n],marker='x')
- n+=1
- plt.ylim(axis_lim)
- plt.xlim(axis_lim)
- #graph
- plt.xlabel('Horizontal Disp (m)',fontsize=9)
- plt.ylabel('Vertical Disp (m)',fontsize=9)
- plt.legend(label,loc='upper left',fontsize=8)
- plt.title('Actual Microcar Displacement',fontsize=10)
- plt.tight_layout() #stops the graphs from overlapping
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement