python在一个界面里画两张独立的折线图
时间: 2024-03-16 21:47:13 浏览: 31
您可以使用Python的Matplotlib库来绘制折线图。以下是一个示例代码,它将在一个界面中绘制两张独立的折线图:
```python
import matplotlib.pyplot as plt
# 数据集
x1 = [1, 2, 3, 4, 5]
y1 = [10, 8, 6, 4, 2]
x2 = [1, 2, 3, 4, 5]
y2 = [2, 4, 6, 8, 10]
# 绘制第一张图
plt.subplot(2, 1, 1) # 子图1
plt.plot(x1, y1)
plt.title('Line Chart 1')
plt.xlabel('X Label')
plt.ylabel('Y Label')
# 绘制第二张图
plt.subplot(2, 1, 2) # 子图2
plt.plot(x2, y2)
plt.title('Line Chart 2')
plt.xlabel('X Label')
plt.ylabel('Y Label')
# 显示图形
plt.show()
```
在这个示例中,我们使用`plt.subplot()`函数创建了两个子图,`plt.plot()`函数分别绘制了两个数据集的折线图,并使用`plt.title()`、`plt.xlabel()`和`plt.ylabel()`函数设置了图表的标题和轴标签。最后,使用`plt.show()`函数显示图形。
相关问题
python matplotlib 画两个折线图,在同一个界面里面
好的,可以使用 Matplotlib 库中的 subplot() 函数来实现在同一个界面中画多个子图,其中每个子图对应一个折线图。
示例代码如下:
```python
import matplotlib.pyplot as plt
# 定义 x 轴数据
x = [1, 2, 3, 4, 5]
# 定义 y1 和 y2 轴数据
y1 = [2, 4, 6, 8, 10]
y2 = [1, 3, 5, 7, 9]
# 创建画布,并在画布中创建两个子图
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
# 在第一个子图中画折线图
ax1.plot(x, y1)
ax1.set_xlabel('X Label')
ax1.set_ylabel('Y1 Label')
ax1.set_title('Line Plot 1')
# 在第二个子图中画折线图
ax2.plot(x, y2)
ax2.set_xlabel('X Label')
ax2.set_ylabel('Y2 Label')
ax2.set_title('Line Plot 2')
# 显示图形
plt.show()
```
在这个示例中,我们先定义了两组 x 轴和 y 轴的数据。然后使用 subplots() 函数创建了一个画布,并在画布中创建了两个子图。接着分别在两个子图中使用 plot() 函数画出了对应的折线图,并设置了 x 轴和 y 轴的标签和标题。最后使用 show() 函数显示了图形。
python matplotlib 画两个折线图,在同一个界面里面上下排列
好的,你可以使用 `subplots()` 函数创建画布,并设置 `nrows` 和 `ncols` 参数为 2 和 1,表示创建两行一列的子图布局。然后在每个子图中使用 `plot()` 函数画出对应的折线图,设置 x 轴和 y 轴的标签和标题。最后使用 `show()` 函数显示图形。
示例代码如下:
```python
import matplotlib.pyplot as plt
# 定义 x 轴数据
x = [1, 2, 3, 4, 5]
# 定义 y1 和 y2 轴数据
y1 = [2, 4, 6, 8, 10]
y2 = [1, 3, 5, 7, 9]
# 创建画布,并在画布中创建两个子图
fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, figsize=(10, 5))
# 在第一个子图中画折线图
ax1.plot(x, y1)
ax1.set_xlabel('X Label')
ax1.set_ylabel('Y1 Label')
ax1.set_title('Line Plot 1')
# 在第二个子图中画折线图
ax2.plot(x, y2)
ax2.set_xlabel('X Label')
ax2.set_ylabel('Y2 Label')
ax2.set_title('Line Plot 2')
# 调整子图之间的间距
plt.subplots_adjust(hspace=0.5)
# 显示图形
plt.show()
```
在这个示例中,我们使用 `subplots()` 函数创建了一个画布,并设置 `nrows` 和 `ncols` 参数为 2 和 1,表示创建两行一列的子图布局。接着在每个子图中使用 `plot()` 函数画出对应的折线图,并设置 x 轴和 y 轴的标签和标题。然后使用 `subplots_adjust()` 函数调整了两个子图之间的间距。最后使用 `show()` 函数显示了图形。