python第15章生成数据课后习题答案

上传人:碎****木 文档编号:220862471 上传时间:2021-12-09 格式:DOCX 页数:16 大小:169.88KB
返回 下载 相关 举报
python第15章生成数据课后习题答案_第1页
第1页 / 共16页
python第15章生成数据课后习题答案_第2页
第2页 / 共16页
python第15章生成数据课后习题答案_第3页
第3页 / 共16页
亲,该文档总共16页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《python第15章生成数据课后习题答案》由会员分享,可在线阅读,更多相关《python第15章生成数据课后习题答案(16页珍藏版)》请在金锄头文库上搜索。

1、Solutions - Chapter 1515-1 : CubesA number raised to the third power is a cube . Plot the first five cubic numbers, and then plot the first 5000 cubic numbers.Plotting 5 cubes:from matplotlib import pyplot as plt# Define data.x_values = 1, 2, 3, 4, 5cubes = 1, 8, 27, 64, 125# Make plot.plt.scatter(x

2、_values, cubes, edgecolor=”none”, s=40)# Customize plot.plt.title(“Cubes“, fontsize=24) plt.xlabel(”Value”, fontsize=14) plt.ylabel(”Cube of Value”, fontsize=14) plt.tick_params(axis=”both”, labelsize=14)# Show plot.plt.show()Output:Plotting 5000 cubes:from matplotlib import pyplot as plt# Define da

3、ta.x_values = list(range(5001) cubes = x*3 for x in x_values# Make plot.plt.scatter(x_values, cubes, edgecolor=”none”, s=40)# Customize plot.plt.title(“Cubes“, fontsize=24) plt.xlabel(”Value”, fontsize=14) plt.ylabel(”Cube of Value”, fontsize=14) plt.tick_params(axis=”both”, labelsize=14) plt.axis(0

4、, 5100, 0, 5100*3)# Show plot.plt.show()Output:15-2 : Colored CubesApply a colormap to your cubes plot.from matplotlib import pyplot as plt# Define data.x_values = list(range(5001) cubes = x*3 for x in x_values# Make plot.plt.scatter(x_values, cubes, edgecolor=”none”, c=cubes, cmap=plt .BuGn, s=40)#

5、 Customize plot.plt.title(“Cubes“, fontsize=24) plt.xlabel(”Value”, fontsize=14) plt.ylabel(”Cube of Value”, fontsize=14) plt.tick_params(axis=”both”, labelsize=14) plt.axis(0, 5100, 0, 5100*3)# Show plot.plt.show()Output:15-3 : Molecular Motionplt.scatter()Modify rw_visual.pyby replacingwith plt.pl

6、ot(). tosimulate the path of a pollen grain on the surface of a drop of water,rw.x_valuespass in theand rw.y_values, and includealinewidth argument. Use 5000 instead of 50,000 points.import matplotlib.pyplot as pltfrom random_walk import RandomWalk# Keep making new walks, as long as the program is a

7、ctive.while True:# Make a random walk, and plot the points.rw = RandomWalk(5000) rw.fill_walk()# Set the size of the plotting window.plt.figure(dpi=128, figsize=(10, 6)point_numbers = list(range(rw.num_points)plt.plot(rw.x_values, rw.y_values, linewidth=1)# Emphasize the first and last points.plt.sc

8、atter(0, 0, c=”green”, edgecolors=”none”, s=75) plt.scatter(rw.x_values 1, rw.y_values 1, c=”red”,edgecolors=”none”,s=75)# Remove the axes.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 =

9、”n”:breakOutput:The scatter plots appear behind the lines. To place them on top of the lines, we can use the zorderargument. Plot elements withzorderhighervalues are placed on top of elements withlowerzorder values.import matplotlib.pyplot as plt from random_walk import RandomWalk# Keep making new w

10、alks, as long as the program is active.while True:# Make a random walk, and plot the points.rw = RandomWalk(5000) rw.fill_walk()# Set the size of the plotting window.plt.figure(dpi=128, figsize=(10, 6)point_numbers = list(range(rw.num_points) plt.plot(rw.x_values, rw.y_values, linewidth=1, zorder=1)

11、# Emphasize the first and last points.plt.scatter(0, 0, c=”green”, edgecolors=”none”, s=75, zorder=2)plt.scatter(rw.x_values 1, rw.y_values 1, c=”red”, edgecolors=”none”,s=75, zorder=2)# Remove the axes. plt.axes().get_xaxis().set_visible(False) plt.axes().get_yaxis().set_visible(False)plt.show()kee

12、p_running = input(“Make another walk? (y/n): “)if keep_running = ”n”:breakOutput:15-5 : RefactoringThe methodget_step()calledis lengthy. Create a new methodfill_walk()to determine the direction and distance for eachstep, and then calculate the step. You should end up with two callsget_step()toin fil

13、l_walk():x_step = get_step() y_step = get_step()fill_walk()This refactoring should reduce the size of method easier to read and understand.random_walk.py:and make thefrom random import choiceclass RandomWalk():“A class to generate random walks.“definit(self, num_points=5000): “Initialize attributes

14、of a walk.“ self.num_points = num_points# All walks start at (0, 0).self.x_values = 0self.y_values = 0def get_step(self):“Determine the direction and distance for a step.“ direction = choice(1,1)distance = choice(0, 1, 2, 3, 4) step = direction * distancereturn stepdef fill_walk(self):“Calculate all the points in the walk.“length.# Keep taking steps until the walk reaches the

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 教育/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号