为什么plt.plot正常,但是fig, axs = plt.subplots(2, 2, figsize=(12, 4)) 用axs[0].plot就会报错
时间: 2024-03-21 19:43:32 浏览: 28
可能是因为 `axs` 是一个包含 2 行 2 列的 `AxesSubplot` 对象数组,需要使用类似于 `axs[row_index][col_index].plot` 这样的方法访问每个子图。所以,对于第一个子图,你需要使用 `axs[0, 0].plot` 来绘制。具体的代码可以参考以下示例:
```
import matplotlib.pyplot as plt
import numpy as np
# 创建一个包含 2 行 2 列的子图
fig, axs = plt.subplots(2, 2, figsize=(12, 4))
# 在第一个子图中绘制 sin(x)
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
axs[0, 0].plot(x, y)
# 在第二个子图中绘制 cos(x)
y = np.cos(x)
axs[0, 1].plot(x, y)
# 在第三个子图中绘制 tan(x)
y = np.tan(x)
axs[1, 0].plot(x, y)
# 在第四个子图中绘制 exp(x)
y = np.exp(x)
axs[1, 1].plot(x, y)
# 显示图形
plt.show()
```
相关问题
fig, axs = plt.subplots(2, 2, figsize=(12, 4))
这段代码创建了一个大小为 12x4 的画布,并将画布分成 2 行 2 列的四个子图区域。`axs` 是一个包含四个子图对象的二维数组,可以分别在每个子图中绘制不同的图形。
下面是一个例子,展示如何在这四个子图中绘制不同的图形:
```python
import matplotlib.pyplot as plt
import numpy as np
# 生成数据
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)
y4 = np.exp(x)
# 创建画布和子图
fig, axs = plt.subplots(2, 2, figsize=(12, 4))
# 在子图中绘制图形
axs[0, 0].plot(x, y1)
axs[0, 0].set_title('sin(x)')
axs[0, 1].plot(x, y2)
axs[0, 1].set_title('cos(x)')
axs[1, 0].plot(x, y3)
axs[1, 0].set_title('tan(x)')
axs[1, 1].plot(x, y4)
axs[1, 1].set_title('exp(x)')
# 显示图形
plt.show()
```
运行以上代码,会在同一画布上绘制四个不同的图形:正弦曲线、余弦曲线、正切曲线和指数曲线。注意,在二维数组中使用 `axs[row, col]` 的方式访问每个子图对象。
fig, axs = plt.subplots(r,shared=‘all’)
`fig, axs = plt.subplots(r, shared='all')` 是一个 Matplotlib 绘制多个子图时比较方便的方式,它可以创建一个 `r` 行、`r` 列的子图网格,并将它们存储在一个名为 `axs` 的二维 numpy 数组中。
`shared='all'` 参数表示所有子图共享相同的 x 轴和 y 轴刻度。这意味着,如果某个子图的数据范围改变了,所有其他子图的刻度也会相应地调整。
以下是一个简单的示例代码:
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
fig, axs = plt.subplots(2, 2, figsize=(8, 6), sharex='all', sharey='all')
axs[0, 0].plot(x, y1)
axs[0, 1].plot(x, y2)
axs[1, 0].plot(x, y1+y2)
axs[1, 1].plot(x, y1*y2)
plt.show()
```
在这里,`fig, axs = plt.subplots(2, 2, figsize=(8, 6), sharex='all', sharey='all')` 创建了一个 2x2 的子图网格,每个子图都共享相同的 x 轴和 y 轴刻度。然后,我们在四个子图中绘制了不同的函数。注意,`axs` 是一个 2x2 的 numpy 数组,可以用来对每个子图进行进一步的定制。
相关推荐
![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)