当前位置 博文首页 > 孤寒者的博客:数据可视化入门篇----matplotlib库绘制简单的折线

    孤寒者的博客:数据可视化入门篇----matplotlib库绘制简单的折线

    作者:[db:作者] 时间:2021-07-25 22:01

    查看使用matplotlib可制作的各种图表,请访问我!,这是示例画廊。单击画廊中的图表,就可查看用于生成图表的代码!

    1.绘制简单的折线图

    import matplotlib.pyplot as plt
    
    squares = [1, 4, 9, 16, 25]
    plt.plot(squares)    # 函数尝试根据squares列表中的数字绘制出有意义的图形!
    plt.show()
    

    在这里插入图片描述

    (1)修改标签文字和线条粗细

    import matplotlib.pyplot as plt
    
    squares = [1, 4, 9, 16, 25]
    # 参数linewidth决定plot()绘制的线条的粗细
    plt.plot(squares, linewidth=5)
    
    # 设置图表标题,并给坐标轴加上标签     参数fontsize指定了图表中文字的大小。
    plt.title('Square Numbers', fontsize=24)
    plt.xlabel('Value', fontsize=14)
    plt.ylabel('Square of Value', fontsize=14)
    
    # 设置刻度标记的大小   参数labelsize设置刻度标记的字号。
    plt.tick_params(axis='both', labelsize=14)
    
    plt.show()
    

    在这里插入图片描述

    (2)校正图形

    我们会发现没有正确的绘制数据:折线图的终点指出4.0的平方为25!
    出现这个问题的原因是:当你向plot()提供一系列的数字时,它假设第一个数据点对应的x的坐标值是0,但我们的第一个点对应的x值为1。
    为改变这种默认行为,我们可以给plot()同时提供输入值和输出值。

    import matplotlib.pyplot as plt
    
    input_values = [1, 2, 3, 4, 5]
    squares = [1, 4, 9, 16, 25]
    # 参数linewidth决定plot()绘制的线条的粗细
    plt.plot(input_values, squares, linewidth=5)
    
    # 设置图表标题,并给坐标轴加上标签     参数fontsize指定了图表中文字的大小。
    plt.title('Square Numbers', fontsize=24)
    plt.xlabel('Value', fontsize=14)
    plt.ylabel('Square of Value', fontsize=14)
    
    # 设置刻度标记的大小   参数labelsize设置刻度标记的字号。
    plt.tick_params(axis='both', labelsize=14)
    
    plt.show()
    

    在这里插入图片描述

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

    要绘制一系列的点,可向scatter()传递两个分别包含x值和y值的列表:

    import matplotlib.pyplot as plt
    
    x_values = [1, 2, 3, 4, 5]
    y_values = [1, 4, 9, 16, 25]
    # 参数linewidth决定plot()绘制的线条的粗细
    plt.scatter(x_values, y_values, s=100)
    
    # 设置图表标题,并给坐标轴加上标签     参数fontsize指定了图表中文字的大小。
    plt.title('Square Numbers', fontsize=24)
    plt.xlabel('Value', fontsize=14)
    plt.ylabel('Square of Value', fontsize=14)
    
    # 设置刻度标记的大小   参数labelsize设置刻度标记的字号。
    plt.tick_params(axis='both', labelsize=14)
    
    plt.show()
    

    在这里插入图片描述

    (4)自动计算数据

    手工计算列表要包含的值可能效率低下,所以让python循环来替我们完成这种计算即可!下面是绘制1000个点的代码:

    import matplotlib.pyplot as plt
    
    x_values = list(range(1, 1001))
    y_values = [x**2 for x in x_values]
    # 参数linewidth决定plot()绘制的线条的粗细
    plt.scatter(x_values, y_values, s=40)
    
    # 设置图表标题,并给坐标轴加上标签     参数fontsize指定了图表中文字的大小。
    plt.title('Square Numbers', fontsize=24)
    plt.xlabel('Value', fontsize=14)
    plt.ylabel('Square of Value', fontsize=14)
    
    # 设置刻度标记的大小   参数labelsize设置刻度标记的字号。
    plt.tick_params(axis='both', labelsize=14)
    
    # 设置每个坐标轴的取值范围    axis()要求提供四个值:x和y坐标轴的最小值和最大值。
    plt.axis([0, 1000, 0, 1100000])
    
    plt.show()
    

    在这里插入图片描述

    (5)删除数据点的轮廓

    matplotlib允许你给散点图中的各个点指定颜色。默认为黑色点和无轮廓。要添加数据点的轮廓,可在调用scatter()时传递实参edgecolor=‘black’:

    import matplotlib.pyplot as plt
    
    x_values = [1, 2, 3, 4, 5]
    y_values = [1, 4, 9, 16, 25]
    # 参数linewidth决定plot()绘制的线条的粗细
    plt.scatter(x_values, y_values, edgecolors='black', s=100)
    
    # 设置图表标题,并给坐标轴加上标签     参数fontsize指定了图表中文字的大小。
    plt.title('Square Numbers', fontsize=24)
    plt.xlabel('Value', fontsize=14)
    plt.ylabel('Square of Value', fontsize=14)
    
    # 设置刻度标记的大小   参数labelsize设置刻度标记的字号。
    plt.tick_params(axis='both', labelsize=14)
    
    plt.show()
    

    在这里插入图片描述

    (6)自定义颜色

    要修改数据点的颜色,可向scatter()传递参数c,并将其设置为要使用的颜色的名称,如下:

    import matplotlib.pyplot as plt
    
    x_values = [1, 2, 3, 4, 5]
    y_values = [1, 4, 9, 16, 25]
    # 参数linewidth决定plot()绘制的线条的粗细
    plt.scatter(x_values, y_values, c='red', edgecolors='black', s=100)
    
    # 设置图表标题,并给坐标轴加上标签     参数fontsize指定了图表中文字的大小。
    plt.title('Square Numbers', fontsize=24)
    plt.xlabel('Value', fontsize=14)
    plt.ylabel('Square of Value', fontsize=14)
    
    # 设置刻度标记的大小   参数labelsize设置刻度标记的字号。
    plt.tick_params(axis='both', labelsize=14)
    
    plt.show()
    

    在这里插入图片描述

    **还可以使用RGB颜色模式自定义颜色。要指定自定义颜色,可传递参数c,并将其设置为一个元组,其中包含三个0~1之间的小数值,它们分别表示红绿蓝的分量。如下是一个淡蓝色点组成的散点图:
    plt.scatter(x_values, y_values, c=(0,0,0.8), edgecolors=‘black’, s=100)
    值越接近0,指定的颜色越深;值越接近1,指定的颜色越浅。
    **

    (7)使用颜色映射

    颜色映射是一系列颜色,它们从开始颜色渐变到结束颜色。在可视化中,颜色映射用于突出数据的规律!
    模块pyplot内置了一组颜色映射。要使用这些颜色映射,你要告诉pyplot该如何设置数据集中每个点的颜色。下面示范如何根据每个点的y值来设置其颜色:

    import matplotlib.pyplot as plt
    
    x_values = [1, 2, 3, 4, 5]
    y_values = [1, 4, 9, 16, 25]
    # 参数linewidth决定plot()绘制的线条的粗细   将参数c设置成了一个y值列表,并使用cmap参数告诉pyplot使用哪个颜色映射!
    plt.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Reds, edgecolors='none', s=100)
    
    # 设置图表标题,并给坐标轴加上标签     参数fontsize指定了图表中文字的大小。
    plt.title('Square Numbers', fontsize=24)
    plt.xlabel('Value', fontsize=14)
    plt.ylabel('Square of Value', fontsize=14)
    
    # 设置刻度标记的大小   参数labelsize设置刻度标记的字号。
    plt.tick_params(axis='both', labelsize=14)
    
    plt.show()
    

    在这里插入图片描述在这里插入图片描述

    (8)自动保存图表

    将对plt.show()的调用替换为对plt.savefig()的调用:

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

    第一个实参指定以什么样的文件名保存;
    第二个实参指定将图表多余的空白区域裁剪掉,如果要保留图表周围多余的空白区域,可省略这个实参。

    cs