美文网首页
Python 编程从入门到实践4

Python 编程从入门到实践4

作者: 蜘蛛的梦呓 | 来源:发表于2018-04-02 00:18 被阅读0次

    今天看的有点少,测试和项目1直接跳过,直接看项目2。
    15.2 绘制简单的折线图

    import matplotlib.pyplot as plt
    
    squares = [1, 4, 9, 16, 25]
    plt.plot(squares)
    plt.show()
    

    15.2.1 修改标签文字的线条粗细

    import matplotlib.pyplot as plt
    
    input_value = [1, 2, 3, 4, 5]
    squares = [1, 4, 9, 16, 25]
    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)
    plt.show()
    

    15.2.3 使用 scatter() 绘制散点图并设置样式

    import matplotlib.pyplot as plt
    
    plt.scatter(2, 4, s=200)
    
    # 设置图表标题,并给坐标轴加上标签
    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()
    

    15.2.4 使用 scatter() 绘制一系列点

    import matplotlib.pyplot as plt
    
    x_values = [1, 2, 3, 4, 5]
    y_values = [1, 4, 9, 16, 25]
    plt.scatter(x_values, y_values, s=50)
    # 设置图表标题,并给坐标轴加上标签
    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()
    

    15.2.5 自动计算数据

    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, s=20)
    # 15.2.6 删除数据点的轮廓
    # plt.scatter(x_values, y_values, edgecolors='none', s=20)
    # 15.2.7 自定义颜色
    plt.scatter(x_values, y_values, c='red', edgecolors='none', s=20)
    
    # 设置图表标题,并给坐标轴加上标签
    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()
    

    15.2.8 使用颜色映射

    颜色映射是一系列颜色,它们从起始颜色渐变到结束颜色。在可视化中,颜色映射用于突出颜色的规律,例如,用较浅的颜色显示较小的值,并用较深的颜色来显示较大的值。

    x_values = list(range(1, 1001))
    y_values = [x ** 2 for x in x_values]
    
    # 我们将参数 c 设置成了一个 y 值列表,并告诉参数 cmap 用哪个颜色映射,这样 y 值较小的点显示为浅蓝色, y 值较大的点显示为深蓝色
    
    plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues, edgecolors='none', s=20)
    # 设置图表标题,并给坐标轴加上标签
    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()
    
    # 15.2.9 自动保存图表
    # 第二个参数指定将图表多余的空白区域裁掉,如若保留,可忽略这个参数
    plt.savefig('squares_plot.png', bbox_inches='tight')
    

    15.3.1 创建 RandomWalk() 类

    from random import choice
    
    
    class RandomWalk():
        # 一个生产随机漫步数据的类
    
        def __init__(self, num_points=500):
            # 初始化随机漫步的属性
            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_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
    
                # 计算下一个点的 x 和 y值
                # 为获取漫步中下一个点的 x 值,我们将 x_step 与x_values中的最后一个值相加,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)
    

    15.3.4 模拟多次漫步

    import matplotlib.pyplot as plt
    from random_walk import RandomWalk
    
    while True:
        rw = RandomWalk()
        rw.fill_walk()
        plt.scatter(rw.x_values, rw.y_values, s=5)
        plt.show()
    
        keep_running = input('Make another walk?(y/n):')
        if keep_running == 'n':
            break
    

    15.3.5 设置随机漫步图的样式

    # 在本节中,我们将定制图表,以突出每次漫步的重要特征,并让分散注意力的元素不那么显眼
    
    
    while True:
        # 增加点数
        rw = RandomWalk(50000)
        # rw = RandomWalk()
        rw.fill_walk()
    
        # 15.3.10 调整尺寸
        # 设置绘图窗口的尺寸
        #函数 figure() 用于指定图表的宽度、高度、分辨率以及背景色。
        plt.figure(dpi=80,figsize=(10, 6))
    
        point_numbers = list(range(rw.num_points))
        # 15.3.6 给点着色
        # 我们将使用颜色映射来之处漫步中各点的先后顺序,并删除黑色轮廓,让它们的颜色更加明显
        plt.scatter(rw.x_values, rw.y_values, c=point_numbers, cmap=plt.cm.Blues, edgecolors='none', s=1)[图片上传中...(Figure_1.png-cd33eb-1522599369527-0)]
    
    
        # 15.3.7 重新绘制起点和终点
        # 突出起点和终点
        plt.scatter(0, 0, c='green', edgecolors='none', s=50)
        plt.scatter(rw.x_values[-1], rw.y_values[-1], c='red', edgecolors='none', s=50)
    
        # 15.3.8 隐藏坐标轴
        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
    

    今天暂时到这里了,每天应该能看完项目2。

    相关文章

      网友评论

          本文标题:Python 编程从入门到实践4

          本文链接:https://www.haomeiwen.com/subject/trbzcftx.html