From 9c596abef57d5bbbb3729d9e52860c93ac5eb0cb Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 28 Jun 2021 12:59:23 +0200 Subject: [PATCH] Show box/arrowstyle parameters in reference examples. This will allow later removing the table of values in the annotation tutorial. Also, draw things each in their axes and in axes coordinates, to simplify the code. --- .../shapes_and_collections/fancybox_demo.py | 33 ++++++++---- .../fancyarrow_demo.py | 53 ++++++++++--------- 2 files changed, 50 insertions(+), 36 deletions(-) diff --git a/examples/shapes_and_collections/fancybox_demo.py b/examples/shapes_and_collections/fancybox_demo.py index ba80979ab0a2..de3263612d2a 100644 --- a/examples/shapes_and_collections/fancybox_demo.py +++ b/examples/shapes_and_collections/fancybox_demo.py @@ -6,6 +6,8 @@ The following examples show how to plot boxes with different visual properties. """ +import inspect + import matplotlib.pyplot as plt import matplotlib.transforms as mtransforms import matplotlib.patches as mpatch @@ -15,17 +17,26 @@ # First we'll show some sample boxes with fancybox. styles = mpatch.BoxStyle.get_styles() -spacing = 1.2 - -figheight = (spacing * len(styles) + .5) -fig = plt.figure(figsize=(4 / 1.5, figheight / 1.5)) -fontsize = 0.3 * 72 - -for i, stylename in enumerate(styles): - fig.text(0.5, (spacing * (len(styles) - i) - 0.5) / figheight, stylename, - ha="center", - size=fontsize, - bbox=dict(boxstyle=stylename, fc="w", ec="k")) +ncol = 2 +nrow = (len(styles) + 1) // ncol +axs = (plt.figure(figsize=(3 * ncol, 1 + nrow)) + .add_gridspec(1 + nrow, ncol, wspace=.5).subplots()) +for ax in axs.flat: + ax.set_axis_off() +for ax in axs[0, :]: + ax.text(.2, .5, "boxstyle", + transform=ax.transAxes, size="large", color="tab:blue", + horizontalalignment="right", verticalalignment="center") + ax.text(.4, .5, "default parameters", + transform=ax.transAxes, + horizontalalignment="left", verticalalignment="center") +for ax, (stylename, stylecls) in zip(axs[1:, :].T.flat, styles.items()): + ax.text(.2, .5, stylename, bbox=dict(boxstyle=stylename, fc="w", ec="k"), + transform=ax.transAxes, size="large", color="tab:blue", + horizontalalignment="right", verticalalignment="center") + ax.text(.4, .5, str(inspect.signature(stylecls))[1:-1].replace(", ", "\n"), + transform=ax.transAxes, + horizontalalignment="left", verticalalignment="center") ############################################################################### diff --git a/examples/text_labels_and_annotations/fancyarrow_demo.py b/examples/text_labels_and_annotations/fancyarrow_demo.py index fbaca019fdf8..546384a0f4d0 100644 --- a/examples/text_labels_and_annotations/fancyarrow_demo.py +++ b/examples/text_labels_and_annotations/fancyarrow_demo.py @@ -6,38 +6,41 @@ Overview of the arrow styles available in `~.Axes.annotate`. """ +import inspect + import matplotlib.patches as mpatches import matplotlib.pyplot as plt styles = mpatches.ArrowStyle.get_styles() - ncol = 2 nrow = (len(styles) + 1) // ncol -figheight = (nrow + 0.5) -fig = plt.figure(figsize=(4 * ncol / 1.5, figheight / 1.5)) -fontsize = 0.2 * 70 - -ax = fig.add_axes([0, 0, 1, 1], frameon=False) -ax.set(xlim=(0, 4 * ncol), ylim=(0, figheight)) - -for i, (stylename, styleclass) in enumerate(styles.items()): - x = 3.2 + (i // nrow) * 4 - y = (figheight - 0.7 - i % nrow) # /figheight - p = mpatches.Circle((x, y), 0.2) - ax.add_patch(p) - - ax.annotate(stylename, (x, y), - (x - 1.2, y), - ha="right", va="center", - size=fontsize, - arrowprops=dict(arrowstyle=stylename, - patchB=p, - shrinkA=5, - shrinkB=5, - fc="k", ec="k", - connectionstyle="arc3,rad=-0.05", - ), +axs = (plt.figure(figsize=(4 * ncol, 1 + nrow)) + .add_gridspec(1 + nrow, ncol, + wspace=.5, left=.1, right=.9, bottom=0, top=1).subplots()) +for ax in axs.flat: + ax.set_axis_off() +for ax in axs[0, :]: + ax.text(0, .5, "arrowstyle", + transform=ax.transAxes, size="large", color="tab:blue", + horizontalalignment="center", verticalalignment="center") + ax.text(.5, .5, "default parameters", + transform=ax.transAxes, + horizontalalignment="left", verticalalignment="center") +for ax, (stylename, stylecls) in zip(axs[1:, :].T.flat, styles.items()): + l, = ax.plot(.35, .5, "ok", transform=ax.transAxes) + ax.annotate(stylename, (.35, .5), (0, .5), + xycoords="axes fraction", textcoords="axes fraction", + size="large", color="tab:blue", + horizontalalignment="center", verticalalignment="center", + arrowprops=dict( + arrowstyle=stylename, connectionstyle="arc3,rad=-0.05", + color="k", shrinkA=5, shrinkB=5, patchB=l, + ), bbox=dict(boxstyle="square", fc="w")) + ax.text(.5, .5, + str(inspect.signature(stylecls))[1:-1].replace(", ", "\n"), + transform=ax.transAxes, + horizontalalignment="left", verticalalignment="center") plt.show()