Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
matplotlib-doc-zh
提交
cfd2b600
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 搜索 >>
提交
cfd2b600
编写于
1月 29, 2017
作者:
W
wizardforcel
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
3.5
上级
451fb4ef
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
133 addition
and
0 deletion
+133
-0
3.5.md
3.5.md
+133
-0
未找到文件。
3.5.md
浏览文件 @
cfd2b600
# 艺术家教程
> 原文:[Artist tutorial](http://matplotlib.org/users/artists.html)
> 译者:[飞龙](https://github.com/)
> 协议:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
matplotlib API 有三个层级。
`matplotlib.backend_bases.FigureCanvas`
是绘制图形的区域,
`matplotlib.backend_bases.Renderer`
是知道如何在
`ChartCanvas`
上绘制的对象,而
`matplotlib.artist.Artist`
是知道如何使用渲染器在画布上画图的对象。
`FigureCanvas`
和
`Renderer`
处理与用户界面工具包(如 wxPython)或 PostScript® 等绘图语言交互的所有细节,
`Artist`
处理所有高级结构,如表示和布局图形,文本和线条。用户通常要花费95%的时间来处理艺术家。
有两种类型的艺术家:基本类型和容器类型。基本类型表示我们想要绘制到画布上的标准图形对象:
`Line2D`
,
`Rectangle`
,
`Text`
,
`AxesImage`
等,容器是放置它们的位置(
`Axis`
,
`Axes`
和
`Figure`
)。标准用法是创建一个
`Figure`
实例,使用
`Figure`
创建一个或多个
`Axes`
或
`Subplot`
实例,并使用
`Axes`
实例的辅助方法来创建基本类型。在下面的示例中,我们使用
`matplotlib.pyplot.figure()`
创建一个
`Figure`
实例,这是一个便捷的方法,用于实例化
`Figure`
实例并将它们与您的用户界面或绘图工具包
`FigureCanvas`
连接。正如我们将在下面讨论的,这不是必须的 - 你可以直接使用 PostScript,PDF,Gtk+ 或 wxPython
`FigureCanvas`
实例,直接实例化你的图形并连接它们 - 但是因为我们在这里关注艺术家 API,我们让
`pyplot`
为我们处理一些细节:
...
...
@@ -314,3 +320,130 @@ for label in ax.get_xticklabels():
|
`yaxis`
|
`matplotlib.axis.YAxis`
实例 |
## 轴容器
`matplotlib.axis.Axis`
实例处理刻度线,网格线,刻度标签和轴标签的绘制。您可以分别为y轴配置左和右刻度,为x轴分别配置上和下刻度。
`Axis`
还存储在自动缩放,平移和缩放中使用的数据和视图间隔,以及
`Locator`
和
`Formatter`
实例,它们控制刻度位置以及它们表示为字符串的方式。
每个
`Axis`
对象都包含一个
`label`
属性(这是 pylab 在调用
`xlabel()`
和
`ylabel()`
时修改的东西)以及主和次刻度的列表。刻度是
`XTick`
和
`YTick`
实例,它包含渲染刻度和刻度标签的实际线条和文本基本类型。因为刻度是按需动态创建的(例如,当平移和缩放时),您应该通过访问器方法
`get_major_ticks()`
和
`get_minor_ticks()`
访问主和次刻度的列表。虽然刻度包含所有下面要提及的基本类型,
`Axis`
方法包含访问器方法来返回刻度线,刻度标签,刻度位置等:
```
py
In
[
285
]:
axis
=
ax
.
xaxis
In
[
286
]:
axis
.
get_ticklocs
()
Out
[
286
]:
array
([
0.
,
1.
,
2.
,
3.
,
4.
,
5.
,
6.
,
7.
,
8.
,
9.
])
In
[
287
]:
axis
.
get_ticklabels
()
Out
[
287
]:
<
a
list
of
10
Text
major
ticklabel
objects
>
# note there are twice as many ticklines as labels because by
# default there are tick lines at the top and bottom but only tick
# labels below the xaxis; this can be customized
In
[
288
]:
axis
.
get_ticklines
()
Out
[
288
]:
<
a
list
of
20
Line2D
ticklines
objects
>
# by default you get the major ticks back
In
[
291
]:
axis
.
get_ticklines
()
Out
[
291
]:
<
a
list
of
20
Line2D
ticklines
objects
>
# but you can also ask for the minor ticks
In
[
292
]:
axis
.
get_ticklines
(
minor
=
True
)
Out
[
292
]:
<
a
list
of
0
Line2D
ticklines
objects
>
```
下面是
`Axis`
的一些有用的访问器方法的总结(它们拥有相应的
`setter`
,如
`set_major_formatter`
)。
| 访问器方法 | 描述 |
| --- | --- |
| get_scale | 轴的比例,例如
`'log'`
或
`'linear'`
|
| get_view_interval | 轴视图范围的内部实例 |
| get_data_interval | 轴数据范围的内部实例 |
| get_gridlines | 轴的网格线列表 |
| get_label | 轴标签 -
`Text`
实例 |
| get_ticklabels |
`Text`
实例的列表 - 关键字
`minor=True|False`
|
| get_ticklines |
`Line2D`
实例的列表 - 关键字
`minor=True|False`
|
| get_ticklocs |
`Tick`
位置的列表 - 关键字
`minor=True|False`
|
| get_major_locator | 用于主刻度的
`matplotlib.ticker.Locator`
实例 |
| get_major_formatter | 用于主刻度的
`matplotlib.ticker.Formatter`
实例 |
| get_minor_locator | 用于次刻度的
`matplotlib.ticker.Locator`
实例 |
| get_minor_formatter | 用于次刻度的
`matplotlib.ticker.Formatter`
实例 |
| get_major_ticks | 用于主刻度的
`Tick`
实例列表 |
| get_minor_ticks | 用于次刻度的
`Tick`
实例列表 |
| grid | 为主或次刻度打开或关闭网格 |
这里是个例子,出于美观不太推荐,它自定义了轴域和刻度属性。
```
py
import
numpy
as
np
import
matplotlib.pyplot
as
plt
# plt.figure creates a matplotlib.figure.Figure instance
fig
=
plt
.
figure
()
rect
=
fig
.
patch
# a rectangle instance
rect
.
set_facecolor
(
'lightgoldenrodyellow'
)
ax1
=
fig
.
add_axes
([
0.1
,
0.3
,
0.4
,
0.4
])
rect
=
ax1
.
patch
rect
.
set_facecolor
(
'lightslategray'
)
for
label
in
ax1
.
xaxis
.
get_ticklabels
():
# label is a Text instance
label
.
set_color
(
'red'
)
label
.
set_rotation
(
45
)
label
.
set_fontsize
(
16
)
for
line
in
ax1
.
yaxis
.
get_ticklines
():
# line is a Line2D instance
line
.
set_color
(
'green'
)
line
.
set_markersize
(
25
)
line
.
set_markeredgewidth
(
3
)
plt
.
show
()
```
![](
http://matplotlib.org/_images/fig_axes_customize_simple.png
)
## 刻度容器
`matplotlib.axis.Tick`
是我们从
`Figure`
到
`Axes`
再到
`Axis`
再到
`Tick`
的最终的容器对象。
`Tick`
包含刻度和网格线的实例,以及上侧和下侧刻度的标签实例。 每个都可以直接作为
`Tick`
的属性访问。此外,也有用于确定上标签和刻度是否对应
`x`
轴,以及右标签和刻度是否对应
`y`
轴的布尔变量。
| 刻度属性 | 描述 |
| --- | --- |
|
`tick1line`
|
`Line2D`
实例 |
|
`tick2line`
|
`Line2D`
实例 |
|
`gridline`
|
`Line2D`
实例 |
|
`label1`
|
`Text`
实例 |
|
`label2`
|
`Text`
实例 |
|
`gridOn`
| 确定是否绘制刻度线的布尔值 |
|
`tick1On`
| 确定是否绘制主刻度线的布尔值 |
|
`tick2On`
| 确定是否绘制次刻度线的布尔值 |
|
`label1On`
| 确定是否绘制主刻度标签的布尔值 |
|
`label2On`
| 确定是否绘制次刻度标签的布尔值 |
这里是个例子,使用美元符号设置右侧刻度,并在
`y`
轴右侧将它们设成绿色。
```
py
import
numpy
as
np
import
matplotlib.pyplot
as
plt
import
matplotlib.ticker
as
ticker
# Fixing random state for reproducibility
np
.
random
.
seed
(
19680801
)
fig
=
plt
.
figure
()
ax
=
fig
.
add_subplot
(
111
)
ax
.
plot
(
100
*
np
.
random
.
rand
(
20
))
formatter
=
ticker
.
FormatStrFormatter
(
'$%1.2f'
)
ax
.
yaxis
.
set_major_formatter
(
formatter
)
for
tick
in
ax
.
yaxis
.
get_major_ticks
():
tick
.
label1On
=
False
tick
.
label2On
=
True
tick
.
label2
.
set_color
(
'green'
)
plt
.
show
()
```
![](
http://matplotlib.org/_images/dollar_ticks.png
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录