matplotlib多子图设置legend
时间: 2023-07-12 16:13:37 浏览: 156
在 Matplotlib 中,可以使用 `plt.legend()` 方法为图形添加图例。对于多子图的情况,可以使用 `fig.legend()` 方法在整个图形上添加一个全局图例,或者在每个子图上分别添加局部图例。
以下是一个简单的例子,展示了如何在多个子图上添加局部图例:
```python
import matplotlib.pyplot as plt
# 数据
x = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10]
y2 = [1, 3, 5, 7, 9]
# 创建子图
fig, axs = plt.subplots(1, 2, figsize=(10, 5))
# 在第一个子图上绘制折线图,并添加图例
axs[0].plot(x, y1, label='y1')
axs[0].plot(x, y2, label='y2')
axs[0].legend()
# 在第二个子图上绘制折线图,并添加图例
axs[1].plot(x, y1, label='y1')
axs[1].plot(x, y2, label='y2')
axs[1].legend()
# 显示图形
plt.show()
```
在上面的代码中,我们创建了一个包含两个子图的图形,并在每个子图上绘制了两条折线。然后使用 `ax.legend()` 方法在每个子图上添加局部图例,以便表示不同的线条。如果需要在整个图形上添加全局图例,可以使用 `fig.legend()` 方法。
你可以根据自己的需求,修改图例的位置、标签、字体大小等属性。
相关问题
matplotlib多子图设置间距
在 Matplotlib 中,可以使用 `subplots_adjust()` 方法来调整子图之间的间距。`subplots_adjust()` 方法接受四个参数:`left`、`right`、`bottom` 和 `top`,用于设置子图与画布边缘的距离。下面是一个简单的例子:
```python
import matplotlib.pyplot as plt
# 创建一个 2x2 的子图
fig, axs = plt.subplots(2, 2)
# 在第一个子图中画图
axs[0, 0].plot([1, 2, 3], [4, 5, 6])
# 在第二个子图中画图
axs[0, 1].plot([1, 2, 3], [6, 5, 4])
# 在第三个子图中画图
axs[1, 0].plot([1, 2, 3], [2, 4, 6])
# 在第四个子图中画图
axs[1, 1].plot([1, 2, 3], [6, 4, 2])
# 调整子图间距
plt.subplots_adjust(left=0.1, right=0.9, bottom=0.1, top=0.9, wspace=0.4, hspace=0.4)
plt.show()
```
在上面的代码中,我们使用 `plt.subplots_adjust()` 方法来调整子图间距,其中 `wspace` 和 `hspace` 参数用于设置子图之间的水平和垂直间距。
matplotlib多子图设置x标签
在 Matplotlib 中,设置子图的 x 轴标签可以使用 `set_xlabel()` 方法。下面是一个简单的例子:
```python
import matplotlib.pyplot as plt
# 创建一个 2x2 的子图
fig, axs = plt.subplots(2, 2)
# 在第一个子图中画图
axs[0, 0].plot([1, 2, 3], [4, 5, 6])
axs[0, 0].set_xlabel('X Label 1')
# 在第二个子图中画图
axs[0, 1].plot([1, 2, 3], [6, 5, 4])
axs[0, 1].set_xlabel('X Label 2')
# 在第三个子图中画图
axs[1, 0].plot([1, 2, 3], [2, 4, 6])
axs[1, 0].set_xlabel('X Label 3')
# 在第四个子图中画图
axs[1, 1].plot([1, 2, 3], [6, 4, 2])
axs[1, 1].set_xlabel('X Label 4')
plt.show()
```
在上面的代码中,我们使用 `set_xlabel()` 方法来设置 x 轴标签。
如果你想要对所有子图设置相同的 x 轴标签,可以在 `fig.supxlabel()` 方法中指定标签。示例如下:
```python
import matplotlib.pyplot as plt
# 创建一个 2x2 的子图
fig, axs = plt.subplots(2, 2)
# 在第一个子图中画图
axs[0, 0].plot([1, 2, 3], [4, 5, 6])
# 在第二个子图中画图
axs[0, 1].plot([1, 2, 3], [6, 5, 4])
# 在第三个子图中画图
axs[1, 0].plot([1, 2, 3], [2, 4, 6])
# 在第四个子图中画图
axs[1, 1].plot([1, 2, 3], [6, 4, 2])
# 设置 x 轴标签
fig.supxlabel('X Label')
plt.show()
```
在上面的代码中,我们使用 `fig.supxlabel()` 方法来设置 x 轴标签。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)