• Python 项目一 数据可视化 01


    一、安装 matplotlib

    奉上大佬安装链接
    https://blog.csdn.net/weixin_44768795/article/details/120613424

    下面所有内容来自《Python编程:从入门到实践》,如有侵权请联系

    二、绘制图形

    2.1 简单的折线图 plot

    import matplotlib.pyplot as plt
    squares = [1, 4, 9, 16, 25] #存储前述平方数
    plt.plot(squares)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述
    看图形好像有点对不上啊,稍微修改一下

    import matplotlib.pyplot as plt
    
    input_values = [1, 2, 3, 4, 5]
    squares = [1, 4, 9, 16, 25]
    # linewidth 线条的粗细
    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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述这就好看了嘛

    2.2 简单的散点图 scatter

    import matplotlib.pyplot as plt
    # plt.scatter(2, 4)
    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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    2.2.1 自动计算
    import matplotlib.pyplot as plt
    # plt.scatter(2, 4)
    先创建了一个包含x值的列表,其中包含数字1~1000。接下来是一个生成y值
    的列表解析,它遍历x值(for x in x_values),计算其平方值(x**2)
    x_values = list(range(1,1001))
    y_values = [x**2 for x in x_values]
    
    plt.scatter(x_values, y_values, s=40)
    # 设置图表标题并给坐标轴加上标签
    plt.title("Square Numbers", fontsize=24)
    plt.xlabel("Value", fontsize=14)
    plt.ylabel("Square of Value", fontsize=14)
    # 设置每个坐标轴的取值范围
    plt.axis([0, 1100, 0, 1100000])
    # 设置刻度标记的大小
    # plt.tick_params(axis='both', which='major', labelsize=14)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在这里插入图片描述

    2.2.2 颜色映射

    将控制颜色的参数c设置成了一个y值列表,并使用参数cmap告诉pyplot使用哪个颜色映射

    plt.scatter(x_values, y_values,c=y_values, cmap=plt.cm.Blues,edgecolor='none',s=40)
    
    • 1

    在这里插入图片描述

    2.2.3 自动保存图表

    第一个实参指定要以什么样的文件名保存图表,这个文件将存储到scatter_squares.py所在的
    目录中;第二个实参指定将图表多余的空白区域裁剪掉。

    plt.savefig('squares_plot.png', bbox_inches='tight') 
    
    • 1

    2.3 随机漫步图

    from random import choice
    import matplotlib.pyplot as plt
    # 创建RandomWalk的类,随机地选择前进方向。需要三个属性,
    # 一个是存储随机漫步次数的变量,其他两个是列表,分别存储随机漫步经过的
    # 每个点的x和y坐标
    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([1, -1])表示要么向右走1,要么向左走1
                x_direction = choice([1,-1])
                # choice([0, 1, 2, 3, 4]) 表示走多远
                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)
    # 创建一个实例对象,将其包含的点都绘制出来
    rw = RandomWalk()
    rw.fill_walk()
    plt.scatter(rw.x_values, rw.y_values, s=15)
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    温馨提示:有密恐的不要看
    在这里插入图片描述

    在这里插入图片描述

    2.3.1 颜色和起终点

    渐变颜色,使用了range()生成一个数字列表,包含的数字个数与漫步包含的点数相
    同。列表存储在point_numbers中,便于后面使用它来设置每个漫步点的颜色。
    将参数c设置为point_numbers,指定使用颜色映射Blues,
    传递实参edgecolor=none来删除每个点周围的轮廓。

    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=15)
    
    • 1
    • 2

    突出起点和终点

    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)
    
    • 1
    • 2
    • 3

    在这里插入图片描述

  • 相关阅读:
    如何用一款产品推动「品牌的惊险一跃」?
    优化程序性能
    码蹄集 - MT2093 · 回文数数位
    解决Verilog中的generate_for问题,就是生成多个块,其区别于for块。
    CSP-J 2023 入门级 第一轮 完善程序(2)
    操作系统的运行机制--操作系统内核负责的内容
    算法与数据结构-堆
    新兴国家战略级安全话题-软件供应链安全
    数学学习经验
    SBCS MBCS Unicode三种编码方式?
  • 原文地址:https://blog.csdn.net/rookie_0_0/article/details/125262853