From 3cdc31124f9f786ce96baee627e981dcd2ca46c8 Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Fri, 15 Sep 2017 12:26:33 -0500 Subject: [PATCH 1/4] Change the way expansion arrows are handled in plot visualization. * Use annotate() to draw the arrows. This has a higher order API than arrow() that makes it easier to produce nice arrows. * Try to be smarter about avoiding overlapping arrows with expansion labels. --- sumpy/toys.py | 78 +++++++++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/sumpy/toys.py b/sumpy/toys.py index 4f11fcbf..82138762 100644 --- a/sumpy/toys.py +++ b/sumpy/toys.py @@ -338,8 +338,9 @@ class ExpansionPotentialSource(PotentialSource): .. attribute:: text_kwargs - Passed to :method:`matplotlib.axes.Axes.text`. Used for customizing the - expansion label. Just for visualization, purely advisory. + Passed to :method:`matplotlib.pyplot.annotate`. Used for customizing the + expansion label. Changing the label text is supported by passing the + kwarg *s*. Just for visualization, purely advisory. """ def __init__(self, toy_ctx, center, rscale, order, coeffs, derived_from, radius=None, expn_style=None, text_kwargs=None): @@ -526,28 +527,28 @@ def draw_point(loc, **kwargs): plt.plot(*loc, marker="o", **kwargs) -def draw_arrow(from_pt, to_pt, shorten=0, **kwargs): - import matplotlib.pyplot as plt - dist = to_pt - from_pt +def draw_annotation(from_pt, to_pt, label, arrowprops={}, **kwargs): + """ + :arg from_pt: Tail of arrow + :arg to_pt: Head of arrow + :arg label: Annotation label + :arg arrowprops: Passed to arrowprops + :arg kwargs: Passed to annotate + """ - from_pt = from_pt + shorten*dist - dist = dist - 2*shorten*dist - plt.arrow(from_pt[0], from_pt[1], dist[0], dist[1], **kwargs) + import matplotlib.pyplot as plt + my_arrowprops = dict( + facecolor="black", + edgecolor="black", + arrowstyle="->") -def draw_annotation(from_pt, to_pt, label, **kwargs): - import matplotlib.pyplot as plt - color = kwargs.setdefault("color", "white") + my_arrowprops.update(arrowprops) - arrowprops = dict( - facecolor=color, - edgecolor=color, - shrink=0.05, - width=1, - headwidth=5) + print("KWARGS", kwargs) plt.gca().annotate(label, xy=from_pt, xytext=to_pt, - arrowprops=arrowprops, **kwargs) + arrowprops=my_arrowprops, **kwargs) class SchematicVisitor(object): @@ -581,26 +582,37 @@ class SchematicVisitor(object): else: raise ValueError("unknown expn_style: %s" % self.expn_style) + if psource.derived_from is None: + return + + # Draw an annotation of the form + # + # ------> M + text_kwargs = dict( - x=psource.center[0], - y=psource.center[1], - s=type(psource).__name__[0], verticalalignment="center", horizontalalignment="center") - if psource.text_kwargs is not None: - text_kwargs.update(psource.text_kwargs) + label = type(psource).__name__[0] - import matplotlib.pyplot as plt - plt.gca().text(**text_kwargs) - - if psource.derived_from is not None: - xmin, xmax = plt.xlim() - plt_width = xmax - xmin - draw_arrow(psource.derived_from.center, psource.center, shorten=0.1, - facecolor="black", length_includes_head=True, - width=0.0005 * plt_width) - self.rec(psource.derived_from) + if psource.text_kwargs is not None: + psource_text_kwargs_copy = psource.text_kwargs.copy() + label = psource_text_kwargs_copy.pop('s', label) + text_kwargs.update(psource_text_kwargs_copy) + + shrinkA = 0 + if isinstance(psource.derived_from, ExpansionPotentialSource): + # Avoid overlapping the tail of the arrow with any expansion labels that + # are present at the tail. + import matplotlib as mpl + font_size = mpl.rcParams['font.size'] + shrinkA = 2/3 * font_size + + arrowprops = dict(shrinkA=shrinkA, arrowstyle="<|-") + + draw_annotation(psource.center, psource.derived_from.center, label, + arrowprops, **text_kwargs) + self.rec(psource.derived_from) visit_localexpansion = visit_multipoleexpansion -- GitLab From 2c08b15ac60f979f1167fe4e22e0672d635202ca Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Fri, 15 Sep 2017 12:32:49 -0500 Subject: [PATCH 2/4] Remove print statement. --- sumpy/toys.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/sumpy/toys.py b/sumpy/toys.py index 82138762..673ef7b3 100644 --- a/sumpy/toys.py +++ b/sumpy/toys.py @@ -545,8 +545,6 @@ def draw_annotation(from_pt, to_pt, label, arrowprops={}, **kwargs): my_arrowprops.update(arrowprops) - print("KWARGS", kwargs) - plt.gca().annotate(label, xy=from_pt, xytext=to_pt, arrowprops=my_arrowprops, **kwargs) -- GitLab From bce10f1c8ba04ce6d6d710064f767a0976d4fcc7 Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Fri, 15 Sep 2017 13:18:14 -0500 Subject: [PATCH 3/4] Placate flake8. --- sumpy/toys.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sumpy/toys.py b/sumpy/toys.py index 673ef7b3..e5f10816 100644 --- a/sumpy/toys.py +++ b/sumpy/toys.py @@ -598,7 +598,7 @@ class SchematicVisitor(object): label = psource_text_kwargs_copy.pop('s', label) text_kwargs.update(psource_text_kwargs_copy) - shrinkA = 0 + shrinkA = 0 # noqa if isinstance(psource.derived_from, ExpansionPotentialSource): # Avoid overlapping the tail of the arrow with any expansion labels that # are present at the tail. @@ -606,7 +606,7 @@ class SchematicVisitor(object): font_size = mpl.rcParams['font.size'] shrinkA = 2/3 * font_size - arrowprops = dict(shrinkA=shrinkA, arrowstyle="<|-") + arrowprops = dict(shrinkA=shrinkA, arrowstyle="<|-") # noqa draw_annotation(psource.center, psource.derived_from.center, label, arrowprops, **text_kwargs) -- GitLab From 54b17412796b1244fc73b8c9428a956cdd28afe9 Mon Sep 17 00:00:00 2001 From: Matt Wala Date: Fri, 15 Sep 2017 13:19:37 -0500 Subject: [PATCH 4/4] Actually placate flake8. --- sumpy/toys.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sumpy/toys.py b/sumpy/toys.py index e5f10816..20ba4e0e 100644 --- a/sumpy/toys.py +++ b/sumpy/toys.py @@ -604,7 +604,7 @@ class SchematicVisitor(object): # are present at the tail. import matplotlib as mpl font_size = mpl.rcParams['font.size'] - shrinkA = 2/3 * font_size + shrinkA = 2/3 * font_size # noqa arrowprops = dict(shrinkA=shrinkA, arrowstyle="<|-") # noqa -- GitLab