Python3 学习笔记(十二)生成数据
参考书籍《Python编程:从入门到实践》【美】Eric Matthes
数字的三次方被称为其立方。请绘制一个图形,显示前5000个整数的立方值
import matplotlib.pyplot as plt
x_values = list(range(1, 5001))
y_values = [value**3 for value in x_values]
plt.scatter(x_values, y_values)
plt.tick_params(axis='both', labelsize=14)
plt.show()
给你前面绘制的立方图指定颜色映射。
import matplotlib.pyplot as plt
x_values = list(range(1, 5001))
y_values = [value**3 for value in x_values]
plt.scatter(x_values, y_values, c='red')
plt.tick_params(axis='both', labelsize=14)
plt.show()
模拟随机漫步
#random_walk.py
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([