Files
notes_estom/Python/matplotlab/gallery/recipes/create_subplots.md
2020-09-26 22:03:11 +08:00

46 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 轻松创建子图
在matplotlib的早期版本中如果你想使用pythonic API并创建一个图形实例并从中创建一个子图的网格可能有共享轴它涉及相当数量的样板代码。例如
```python
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(50)
# old style
fig = plt.figure()
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222, sharex=ax1, sharey=ax1)
ax3 = fig.add_subplot(223, sharex=ax1, sharey=ax1)
ax3 = fig.add_subplot(224, sharex=ax1, sharey=ax1)
```
![轻松创建子图示例](https://matplotlib.org/_images/sphx_glr_create_subplots_001.png)
费尔南多·佩雷斯提供了一个很好的方法来创建子图的一切 ``subplots()``最后注意“s”并为整个群体打开x和y共享。您可以单独打开轴...
```python
# new style method 1; unpack the axes
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=True, sharey=True)
ax1.plot(x)
```
![轻松创建子图示例2](https://matplotlib.org/_images/sphx_glr_create_subplots_002.png)
或者将它们作为支持numpy索引的numrows x numcolumns对象数组返回
```python
# new style method 2; use an axes array
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
axs[0, 0].plot(x)
plt.show()
```
![轻松创建子图示例3](https://matplotlib.org/_images/sphx_glr_create_subplots_003.png)
## 下载这个示例
- [下载python源码: create_subplots.py](https://matplotlib.org/_downloads/create_subplots.py)
- [下载Jupyter notebook: create_subplots.ipynb](https://matplotlib.org/_downloads/create_subplots.ipynb)