From 594cc867293cc0cc292748594ccba9994dfd396d Mon Sep 17 00:00:00 2001 From: David Stansby Date: Thu, 10 May 2018 15:34:48 +0100 Subject: [PATCH 1/4] Add an arrow tutorial --- tutorials/intermediate/arrow_guide.py | 110 ++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 tutorials/intermediate/arrow_guide.py diff --git a/tutorials/intermediate/arrow_guide.py b/tutorials/intermediate/arrow_guide.py new file mode 100644 index 000000000000..6ab50eefd780 --- /dev/null +++ b/tutorials/intermediate/arrow_guide.py @@ -0,0 +1,110 @@ +""" +=========== +Arrow guide +=========== + +Adding arrows to plots Matplotlib. + +Arrows are often used to annotate plots. This tutorial shows how to plot arrows +that behave differently when the data limits on a plot are changed. In general, +points on a plot can either be fixed in "data space" or "display space". +Something plotted in data space moves when the data limits are altered - an +example would the points in a scatter plot. Something plotted in display space +stays static when data limits are altered - an example would be a figure title +or the axis labels. + +Arrows consist of a head (and possibly a tail) and a stem drawn between a +start point and and end point, called 'anchor points' from now on. +Here we show three use cases for plotting arrows, depending on whether the +head or anchor points need to be fixed in data or display space: + + 1. Head fixed in display space, anchor points fixed in data space + 2. Head and anchor points fixed in display space + 3. Head and anchor points fixed in data space + +Below each use case is presented in turn. +""" +import matplotlib.patches as mpatches +import matplotlib.pyplot as plt +x_tail = 0.1 +y_tail = 0.1 +x_head = 0.9 +y_head = 0.9 +dx = x_head - x_tail +dy = y_head - y_tail + + +############################################################################### +# Head fixed in display space and anchor points fixed in data space +# ----------------------------------------------------------------- +# +# This is useful if you are annotating a plot, and don't want the arrow to +# to change shape or position if you pan or scale the plot. Note that when +# the axis limits change +# +# In this case we use `.patches.FancyArrowPatch` +# +# Note that when the axis limits are changed, the arrow shape stays the same, +# but the anchor points move. + +fig, axs = plt.subplots(nrows=2) +arrow = mpatches.FancyArrowPatch((x_tail, y_tail), (dx, dy), + mutation_scale=100) +axs[0].add_patch(arrow) + +arrow = mpatches.FancyArrowPatch((x_tail, y_tail), (dx, dy), + mutation_scale=100) +axs[1].add_patch(arrow) +axs[1].set_xlim(0, 2) +axs[1].set_ylim(0, 2) + +############################################################################### +# Head and anchor points fixed in display space +# --------------------------------------------- +# +# This is useful if you are annotating a plot, and don't want the arrow to +# to change shape or position if you pan or scale the plot. +# +# In this case we use `.patches.FancyArrowPatch`, and pass the keyword argument +# ``transform=ax.transAxes`` where ``ax`` is the axes we are adding the patch +# to. +# +# Note that when the axis limits are changed, the arrow shape and location +# stays the same. + +fig, axs = plt.subplots(nrows=2) +arrow = mpatches.FancyArrowPatch((x_tail, y_tail), (dx, dy), + mutation_scale=100, + transform=axs[0].transAxes) +axs[0].add_patch(arrow) + +arrow = mpatches.FancyArrowPatch((x_tail, y_tail), (dx, dy), + mutation_scale=100, + transform=axs[1].transAxes) +axs[1].add_patch(arrow) +axs[1].set_xlim(0, 2) +axs[1].set_ylim(0, 2) + + +############################################################################### +# Head and anchor points fixed in data space +# ------------------------------------------ +# +# In this case we use `.patches.Arrow` +# +# Note that when the axis limits are changed, the arrow shape and location +# changes. + +fig, axs = plt.subplots(nrows=2) + +arrow = mpatches.Arrow(x_tail, y_tail, dx, dy) +axs[0].add_patch(arrow) + +arrow = mpatches.Arrow(x_tail, y_tail, dx, dy) +axs[1].add_patch(arrow) +axs[1].set_xlim(0, 2) +axs[1].set_ylim(0, 2) + +############################################################################### + +plt.show() From 3fe6306b236899d41ab80d7575e3dff953267f15 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Thu, 24 May 2018 10:39:55 +0100 Subject: [PATCH 2/4] Small comment fixes --- tutorials/intermediate/arrow_guide.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tutorials/intermediate/arrow_guide.py b/tutorials/intermediate/arrow_guide.py index 6ab50eefd780..64f2850e0a07 100644 --- a/tutorials/intermediate/arrow_guide.py +++ b/tutorials/intermediate/arrow_guide.py @@ -3,7 +3,7 @@ Arrow guide =========== -Adding arrows to plots Matplotlib. +Adding arrow patches to plots. Arrows are often used to annotate plots. This tutorial shows how to plot arrows that behave differently when the data limits on a plot are changed. In general, @@ -18,9 +18,9 @@ Here we show three use cases for plotting arrows, depending on whether the head or anchor points need to be fixed in data or display space: - 1. Head fixed in display space, anchor points fixed in data space - 2. Head and anchor points fixed in display space - 3. Head and anchor points fixed in data space + 1. Head shape fixed in display space, anchor points fixed in data space + 2. Head shape and anchor points fixed in display space + 3. Entire patch fixed in data space Below each use case is presented in turn. """ @@ -35,7 +35,7 @@ ############################################################################### -# Head fixed in display space and anchor points fixed in data space +# Head shape fixed in display space and anchor points fixed in data space # ----------------------------------------------------------------- # # This is useful if you are annotating a plot, and don't want the arrow to @@ -59,8 +59,8 @@ axs[1].set_ylim(0, 2) ############################################################################### -# Head and anchor points fixed in display space -# --------------------------------------------- +# Head shape and anchor points fixed in display space +# --------------------------------------------------- # # This is useful if you are annotating a plot, and don't want the arrow to # to change shape or position if you pan or scale the plot. @@ -87,8 +87,8 @@ ############################################################################### -# Head and anchor points fixed in data space -# ------------------------------------------ +# Head shape and anchor points fixed in data space +# ------------------------------------------------ # # In this case we use `.patches.Arrow` # From 5a3f6c75f922b0fb3ad102a8d4a99549d919759e Mon Sep 17 00:00:00 2001 From: David Stansby Date: Thu, 24 May 2018 10:40:42 +0100 Subject: [PATCH 3/4] Move arrow tutorial to examples --- .../shapes_and_collections}/arrow_guide.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {tutorials/intermediate => examples/shapes_and_collections}/arrow_guide.py (100%) diff --git a/tutorials/intermediate/arrow_guide.py b/examples/shapes_and_collections/arrow_guide.py similarity index 100% rename from tutorials/intermediate/arrow_guide.py rename to examples/shapes_and_collections/arrow_guide.py From f05fdffcde064aadf469763056af6dd3125aedeb Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Wed, 30 May 2018 11:21:33 -0700 Subject: [PATCH 4/4] Small formatting fix to pass CI --- examples/shapes_and_collections/arrow_guide.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/shapes_and_collections/arrow_guide.py b/examples/shapes_and_collections/arrow_guide.py index 64f2850e0a07..1c77d8a7e98b 100644 --- a/examples/shapes_and_collections/arrow_guide.py +++ b/examples/shapes_and_collections/arrow_guide.py @@ -36,7 +36,7 @@ ############################################################################### # Head shape fixed in display space and anchor points fixed in data space -# ----------------------------------------------------------------- +# ----------------------------------------------------------------------- # # This is useful if you are annotating a plot, and don't want the arrow to # to change shape or position if you pan or scale the plot. Note that when