温馨提示×

怎么使用Matplotlib绘制复合折线图

小亿
83
2024-05-21 14:22:24
栏目: 编程语言

要绘制复合折线图,即多条折线图在同一张图上显示,可以使用Matplotlib库中的plot函数。以下是一个简单的示例代码,演示如何使用Matplotlib绘制复合折线图:

import matplotlib.pyplot as plt

# 创建数据
x = [1, 2, 3, 4, 5]
y1 = [1, 2, 3, 4, 5]
y2 = [5, 4, 3, 2, 1]

# 绘制第一条折线图
plt.plot(x, y1, label='Line 1', color='b')

# 绘制第二条折线图
plt.plot(x, y2, label='Line 2', color='r')

# 添加图例
plt.legend()

# 添加标题和标签
plt.title('Complex Line Chart')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')

# 显示图形
plt.show()

运行以上代码,将生成一个包含两条折线图的复合折线图,并显示在一个图形窗口中。您可以根据实际需求调整数据、颜色和其他参数,以满足不同的绘图要求。Matplotlib提供了丰富的功能和选项,使用户可以灵活地定制和美化图形。

0