Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
matplotlib-doc-zh
提交
dab8b6f3
M
matplotlib-doc-zh
项目概览
OpenDocCN
/
matplotlib-doc-zh
8 个月 前同步成功
通知
0
Star
192
Fork
111
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
matplotlib-doc-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
dab8b6f3
编写于
1月 23, 2017
作者:
W
wizardforcel
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
3.3
上级
e9283187
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
166 addition
and
0 deletion
+166
-0
3.3.md
3.3.md
+166
-0
未找到文件。
3.3.md
0 → 100644
浏览文件 @
dab8b6f3
# 使用 GridSpec 自定义子图位置
> 原文:[Customizing Location of Subplot Using GridSpec](http://matplotlib.org/users/gridspec.html)
> 译者:[飞龙](https://github.com/)
> 协议:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
`GridSpec`
指定子图将放置的网格的几何位置。 需要设置网格的行数和列数。 子图布局参数(例如,左,右等)可以选择性调整。
`SubplotSpec`
指定在给定
`GridSpec`
中的子图位置。
`subplot2grid`
一个辅助函数,类似于
`pyplot.subplot`
,但是使用基于 0 的索引,并可使子图跨越多个格子。
## `subplot2grid`基本示例
要使用subplot2grid,你需要提供网格的几何形状和网格中子图的位置。 对于简单的单网格子图:
```
py
ax
=
plt
.
subplot2grid
((
2
,
2
),(
0
,
0
))
```
等价于:
```
ax = plt.subplot(2,2,1)
```
```
nRow=2, nCol=2
(0,0) +-------+-------+
| 1 | |
+-------+-------+
| | |
+-------+-------+
```
要注意不想
`subplot`
,
`gridspec`
中的下标从 0 开始。
为了创建跨越多个格子的子图,
```
py
ax2
=
plt
.
subplot2grid
((
3
,
3
),
(
1
,
0
),
colspan
=
2
)
ax3
=
plt
.
subplot2grid
((
3
,
3
),
(
1
,
2
),
rowspan
=
2
)
```
例如,下列命令:
```
py
ax1
=
plt
.
subplot2grid
((
3
,
3
),
(
0
,
0
),
colspan
=
3
)
ax2
=
plt
.
subplot2grid
((
3
,
3
),
(
1
,
0
),
colspan
=
2
)
ax3
=
plt
.
subplot2grid
((
3
,
3
),
(
1
,
2
),
rowspan
=
2
)
ax4
=
plt
.
subplot2grid
((
3
,
3
),
(
2
,
0
))
ax5
=
plt
.
subplot2grid
((
3
,
3
),
(
2
,
1
))
```
会创建:
![](
http://matplotlib.org/_images/demo_gridspec01.png
)
## `GridSpec`和`SubplotSpec`
你可以显式创建
`GridSpec `
并用它们创建子图。
例如,
```
py
ax
=
plt
.
subplot2grid
((
2
,
2
),(
0
,
0
))
```
等价于:
```
py
import
matplotlib.gridspec
as
gridspec
gs
=
gridspec
.
GridSpec
(
2
,
2
)
ax
=
plt
.
subplot
(
gs
[
0
,
0
])
```
`gridspec `
示例提供类似数组(一维或二维)的索引,并返回
`SubplotSpec`
实例。例如,使用切片来返回跨越多个格子的
`SubplotSpec`
实例。
上面的例子会变成:
```
py
gs
=
gridspec
.
GridSpec
(
3
,
3
)
ax1
=
plt
.
subplot
(
gs
[
0
,
:])
ax2
=
plt
.
subplot
(
gs
[
1
,:
-
1
])
ax3
=
plt
.
subplot
(
gs
[
1
:,
-
1
])
ax4
=
plt
.
subplot
(
gs
[
-
1
,
0
])
ax5
=
plt
.
subplot
(
gs
[
-
1
,
-
2
])
```
![](
http://matplotlib.org/_images/demo_gridspec02.png
)
## 调整 `GridSpec`布局
在显式使用
`GridSpec`
的时候,你可以调整子图的布局参数,子图由
`gridspec`
创建。
```
py
gs1
=
gridspec
.
GridSpec
(
3
,
3
)
gs1
.
update
(
left
=
0.05
,
right
=
0.48
,
wspace
=
0.05
)
```
这类似于
`subplots_adjust`
,但是他只影响从给定
`GridSpec`
创建的子图。
下面的代码
```
py
gs1
=
gridspec
.
GridSpec
(
3
,
3
)
gs1
.
update
(
left
=
0.05
,
right
=
0.48
,
wspace
=
0.05
)
ax1
=
plt
.
subplot
(
gs1
[:
-
1
,
:])
ax2
=
plt
.
subplot
(
gs1
[
-
1
,
:
-
1
])
ax3
=
plt
.
subplot
(
gs1
[
-
1
,
-
1
])
gs2
=
gridspec
.
GridSpec
(
3
,
3
)
gs2
.
update
(
left
=
0.55
,
right
=
0.98
,
hspace
=
0.05
)
ax4
=
plt
.
subplot
(
gs2
[:,
:
-
1
])
ax5
=
plt
.
subplot
(
gs2
[:
-
1
,
-
1
])
ax6
=
plt
.
subplot
(
gs2
[
-
1
,
-
1
])
```
会产生
![](
http://matplotlib.org/_images/demo_gridspec03.png
)
## 使用 `SubplotSpec`创建 `GridSpec`
你可以从
`SubplotSpec`
创建
`GridSpec`
,其中它的布局参数设置为给定
`SubplotSpec`
的位置的布局参数。
```
py
gs0
=
gridspec
.
GridSpec
(
1
,
2
)
gs00
=
gridspec
.
GridSpecFromSubplotSpec
(
3
,
3
,
subplot_spec
=
gs0
[
0
])
gs01
=
gridspec
.
GridSpecFromSubplotSpec
(
3
,
3
,
subplot_spec
=
gs0
[
1
])
```
![](
http://matplotlib.org/_images/demo_gridspec04.png
)
## 使用`SubplotSpec`创建复杂嵌套的`GridSpec`
这里有一个更复杂的嵌套
`gridspec`
的示例,我们通过在每个 3x3 内部网格中隐藏适当的脊线,在 4x4 外部网格的每个单元格周围放置一个框。
![](
http://matplotlib.org/_images/demo_gridspec06.png
)
## 网格尺寸可变的`GridSpec`
通常,
`GridSpec`
创建大小相等的网格。你可以调整行和列的相对高度和宽度,要注意绝对高度值是无意义的,有意义的只是它们的相对比值。
```
py
gs
=
gridspec
.
GridSpec
(
2
,
2
,
width_ratios
=
[
1
,
2
],
height_ratios
=
[
4
,
1
]
)
ax1
=
plt
.
subplot
(
gs
[
0
])
ax2
=
plt
.
subplot
(
gs
[
1
])
ax3
=
plt
.
subplot
(
gs
[
2
])
ax4
=
plt
.
subplot
(
gs
[
3
])
```
![](
http://matplotlib.org/_images/demo_gridspec05.png
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录