python matplotlib画折线图,两条折线,并分别对其命名
时间: 2023-06-26 21:08:24 浏览: 125
下面是一个简单的例子,可以画出两条折线,并分别对其命名:
```python
import matplotlib.pyplot as plt
# 数据
x = [1, 2, 3, 4, 5]
y1 = [10, 20, 30, 40, 50]
y2 = [15, 25, 35, 45, 55]
# 画图
plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
# 图例
plt.legend()
# 标题和坐标轴标签
plt.title('Two Lines')
plt.xlabel('X Label')
plt.ylabel('Y Label')
# 显示图像
plt.show()
```
在这个例子中,我们定义了两个列表 `y1` 和 `y2`,分别代表两条折线的纵坐标。然后使用 `plt.plot()` 函数画出这两条折线,并用 `label` 参数分别为它们命名。最后使用 `plt.legend()` 函数添加图例。我们还使用 `plt.title()`、`plt.xlabel()` 和 `plt.ylabel()` 函数添加了标题和坐标轴标签。
运行这段代码,你应该可以看到一张包含两条折线的图像,每条折线都有自己的图例。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)