Skip to content

Commit 9bd25b3

Browse files
committed
4.5
1 parent 8a72761 commit 9bd25b3

File tree

1 file changed

+170
-1
lines changed

1 file changed

+170
-1
lines changed

4.5.md

+170-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ ax.set_ylim(-2,2)
2929
plt.show()
3030
```
3131

32+
[源代码](http://matplotlib.org/mpl_examples/pyplots/annotation_basic.py)
33+
3234
![](http://matplotlib.org/_images/annotation_basic.png)
3335

3436
在该示例中,`xy`(箭头尖端)和`xytext`位置(文本位置)都以数据坐标为单位。 有多种可以选择的其他坐标系 - 你可以使用`xycoords``textcoords`以及下列字符串之一(默认为`data`)指定`xy``xytext`的坐标系。
@@ -67,6 +69,8 @@ ax.annotate('local max', xy=(3, 1), xycoords='data',
6769

6870
在下面的示例中,`xy`点是原始坐标(`xycoords`默认为`'data'`)。 对于极坐标轴,它在`(theta, radius)`空间中。 此示例中的文本放置在图形小数坐标系中。 `matplotlib.text.Text`关键字`args`,例如`horizontalalignment``verticalalignment``fontsize`,从`annotate`传给`Text`实例。
6971

72+
[源代码](http://matplotlib.org/mpl_examples/pyplots/annotation_polar.py)
73+
7074
![](http://matplotlib.org/_images/annotation_polar.png)
7175

7276
注释(包括花式箭头)的所有高上大的内容的更多信息,请参阅[高级标注](http://matplotlib.org/users/annotations.html#plotting-guide-annotation)[`pylab_examples`示例代码:`annotation_demo.py`](http://matplotlib.org/examples/pylab_examples/annotation_demo.html#pylab-examples-annotation-demo)
@@ -365,4 +369,169 @@ matplotlib 中的标注支持[标注文本](http://matplotlib.org/users/annotati
365369

366370
![](http://matplotlib.org/_images/annotate_simple_coord02.png)
367371

368-
372+
5. 有时,您希望您的注释带有一些“偏移点”,不是距离注释点,而是距离某些其他点。 `OffsetFrom`是这种情况下的辅助类。
373+
374+
```py
375+
import matplotlib.pyplot as plt
376+
377+
plt.figure(figsize=(3,2))
378+
ax=plt.axes([0.1, 0.1, 0.8, 0.7])
379+
an1 = ax.annotate("Test 1", xy=(0.5, 0.5), xycoords="data",
380+
va="center", ha="center",
381+
bbox=dict(boxstyle="round", fc="w"))
382+
383+
from matplotlib.text import OffsetFrom
384+
offset_from = OffsetFrom(an1, (0.5, 0))
385+
an2 = ax.annotate("Test 2", xy=(0.1, 0.1), xycoords="data",
386+
xytext=(0, -10), textcoords=offset_from,
387+
# xytext is offset points from "xy=(0.5, 0), xycoords=an1"
388+
va="top", ha="center",
389+
bbox=dict(boxstyle="round", fc="w"),
390+
arrowprops=dict(arrowstyle="->"))
391+
plt.show()
392+
```
393+
394+
![](http://matplotlib.org/_images/annotate_simple_coord03.png)
395+
396+
你可以参考这个链接:[`pylab_examples example code: annotation_demo3.py.`](http://matplotlib.org/examples/pylab_examples/annotation_demo3.html#pylab-examples-annotation-demo3)。
397+
398+
### 使用`ConnectorPatch`
399+
400+
`ConnectorPatch`类似于没有文本的标注。 虽然在大多数情况下建议使用标注函数,但是当您想在不同的轴上连接点时,`ConnectorPatch`很有用。
401+
402+
403+
```py
404+
from matplotlib.patches import ConnectionPatch
405+
xy = (0.2, 0.2)
406+
con = ConnectionPatch(xyA=xy, xyB=xy, coordsA="data", coordsB="data",
407+
axesA=ax1, axesB=ax2)
408+
ax2.add_artist(con)
409+
```
410+
411+
上述代码连接了`ax1`中数据坐标的`xy`点,与`ax2`中数据坐标的`xy`点。这是个简单的例子。
412+
413+
[源代码](http://matplotlib.org/users/plotting/examples/connect_simple01.py)
414+
415+
![](http://matplotlib.org/_images/connect_simple01.png)
416+
417+
虽然`ConnectorPatch`实例可以添加到任何轴,但您可能需要将其添加到绘图顺序中最新的轴,以防止与其他轴重叠。
418+
419+
## 高级话题
420+
421+
### 轴域之间的缩放效果
422+
423+
`mpl_toolkits.axes_grid.inset_locator`定义了一些补丁类,用于互连两个轴域。 理解代码需要一些 mpl 转换如何工作的知识。 但是,利用它的方式很直接。
424+
425+
[源代码](http://matplotlib.org/mpl_examples/pylab_examples/axes_zoom_effect.py)
426+
427+
![](http://matplotlib.org/_images/axes_zoom_effect1.png)
428+
429+
### 定义自定义盒样式
430+
431+
你可以使用自定义盒样式,`boxstyle`的值可以为如下形式的可调用对象:
432+
433+
```py
434+
def __call__(self, x0, y0, width, height, mutation_size,
435+
aspect_ratio=1.):
436+
"""
437+
Given the location and size of the box, return the path of
438+
the box around it.
439+
440+
- *x0*, *y0*, *width*, *height* : location and size of the box
441+
- *mutation_size* : a reference scale for the mutation.
442+
- *aspect_ratio* : aspect-ratio for the mutation.
443+
"""
444+
path = ...
445+
return path
446+
```
447+
448+
这里是个复杂的例子:
449+
450+
[源代码](http://matplotlib.org/users/plotting/examples/custom_boxstyle01.py)
451+
452+
![](http://matplotlib.org/_images/custom_boxstyle01.png)
453+
454+
但是,推荐你从`matplotlib.patches.BoxStyle._Base`派生,像这样:
455+
456+
```py
457+
from matplotlib.path import Path
458+
from matplotlib.patches import BoxStyle
459+
import matplotlib.pyplot as plt
460+
461+
# we may derive from matplotlib.patches.BoxStyle._Base class.
462+
# You need to override transmute method in this case.
463+
464+
class MyStyle(BoxStyle._Base):
465+
"""
466+
A simple box.
467+
"""
468+
469+
def __init__(self, pad=0.3):
470+
"""
471+
The arguments need to be floating numbers and need to have
472+
default values.
473+
474+
*pad*
475+
amount of padding
476+
"""
477+
478+
self.pad = pad
479+
super(MyStyle, self).__init__()
480+
481+
def transmute(self, x0, y0, width, height, mutation_size):
482+
"""
483+
Given the location and size of the box, return the path of
484+
the box around it.
485+
486+
- *x0*, *y0*, *width*, *height* : location and size of the box
487+
- *mutation_size* : a reference scale for the mutation.
488+
489+
Often, the *mutation_size* is the font size of the text.
490+
You don't need to worry about the rotation as it is
491+
automatically taken care of.
492+
"""
493+
494+
# padding
495+
pad = mutation_size * self.pad
496+
497+
# width and height with padding added.
498+
width, height = width + 2.*pad, \
499+
height + 2.*pad,
500+
501+
# boundary of the padded box
502+
x0, y0 = x0-pad, y0-pad,
503+
x1, y1 = x0+width, y0 + height
504+
505+
cp = [(x0, y0),
506+
(x1, y0), (x1, y1), (x0, y1),
507+
(x0-pad, (y0+y1)/2.), (x0, y0),
508+
(x0, y0)]
509+
510+
com = [Path.MOVETO,
511+
Path.LINETO, Path.LINETO, Path.LINETO,
512+
Path.LINETO, Path.LINETO,
513+
Path.CLOSEPOLY]
514+
515+
path = Path(cp, com)
516+
517+
return path
518+
519+
520+
# register the custom style
521+
BoxStyle._style_list["angled"] = MyStyle
522+
523+
plt.figure(1, figsize=(3,3))
524+
ax = plt.subplot(111)
525+
ax.text(0.5, 0.5, "Test", size=30, va="center", ha="center", rotation=30,
526+
bbox=dict(boxstyle="angled,pad=0.5", alpha=0.2))
527+
528+
del BoxStyle._style_list["angled"]
529+
530+
plt.show()
531+
```
532+
533+
[源代码](http://matplotlib.org/users/plotting/examples/custom_boxstyle02.py)
534+
535+
![](http://matplotlib.org/_images/custom_boxstyle02.png)
536+
537+
与之类似,您可以定义一个自定义的`ConnectionStyle`和一个自定义的`ArrowStyle`。 请参阅`lib/matplotlib/patches.py`的源代码,并查看每个样式类是如何定义的。

0 commit comments

Comments
 (0)