python&Matplotlib六:Matplotlib的图例和注释功能

1.添加图例:

import matplotlib.pyplot as plt

# 准备数据
x = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10]
y2 = [1, 3, 5, 7, 9]

# 创建折线图并设置标签
plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')

# 添加图例
plt.legend()

# 添加标题和标签
plt.title("折线图示例")
plt.xlabel("X轴")
plt.ylabel("Y轴")

# 显示图形
plt.show()

在上述示例中,通过在调用plot()函数时设置label参数来给每条线条指定一个标签,然后通过plt.legend()添加图例。

2.添加注释:

import matplotlib.pyplot as plt

# 准备数据
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# 创建折线图
plt.plot(x, y)

# 添加注释
plt.annotate('Max Value', xy=(x[y.index(max(y))], max(y)), xytext=(3, 8),
             arrowprops=dict(facecolor='black', arrowstyle='->'))

# 添加标题和标签
plt.title("折线图示例")
plt.xlabel("X轴")
plt.ylabel("Y轴")

# 显示图形
plt.show()

在上述示例中,通过plt.annotate()函数添加注释。xy参数指定注释位置的坐标,xytext参数指定注释文本的位置坐标,arrowprops参数可以设置注释箭头的样式。

以上示例展示了如何使用Matplotlib添加图例和注释。你可以根据需要设置不同的参数来自定义图例和注释的外观。

相关推荐

  1. python&MatplotlibMatplotlib图例注释功能

    2024-01-07 15:22:01       60 阅读
  2. python&Matplotlib四:Matplotlib设置图样式颜色

    2024-01-07 15:22:01       56 阅读
  3. Python强大框架——Matplotlib

    2024-01-07 15:22:01       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-07 15:22:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-07 15:22:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-07 15:22:01       87 阅读
  4. Python语言-面向对象

    2024-01-07 15:22:01       96 阅读

热门阅读

  1. tf特征处理常用函数

    2024-01-07 15:22:01       61 阅读
  2. 前端要学哪些

    2024-01-07 15:22:01       58 阅读
  3. 浏览器渲染原理(面试重点)

    2024-01-07 15:22:01       53 阅读
  4. leetcode:32.有效的字母异位词

    2024-01-07 15:22:01       60 阅读
  5. 动态规划问题

    2024-01-07 15:22:01       59 阅读
  6. 【Python】关于Python的Pillow库

    2024-01-07 15:22:01       46 阅读