温馨提示×

python怎么用matplotlib画折线图

小亿
83
2024-02-04 17:40:42
栏目: 编程语言

要用matplotlib画折线图,可以按照以下步骤进行操作:

  1. 导入所需的库和模块:
import matplotlib.pyplot as plt
  1. 准备数据:
x = [1, 2, 3, 4, 5]  # x轴数据
y = [5, 7, 3, 8, 4]  # y轴数据
  1. 创建绘图窗口:
plt.figure()
  1. 绘制折线图:
plt.plot(x, y)
  1. 添加标题、x轴标签和y轴标签:
plt.title('折线图')  # 标题
plt.xlabel('x轴')  # x轴标签
plt.ylabel('y轴')  # y轴标签
  1. 显示图形:
plt.show()

综合起来,完整的代码如下所示:

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]  # x轴数据
y = [5, 7, 3, 8, 4]  # y轴数据

plt.figure()
plt.plot(x, y)

plt.title('折线图')  # 标题
plt.xlabel('x轴')  # x轴标签
plt.ylabel('y轴')  # y轴标签

plt.show()

运行代码,就可以生成一个简单的折线图。

0