diff --git a/matplotlib/mpl_squares.py b/matplotlib/mpl_squares.py new file mode 100644 index 0000000000000000000000000000000000000000..beb2b6bd81d5a92f8e8df18a473946956952d0f1 --- /dev/null +++ b/matplotlib/mpl_squares.py @@ -0,0 +1,17 @@ +import matplotlib.pyplot as plt + +input_value = [1, 2, 3, 4, 5] +squares = [1, 4, 9, 16, 25] +# 绘制图形,linewidth是线条粗细 +plt.plot(input_value, 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) + +# 打开matplotlib查看器 +plt.show() diff --git a/matplotlib/random_walk.py b/matplotlib/random_walk.py new file mode 100644 index 0000000000000000000000000000000000000000..02de3cdc4e6370842739b4ee454a1bb119580990 --- /dev/null +++ b/matplotlib/random_walk.py @@ -0,0 +1,41 @@ +from random import choice + + +class RandomWalk: + """一个生成随机漫步数据的类""" + + def __init__(self, num_points=5000): + """初始化随机漫步的属性""" + self.num_points = num_points + + # 所有随机漫步都始于(0,0) + self.x_values = [0] + self.y_values = [0] + + def fill_walk(self): + """计算随机漫步包含的所有点""" + + # 不断漫步,知道列表达到指定的长度 + while len(self.x_values) < self.num_points: + # choice() 随机选择列表中的一个数 + # 决定前进方向以及沿这个方向前进的距离 + # x 的方向 + x_direction = choice([1, -1]) + # x 的距离 + 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 + + # 计算下一个点的x和y值 + 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) diff --git a/matplotlib/rw_visual.py b/matplotlib/rw_visual.py new file mode 100644 index 0000000000000000000000000000000000000000..2c90787b0f3fa9aa46ebc958245d3ad7cfea20ee --- /dev/null +++ b/matplotlib/rw_visual.py @@ -0,0 +1,30 @@ +import matplotlib.pyplot as plt + +from random_walk import RandomWalk + +# 只要程序处于活动状态,就不断地模拟随机漫步 +while True: + # 创建一个RandomWalk实例,并将其包含的点都绘制出来 + rw = RandomWalk(50000) + rw.fill_walk() + + # 设置绘图窗口的尺寸 + plt.figure(dpi=256, 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', edgecolor='none', s=100) + plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolor='none', s=100) + + # 隐藏坐标轴 + plt.axes().get_xaxis().set_visible(False) + plt.axes().get_yaxis().set_visible(False) + + plt.show() + + keep_running = input("是否再模拟一次随机漫步?(y/n):") + if keep_running == 'n': + break diff --git a/matplotlib/scatter_squares.py b/matplotlib/scatter_squares.py new file mode 100644 index 0000000000000000000000000000000000000000..0b7db36939fa6217bf3fe4a288bd55b3d3d17ed9 --- /dev/null +++ b/matplotlib/scatter_squares.py @@ -0,0 +1,18 @@ +import matplotlib.pyplot as plt + +# 使用scatter()绘制散点图并设置其样式 +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) + +# 打开matplotlib查看器 +plt.show() diff --git a/matplotlib/scatter_squares2.py b/matplotlib/scatter_squares2.py new file mode 100644 index 0000000000000000000000000000000000000000..b15943f6c5ac968524be492a7e1124b89a18c920 --- /dev/null +++ b/matplotlib/scatter_squares2.py @@ -0,0 +1,30 @@ +import matplotlib.pyplot as plt + +# 自动计算数据 +x_values = list(range(1, 1001)) +y_values = [x**2 for x in x_values] + +# matplotlib 在2.0.0版本中, edgecolor这个实参默认就为none +# 线条颜色可以 c='red' 这样指定,也可以自定义c=(0, 0, 0.8) +# c=y_values, cmap=plt.cm.Blues 这是使用了颜色映射 +# y值较小的点显示为浅蓝色,较大的显示为深蓝色 +plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues, 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]) + +# 打开matplotlib查看器 +# plt.show() + +# 自动保存图标 +# 'squares_plot.png' 当前脚本的所在目录保存 squares_plot.png 文件 +# bbox_inches='tight' 表示将空白的区域裁剪掉 +plt.savefig('squares_plot.png', bbox_inches='tight') diff --git a/matplotlib/squares_plot.png b/matplotlib/squares_plot.png new file mode 100644 index 0000000000000000000000000000000000000000..a5b0ed6f2b3cfd4c378e620edfca7ecf98ab3860 Binary files /dev/null and b/matplotlib/squares_plot.png differ