【CV】使用 matplotlib.pyplot 绘制统计图、坐标系原点在不同的位置和添加辅助点和辅助线

本文介绍了如何使用Matplotlib库在Python中创建图表,包括设置坐标原点的位置(左下角、左上角和中间),添加辅助点及文字标注,以及绘制辅助线。展示了如何调整坐标轴范围和刻度,以满足不同的可视化需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 数据

这里使用模拟数据

import random
import matplotlib.pyplot as plt

    
values = [i * random.random() for i in range(100)]
values

[0.0,
 0.2596417433881839,
 1.0607353016866907,
...
 89.24287458194097,
 78.48300255421442]

2.坐标原点-左下角

坐标系原点在左下角,正常坐标系

def plot_normal(values):
    """
    1. 正常坐标系
    """
    plt.figure(figsize=(6.4, 3.2), dpi=100)
    # 画图
    plt.plot(values)
    # 坐标轴范围
    plt.axis((0, 100, 0, 100))
    # 坐标轴区间: x 为 10 , y 为 20
    plt.xticks([i * 10 for i in range(11)])
    plt.yticks([i * 20 for i in range(6)])
    plt.tight_layout()
    plt.show()


plot_normal(values)

在这里插入图片描述

3.坐标原点-左上角

坐标系原点在左上角-图像坐标

def plot_origin_left_top(values):
    """
    2. 坐标原点在左上角
    """
    plt.figure(figsize=(6.4, 3.2), dpi=100)
    ax = plt.subplot()
    ax.plot(values)
    # 坐标轴范围
    ax.axis((0, 100, 0, 100))
    # 坐标轴区间: x 为 10 , y 为 20
    ax.set_xticks([i * 10 for i in range(11)])
    ax.set_yticks([i * 20 for i in range(6)])
    # 坐标原点在左上角
    ax.xaxis.set_ticks_position("top")
    ax.invert_yaxis()
    # 显示
    plt.tight_layout()
    plt.show()
    
plot_origin_left_top(values)

在这里插入图片描述

4.坐标原点-在中间

坐标系原点在中间位置

def plot_origin_center(values):
    """
    3. 坐标原点在中间
    """
    plt.figure(figsize=(6.4, 3.2), dpi=100)
    ax = plt.subplot()
    ax.plot(values)
    # 坐标轴范围
    ax.axis((-100, 100, -100, 100))
    # 坐标轴原点在中间
    ax.spines["top"].set_color("none")
    ax.spines["left"].set_position("zero")
    ax.spines["right"].set_color("none")
    ax.spines["bottom"].set_position("zero")
    # 显示
    plt.tight_layout()
    plt.show()
    
plot_origin_center(values)

在这里插入图片描述

5.坐标系-辅助点

坐标系,添加辅助点和并标记点文字信息

def plot_point_and_text(values, points):
    """
    4. 绘制辅助点和辅助点文字
    """
    plt.figure(figsize=(6.4, 3.2), dpi=100)
    ax = plt.subplot()
    ax.plot(values)
    # 辅助点
    point_xx = [point[0] for point in points]
    point_yy = [point[1] for point in points]
    point_cc = ["r" for i in range(len(points))]
    point_labels = ["A", "B", "C", "D"]
    ax.scatter(x=point_xx, y=point_yy, c=point_cc)
    for index, point in enumerate(points):
        ax.text(
            x=point[0] * 1.01,
            y=point[1] *1.01,
            s=point_labels[index],
            fontsize=10,
            color="r",
            style="italic",
            weight="light",
        )
    # 坐标轴范围
    ax.axis((0, 100, 0, 100))
    # 坐标轴区间: x 为 10 , y 为 20
    ax.set_xticks([i * 10 for i in range(11)])
    ax.set_yticks([i * 20 for i in range(6)])
    # 显示
    plt.tight_layout()
    plt.show()


points = [[30, 30], [50, 50], [80, 80]]
plot_point_and_text(values, points)

在这里插入图片描述

6.坐标系-辅助线

坐标系,添加横向和纵向辅助参考线

def plot_hline(values, lines):
    """
    5. 绘制辅助线
    """
    plt.figure(figsize=(6.4, 3.2), dpi=100)
    ax = plt.subplot()
    ax.plot(values)
    # 方式1: 基于坐标轴的辅助线
    ax.axhline(y=40, c="lightgrey", ls="dotted")
    ax.axhline(y=80, c="lightgrey", ls="dotted")
    # 方式2: 画线
    for line in lines:
        line_xx = [point[0] for point in line]
        line_yy = [point[1] for point in line]
        ax.plot(line_xx, line_yy, c="r", ls="dotted")
    # 坐标轴范围
    ax.axis((0, 100, 0, 100))
    # 坐标轴区间: x 为 10 , y 为 20
    ax.set_xticks([i * 10 for i in range(11)])
    ax.set_yticks([i * 20 for i in range(6)])
    # 显示
    plt.tight_layout()
    plt.show()


lines = [([50, 0], [50, 100]),([20, 40], [80, 80])]
plot_hline(values, lines)
  • 灰色为方式一,添加的灰色辅助线
  • 红色为方式二,添加的红色辅助线
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值