《Python 程式設計:從入門到實踐》第十五章(生成資料)練習題答案
阿新 • • 發佈:2018-12-11
# -*- coding: gbk -*- import matplotlib.pyplot as plt input_values = [1,2,3,4,5] squares = [1,4,9,16,25] plt.plot(input_values,squares,linewidth=5) plt.title('Square Numbers',fontsize = 24) plt.xlabel('Value',fontsize = 14) plt.ylabel('Square of Value',fontsize=14) plt.tick_params(axis='both',labelsize=14) plt.show() __________________________ x_values = [1,2,3,4,5] y_values = [1,4,9,16,25] plt.scatter(x_values,y_values,s=100) plt.title('Square Numbers',fontsize = 24) plt.xlabel('Value',fontsize = 14) plt.ylabel('Square of Value',fontsize=14) plt.tick_params(axis='both',which='major',labelsize=14) plt.show() ___________________________ # -*- coding: gbk -*- import matplotlib.pyplot as plt x_values = list(range(1,1001)) y_values = [x**2 for x in x_values] #plt.scatter(x_values,y_values,c='red',edgecolor ='none', s=40) #RGB設定顏色 #plt.scatter(x_values,y_values,c=(0.8,0,0),edgecolor ='none', s=40) #根據Y設定顏色 plt.scatter(x_values,y_values,c=y_values,cmap = plt.cm.Reds,edgecolor ='none', s=40) plt.title('Square Numbers',fontsize = 24) plt.xlabel('Value',fontsize = 14) plt.ylabel('Square of Value',fontsize=14) plt.tick_params(axis='both',which='major',labelsize=14) plt.axis([0,1100,0,1100000]) #結果顯示 #plt.show() #結果儲存 plt.savefig('squares_plot.png',bbox_inches='tight') _______________________________ #15-1 x_values = list(range(1,5000)) y_values = [x**3 for x in x_values] plt.scatter(x_values,y_values,edgecolor ='none', s=40) plt.title('Square Numbers',fontsize = 24) plt.xlabel('Value',fontsize = 14) plt.ylabel('Square of Value',fontsize=14) plt.tick_params(axis='both',which='major',labelsize=14) #結果顯示 plt.show() ____________________________________ from random import choice class RandomWalk(): def __init__(self,num_points = 5000): self.num_points = num_points self.x_values = [0] self.y_values = [0] def fill_walk(self): while len(self.x_values)<self.num_points: x_direction = choice([1,-1]) x_distance = choice([0,1,2,3,4]) x_step = x_direction * x_distance y_direction = choice([1,-1]) y_distance = choice([0,1,2,3,4]) y_step = y_direction * y_distance if x_step == 0 and y_step ==0: continue next_x = self.x_values[-1] + x_step next_y = self.y_values[-1] + y_step self.x_values.append(next_x) self.y_values.append(next_y) ———————————————————————————————————— import matplotlib.pyplot as plt from bf import RandomWalk rw = RandomWalk() rw.fill_walk() plt.scatter(rw.x_values,rw.y_values,s=15) plt.show() ———————————————————————————————————————————— # -*- coding: gbk -*- import matplotlib.pyplot as plt from import_file import RandomWalk while True: rw = RandomWalk(5000) rw.fill_walk() #設定繪圖視窗尺寸 plt.figure(dpi=128,figsize=(10,6)) point_numbers = list(range(rw.num_points)) plt.scatter(rw.x_values,rw.y_values,c=point_numbers,cmap=plt.cm.Blues,edgecolor='none',s=1) plt.scatter(0,0,c='green',edgecolors='none',s=100) plt.scatter(rw.x_values[-1],rw.y_values[-1],c='red',edgecolors='none',s=100) plt.axes().get_xaxis().set_visible(False) plt.axes().get_yaxis().set_visible(False) plt.show() keep_running = input('Make another walk?(y/n):') if(keep_running=='n'): break —————————————————————————————————————————————————————————————————————— #15-3 import matplotlib.pyplot as plt from import_file import RandomWalk while True: rw = RandomWalk(5000) rw.fill_walk() plt.scatter(rw.x_values,rw.y_values,linewidth=5) plt.show() keep_running = input('Make another walk?(y/n):') if(keep_running=='n'): break —————————————————————————————————————————————————————————————————————— #15-5 from random import choice class RandomWalk(): def __init__(self,num_points = 5000): self.num_points = num_points self.x_values = [0] self.y_values = [0] def fill_walk(self): while len(self.x_values)<self.num_points: x_step = self.get_step() y_step = self.get_step() if x_step == 0 and y_step ==0: continue next_x = self.x_values[-1] + x_step next_y = self.y_values[-1] + y_step self.x_values.append(next_x) self.y_values.append(next_y) def get_step(self): direction = choice([1,-1]) distance = choice([0,1,2,3,4,5,6,7,8]) step = direction * distance return step #15-8 from import_file import Die die1 = Die(8) die2 = Die(8) die3 = Die(8) results = [] for roll_num in range(5000): result = die1.roll()+die2.roll()+die3.roll() results.append(result) print(results) frequencies = [] max_result = die1.num_sides + die2.num_sides + die3.num_sides for value in range(3,max_result): frequency = results.count(value) frequencies.append(frequency) #print(frequentcies) hist = pygal.Bar() hist.title = "Results of rolling two D6 1000 times." hist.x_labels = range(3,max_result+1) hist.y_title = 'frequency of result' hist.add('D6',frequencies) hist.render_to_file('die_visual2.svg')