4.2.md 3.3 KB
Newer Older
W
v2.0  
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
# 基本的文本命令

> 原文:[Basic text commands](http://matplotlib.org/users/text_intro.html#basic-text-commands)

> 译者:[飞龙](https://github.com/)

> 协议:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)

## text


`Axes`的任意位置添加文本。

命令式:[`matplotlib.pyplot.text`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.text),面向对象:[`matplotlib.axes.Axes.text`](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.text)


## xlabel

向 x 轴添加轴标签。

命令式:[`matplotlib.pyplot.xlabel`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.xlabel),面向对象:[`matplotlib.axes.Axes.set_xlabel`](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_xlabel)

## ylabel

向 y 轴添加轴标签。

命令式:[`matplotlib.pyplot.ylabel`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.ylabel),面向对象:[`matplotlib.axes.Axes.set_ylabel`](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_ylabel)

## title

`Axes`添加标题。

命令式:[`matplotlib.pyplot.title`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.title),面向对象:[`matplotlib.axes.Axes.set_title`](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.set_title)

## figtext

`Figure`的任意位置添加文本。

命令式:[`matplotlib.pyplot.figtext`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.figtext),面向对象:[`matplotlib.figure.Figure.text`](http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.text)

## suptitle

`Figure`添加标题。

命令式:[`matplotlib.pyplot.suptitle`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.suptitle),面向对象:[`matplotlib.figure.Figure.suptitle`](http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.suptitle)

## annotate

`Axes`添加标注,带有可选的箭头。

命令式:[`matplotlib.pyplot.annotate`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.annotate),面向对象:[`matplotlib.axes.Axes.annotate`](http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.annotate)

所有这些函数创建并返回一个`matplotlib.text.Text()`实例,它可以配置各种字体和其他属性。 下面的示例在实战中展示所有这些命令。

```py
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt

fig = plt.figure()
fig.suptitle('bold figure suptitle', fontsize=14, fontweight='bold')

ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.85)
ax.set_title('axes title')

ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')

ax.text(3, 8, 'boxed italics text in data coords', style='italic',
        bbox={'facecolor':'red', 'alpha':0.5, 'pad':10})

ax.text(2, 6, r'an equation: $E=mc^2$', fontsize=15)

ax.text(3, 2, u'unicode: Institut f\374r Festk\366rperphysik')

ax.text(0.95, 0.01, 'colored text in axes coords',
        verticalalignment='bottom', horizontalalignment='right',
        transform=ax.transAxes,
        color='green', fontsize=15)


ax.plot([2], [1], 'o')
ax.annotate('annotate', xy=(2, 1), xytext=(3, 4),
            arrowprops=dict(facecolor='black', shrink=0.05))

ax.axis([0, 10, 0, 10])

plt.show()
```

![](http://matplotlib.org/_images/text_commands.png)