From 9bd25b30422e739638545807ad14439c11c1b94e Mon Sep 17 00:00:00 2001 From: wizardforcel <562826179@qq.com> Date: Sat, 28 Jan 2017 22:21:59 +0800 Subject: [PATCH] 4.5 --- 4.5.md | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 170 insertions(+), 1 deletion(-) diff --git a/4.5.md b/4.5.md index 988ec70..7b4b12b 100644 --- a/4.5.md +++ b/4.5.md @@ -29,6 +29,8 @@ ax.set_ylim(-2,2) plt.show() ``` +[源代码](http://matplotlib.org/mpl_examples/pyplots/annotation_basic.py) + ![](http://matplotlib.org/_images/annotation_basic.png) 在该示例中,`xy`(箭头尖端)和`xytext`位置(文本位置)都以数据坐标为单位。 有多种可以选择的其他坐标系 - 你可以使用`xycoords`和`textcoords`以及下列字符串之一(默认为`data`)指定`xy`和`xytext`的坐标系。 @@ -67,6 +69,8 @@ ax.annotate('local max', xy=(3, 1), xycoords='data', 在下面的示例中,`xy`点是原始坐标(`xycoords`默认为`'data'`)。 对于极坐标轴,它在`(theta, radius)`空间中。 此示例中的文本放置在图形小数坐标系中。 `matplotlib.text.Text`关键字`args`,例如`horizontalalignment`,`verticalalignment`和`fontsize`,从`annotate`传给`Text`实例。 +[源代码](http://matplotlib.org/mpl_examples/pyplots/annotation_polar.py) + ![](http://matplotlib.org/_images/annotation_polar.png) 注释(包括花式箭头)的所有高上大的内容的更多信息,请参阅[高级标注](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 ![](http://matplotlib.org/_images/annotate_simple_coord02.png) - \ No newline at end of file +5. 有时,您希望您的注释带有一些“偏移点”,不是距离注释点,而是距离某些其他点。 `OffsetFrom`是这种情况下的辅助类。 + + ```py + import matplotlib.pyplot as plt + + plt.figure(figsize=(3,2)) + ax=plt.axes([0.1, 0.1, 0.8, 0.7]) + an1 = ax.annotate("Test 1", xy=(0.5, 0.5), xycoords="data", + va="center", ha="center", + bbox=dict(boxstyle="round", fc="w")) + + from matplotlib.text import OffsetFrom + offset_from = OffsetFrom(an1, (0.5, 0)) + an2 = ax.annotate("Test 2", xy=(0.1, 0.1), xycoords="data", + xytext=(0, -10), textcoords=offset_from, + # xytext is offset points from "xy=(0.5, 0), xycoords=an1" + va="top", ha="center", + bbox=dict(boxstyle="round", fc="w"), + arrowprops=dict(arrowstyle="->")) + plt.show() + ``` + + ![](http://matplotlib.org/_images/annotate_simple_coord03.png) + + 你可以参考这个链接:[`pylab_examples example code: annotation_demo3.py.`](http://matplotlib.org/examples/pylab_examples/annotation_demo3.html#pylab-examples-annotation-demo3)。 + +### 使用`ConnectorPatch` + +`ConnectorPatch`类似于没有文本的标注。 虽然在大多数情况下建议使用标注函数,但是当您想在不同的轴上连接点时,`ConnectorPatch`很有用。 + + +```py +from matplotlib.patches import ConnectionPatch +xy = (0.2, 0.2) +con = ConnectionPatch(xyA=xy, xyB=xy, coordsA="data", coordsB="data", + axesA=ax1, axesB=ax2) +ax2.add_artist(con) +``` + +上述代码连接了`ax1`中数据坐标的`xy`点,与`ax2`中数据坐标的`xy`点。这是个简单的例子。 + +[源代码](http://matplotlib.org/users/plotting/examples/connect_simple01.py) + +![](http://matplotlib.org/_images/connect_simple01.png) + +虽然`ConnectorPatch`实例可以添加到任何轴,但您可能需要将其添加到绘图顺序中最新的轴,以防止与其他轴重叠。 + +## 高级话题 + +### 轴域之间的缩放效果 + +`mpl_toolkits.axes_grid.inset_locator`定义了一些补丁类,用于互连两个轴域。 理解代码需要一些 mpl 转换如何工作的知识。 但是,利用它的方式很直接。 + +[源代码](http://matplotlib.org/mpl_examples/pylab_examples/axes_zoom_effect.py) + +![](http://matplotlib.org/_images/axes_zoom_effect1.png) + +### 定义自定义盒样式 + +你可以使用自定义盒样式,`boxstyle`的值可以为如下形式的可调用对象: + +```py +def __call__(self, x0, y0, width, height, mutation_size, + aspect_ratio=1.): + """ + Given the location and size of the box, return the path of + the box around it. + + - *x0*, *y0*, *width*, *height* : location and size of the box + - *mutation_size* : a reference scale for the mutation. + - *aspect_ratio* : aspect-ratio for the mutation. + """ + path = ... + return path +``` + +这里是个复杂的例子: + +[源代码](http://matplotlib.org/users/plotting/examples/custom_boxstyle01.py) + +![](http://matplotlib.org/_images/custom_boxstyle01.png) + +但是,推荐你从`matplotlib.patches.BoxStyle._Base`派生,像这样: + +```py +from matplotlib.path import Path +from matplotlib.patches import BoxStyle +import matplotlib.pyplot as plt + +# we may derive from matplotlib.patches.BoxStyle._Base class. +# You need to override transmute method in this case. + +class MyStyle(BoxStyle._Base): + """ + A simple box. + """ + + def __init__(self, pad=0.3): + """ + The arguments need to be floating numbers and need to have + default values. + + *pad* + amount of padding + """ + + self.pad = pad + super(MyStyle, self).__init__() + + def transmute(self, x0, y0, width, height, mutation_size): + """ + Given the location and size of the box, return the path of + the box around it. + + - *x0*, *y0*, *width*, *height* : location and size of the box + - *mutation_size* : a reference scale for the mutation. + + Often, the *mutation_size* is the font size of the text. + You don't need to worry about the rotation as it is + automatically taken care of. + """ + + # padding + pad = mutation_size * self.pad + + # width and height with padding added. + width, height = width + 2.*pad, \ + height + 2.*pad, + + # boundary of the padded box + x0, y0 = x0-pad, y0-pad, + x1, y1 = x0+width, y0 + height + + cp = [(x0, y0), + (x1, y0), (x1, y1), (x0, y1), + (x0-pad, (y0+y1)/2.), (x0, y0), + (x0, y0)] + + com = [Path.MOVETO, + Path.LINETO, Path.LINETO, Path.LINETO, + Path.LINETO, Path.LINETO, + Path.CLOSEPOLY] + + path = Path(cp, com) + + return path + + +# register the custom style +BoxStyle._style_list["angled"] = MyStyle + +plt.figure(1, figsize=(3,3)) +ax = plt.subplot(111) +ax.text(0.5, 0.5, "Test", size=30, va="center", ha="center", rotation=30, + bbox=dict(boxstyle="angled,pad=0.5", alpha=0.2)) + +del BoxStyle._style_list["angled"] + +plt.show() +``` + +[源代码](http://matplotlib.org/users/plotting/examples/custom_boxstyle02.py) + +![](http://matplotlib.org/_images/custom_boxstyle02.png) + +与之类似,您可以定义一个自定义的`ConnectionStyle`和一个自定义的`ArrowStyle`。 请参阅`lib/matplotlib/patches.py`的源代码,并查看每个样式类是如何定义的。 \ No newline at end of file -- GitLab