• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest May 24th, 2019 111 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. def microcar(insts,acts):
2.     import numpy as np
3.
4.     #defining final return lists
5.     x_exp_disp=[]
6.     y_exp_disp=[]
7.     x_act_disp=[]
8.     y_act_disp=[]
9.     exp_dist=[]
10.     act_dist=[]
11.
12.     #determining expecting values for microcar
13.     for inst in insts:
14.
15.     #variables declared at the start of the loop so when the next file is opened it will reset
16.         x_exp_disp=0
17.         y_exp_disp=0
18.         exp_dist=0
19.
21.
22.         for row in input:
23.             #N is positive virtical and E is positive horizontal with respect to displacement
24.             #check which direction the car is travelling
25.             if row[0]=='N':
26.                 #calculating the new displacement from the origin and the distance travelled
27.                 y_exp_disp+=round(float(row[1])*float(row[2]),10)
28.                 exp_dist+=round(float(row[1])*float(row[2]),10)
29.
30.             elif row[0]=='S':
31.                 y_exp_dist-=round(float(row[1])*float(row[2]),10)
32.                 exp_dist+=round(float(row[1])*float(row[2]),10)
33.
34.             elif row[0]=='E':
35.                 x_exp_dist+=round(float(row[1])*float(row[2]),10)
36.                 exp_dist+=round(float(row[1])*float(row[2]),10)
37.
38.             elif row[0]=='W':
39.                 x_exp_dist-=round(float(row[1])*float(row[2]),10)
40.                 exp_dist+=round(float(row[1])*float(row[2]),10)
41.
42.         x_exp_disp_list.append(round(x_exp_disp,2))
43.         y_exp_disp_list.append(round(y_exp_disp,2))
44.         exp_dist_list.append(round(exp_dist,2))
45.
46.     #determining the actual value of the micro cars
47.     for act in acts:
48.
49.         x_exp_disp=0
50.         y_exp_disp=0
51.         exp_dist=0
52.
54.
55.        if row[0]=='N':
56.                 #calculating the new actual displacement from the origin and the distance travelled
57.                 y_act_disp+=round(float(row[1])*float(row[2]),10)
58.                 act_dist+=round(float(row[1])*float(row[2]),10)
59.
60.             elif row[0]=='S':
61.                 y_act_dist-=round(float(row[1])*float(row[2]),10)
62.                 act_dist+=round(float(row[1])*float(row[2]),10)
63.
64.             elif row[0]=='E':
65.                 x_act_dist+=round(float(row[1])*float(row[2]),10)
66.                 act_dist+=round(float(row[1])*float(row[2]),10)
67.
68.             elif row[0]=='W':
69.                 x_act_dist-=round(float(row[1])*float(row[2]),10)
70.                 act_dist+=round(float(row[1])*float(row[2]),10)
71.
72.         x_act_disp_list.append(round(x_exp_disp,2))
73.         y_act_disp_list.append(round(y_exp_disp,2))
74.         act_dist_list.append(round(exp_dist,2))
75.
76.     #convert lists to arrays
77.     exp_dist_list=np.array(exp_dist_list)
78.     act_dist_list=np.array(act_dist_list)
79.     x_exp_disp_list=np.array(x_exp_dist_list)
80.     y_exp_disp_list=np.array(y_exp_dist_list)
81.     x_act_disp_list=np.array(x_act_disp_list)
82.     y_act_disp_list=np.array(y_act_disp_list)
83.
84.     return x_exp_disp_list, y_exp_disp_list, x_act_disp_list, y_act_disp_list, exp_dist_list, act_dist_list
85.
86. #Part two
87. def plotmicrocar(insts,acts):
88.
89.     #import required modules
90.     import numpy as np
91.     import matplotlib.pyplot as plot
92.
93.     #retrieve calculated vales from the microcar function
94.
95.     #expected values
96.     x_exp_disp_list=microcar(insts,acts)[0]
97.     y_exp_disp_list=microcar(insts,acts)[1]
98.     exp_dist_list=microcar(insts,acts)[4]
99.     #actual values
100.     x_act_disp_list=microcar(insts,acts)[2]
101.     y_act_disp_list=microcar(insts,acts)[3]
102.     act_dist_list=microcar(insts,acts)[5]
103.
104.     #plotting the values
105.     plt.figure(1)
106.
107.     #Graphing the bar graph
108.     #x-axis corresponds to the number of cars the function is considering
109.     x=np.arange(len(exp_dist_list))+1
110.     #added 1 to array because the car number starts at 0
111.     #the length of the list is the number of cars
112.
113.     #place the bar graph at the top of the graphic
114.     plt.subplot(211)
115.     #graph of expected values
116.     plt.bar(x-0.125,exp_dist_list,0.125,0,align='edge',color='blue')
117.     #subtracted 0.125 both the expected and actual bars are plotted side by side
118.
119.     #actual values
120.     plt.bar(x,act_dist_list,0.125,0,align='edge',color='red')
121.
123.     plt.xlabel('Microcar (number)', fontsize=9)
124.     plt.ylabel('Distance (m)', fontsize=9)
125.     plt.legend(['Expected','Actual'],loc='upper left',fontsize=8)
126.     plt.xticks(x) #whole number increases per mark on the x-axis
127.     plt.title('Microcar Distances Travelled',fontsize=10)
128.
129.     #plotting displacements on a scatter graph
130.     #find the largest displacement to get the graph dimensions of both axis
131.     max_disp=np.amax([x_exp_disp_list,y_exp_disp_list,x_act_disp_list,y_act_disp_list])
132.     axis_lim=[-max_disp-0.25*max_disp,max_disp+0.25*max_disp]
133.
134.     #the bottom left graph is the expected displacements
135.     plt.subplot(223)
136.     label=[] #which dot is which car
137.     n=0 #start lists at 0
138.
139.     #counting the number of cars and then plotting a point for each car
140.     for value in x_exp_disp_list:
141.         plt.scatter(x_exp_disp_list[n],y_exp_disp_list[n],marker='x')
142.         label.append('Car '+str(n+1)) #n+1 because cars start at 1 not 0
143.         n+=1
144.
145.     #making the scatter plot square
146.     plt.ylim(axis_lim)
147.     plt.xlim(axis_lim)
148.
149.     #graph
150.     plt.xlabel('Horizontal Disp (m)',fontsize=9)
151.     plt.ylabel('Vertical Disp (m)',fontsize=9)
152.     plt.legend(label,fontsize=8)
153.     plt.title('Expected Microcar Displacement',fontsize=10)
154.
155.     #actual displacements
156.     plt.subplot(224)
157.     n=0
158.
159.     for value in x_act_disp_list:
160.         plt.scatter(x_act_disp_list[n],y_act_disp_list[n],marker='x')
161.         n+=1
162.
163.     plt.ylim(axis_lim)
164.     plt.xlim(axis_lim)
165.
166.     #graph
167.     plt.xlabel('Horizontal Disp (m)',fontsize=9)
168.     plt.ylabel('Vertical Disp (m)',fontsize=9)
169.     plt.legend(label,loc='upper left',fontsize=8)
170.     plt.title('Actual Microcar Displacement',fontsize=10)
171.
172.     plt.tight_layout() #stops the graphs from overlapping
173.     plt.show()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top