python绘制一张图内多条折线代码
时间: 2023-09-07 15:17:32 浏览: 191
以下是Python绘制多条折线的代码演示:
```python
import matplotlib.pyplot as plt
# 定义折线数据
x = [1, 2, 3, 4, 5, 6]
y1 = [2, 3, 2, 4, 3, 5]
y2 = [5, 4, 5, 3, 4, 2]
y3 = [1, 3, 2, 3, 3, 4]
# 绘制折线
plt.plot(x, y1, label='line1')
plt.plot(x, y2, label='line2')
plt.plot(x, y3, label='line3')
# 显示图例
plt.legend()
# 显示图表
plt.show()
```
运行代码后,将会显示一张包含三条折线的图表。你可以根据自己的需要修改数据和图表样式。
相关问题
python matplotlib 绘制多条折线图的代码
下面是绘制多条折线图的 Python Matplotlib 代码示例:
```python
import matplotlib.pyplot as plt
# 定义 x 轴和 y 轴数据
x = [1, 2, 3, 4, 5]
y1 = [1, 3, 5, 4, 2]
y2 = [2, 4, 6, 8, 10]
y3 = [5, 1, 3, 7, 4]
# 创建图形对象
fig = plt.figure()
# 添加子图
ax = fig.add_subplot(111)
# 绘制多条折线图
ax.plot(x, y1, label='Line 1')
ax.plot(x, y2, label='Line 2')
ax.plot(x, y3, label='Line 3')
# 设置图例位置
ax.legend(loc='upper right')
# 设置 x 轴和 y 轴标签
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
# 设置图形标题
plt.title('Multiple Lines')
# 显示图形
plt.show()
```
其中,`x` 是 x 轴数据,`y1`、`y2`、`y3` 是三条折线图的 y 轴数据。`fig` 是图形对象,`ax` 是子图对象。`ax.plot()` 方法用于绘制折线图,`label` 参数用于设置每条折线图的标签。`ax.legend()` 方法用于设置图例位置。`ax.set_xlabel()` 和 `ax.set_ylabel()` 方法用于设置 x 轴和 y 轴标签。`plt.title()` 方法用于设置图形标题。最后调用 `plt.show()` 方法显示图形。
python 绘制多条折线图
可以使用 Matplotlib 库来绘制多条折线图。以下是一个简单的示例代码:
```python
import matplotlib.pyplot as plt
# 数据
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [1, 8, 27, 64, 125]
y3 = [1, 2, 4, 8, 16]
# 绘制折线图
plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.plot(x, y3, label='Line 3')
# 添加图例
plt.legend()
# 添加标题和标签
plt.title('Multiple Line Chart')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
# 显示图形
plt.show()
```
运行后会显示一个包含三条折线图的图形。可以根据需要修改数据和样式。
阅读全文
相关推荐













