Skip to content

[WiP] Brace arrowstyle - #6117

Closed
afvincent wants to merge 7 commits into
matplotlib:masterfrom
afvincent:Brace_arrowstyle
Closed

afvincent wants to merge 7 commits into
matplotlib:masterfrom
afvincent:Brace_arrowstyle

Conversation

@afvincent

@afvincent afvincent commented Mar 6, 2016 •

Copy link
Copy Markdown
Contributor

[2017-03-08] Edit: Removing for the need_review flag for the moment as the code is actually not totally finished. I would qualify the PR of “WiP”, eventhough I am not currently actively working on it… This PR (or a “rebirth” version) might be part of the arrow plotting overhaul that @dstansby is willing to initiate :).

Generalities

PR that was mentioned in #6112 , about adding a new arrowstyle with curly brackets for head or tail. It is mainly targeted for use with FancyArrowPatch (as the other arrowstyles), with the style names "}-" for BraceA, "-{" for BraceB, and "}-{" for BraceAB. I implemented it for myself as I missed a few times such a feature and had to use Inkscape to add this kind of annotation to my figures.

Since #6112 , I finally got working what I really wanted: arrowstyle "<-{", aka CurveABraceB. It seems to do what I want (an arrow with a normal head and a brace at the other side) but unfortunately, I have difficulties to understand how super() works (especially with multpile inheritance), so the review of __init__ in CurveABraceB is liketo to end up with changes. (See the last ~100 lines of the commit)

Furthermore, supplementary variations with straight braces are accessible by initializing the instance with one or two args to "straight". However, I didn't set dedicated stylenames for these versions, as the list of stylenames is already slightly long.

Here are some examples of what is currently achievable with these new arrowstyles:
test_brace_arrowstyles

Interrogations (some details are in the commit comments)

The computation of the brace (with lines and quadratic Bezier curves) is done in the method _get_brace. I tried to explain the construction of the brace as clearly as I could: the docstring may appear a bit long... Besides , I internally use a parameter pad that one may want to expose as an argument.

I have used a @staticmethod decorator for _get_brace, because I may implement a new patch BracePatch that would also use it. For example, such a patch might be used to annotate a range of data. Unfortunately, I don't know when I will have time to try this.

It is planned to update the relevant unit test once review will have been done.

Appendix

The code used to generate to previous figure, adapted from the documentation:

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt

plt.ion()

#styles = mpatches.ArrowStyle.get_styles()
styles = {"}-": mpatches.ArrowStyle.BraceA(),
          "}-{": mpatches.ArrowStyle.BraceAB(),
          "-{": mpatches.ArrowStyle.BraceB(),
          "<-{": mpatches.ArrowStyle.CurveABraceB(),
          "<|-{": mpatches.ArrowStyle._CurveFilledABraceB(),
          # The same arrowstyles, but with "straight braces
          "--straight }-": mpatches.ArrowStyle.BraceA(bracetypeA='straight'),
          "--straight }-{": mpatches.ArrowStyle.BraceAB(bracetypeA='straight',
                                              bracetypeB='straight'),
          "--straight -{": mpatches.ArrowStyle.BraceB(bracetypeB='straight'),
          "--straight <-{": mpatches.ArrowStyle.CurveABraceB(
                                              bracetypeB='straight'),
          "--straight <|-{": mpatches.ArrowStyle._CurveFilledABraceB(
                                              bracetypeB='straight')
        }

ncol = 2
nrow = (len(styles) + 1) // ncol
figheight = (nrow + 0.5)
fig1 = plt.figure("Test_brace_arrowstyles", (2*4.*ncol/1.5, 2*figheight/1.5))
fig1.clf()
fontsize = 2*0.2 * 70

ax = fig1.add_axes([0, 0, 1, 1], frameon=False, aspect=1.)
ax.set_xlim(0, 4*ncol)
ax.set_ylim(0, figheight)

def to_texstring(s):
    s = s.replace("<", r"$<$")
    s = s.replace(">", r"$>$")
    s = s.replace("|", r"$|$")
    s = s.replace("{", r"$\{$")
    s = s.replace("}", r"$\}$")
    s = s.replace("--", "")  # trick, used to sort the 'straight' versions
    return s

# Sort with reverse = True to force straight into the second column
for i, (stylename, styleclass) in enumerate(sorted(styles.items(),
                                                   reverse=True)):

    x = 3.2 + (i//nrow)*4
    y = (figheight - 0.7 - i % nrow)  # /figheight
    p = mpatches.Circle((x, y), 0.2, fc="w", lw=1) # WEIRD: Had to add lw=1
    ax.add_patch(p)

    ax.annotate(to_texstring(stylename), (x, y),
                (x - 1.2, y),
                #xycoords="figure fraction", textcoords="figure fraction",
                ha="right", va="center",
                size=fontsize,
                arrowprops=dict(arrowstyle=styleclass,
                                patchB=p,
                                shrinkA=2*5,  # doubled to avoid collisions
                                shrinkB=2*5,  # with the textbox or the circle
                                fc="CornFlowerBlue", ec="k",
                                connectionstyle="arc3,rad=-0.05",
                                ),
                bbox=dict(boxstyle="square", fc="w"))

ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)

plt.draw()
plt.show()

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Mar 6, 2016
Comment thread lib/matplotlib/patches.py Outdated
'straight': a simplified }, with the 7 control points p0...p6
simply linked with straight lines.

Details of the basic design:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put two colons at the end of this line so the ASCII art gets rendered correctly in the docs.

@afvincent afvincent changed the title Brace arrowstyle [WiP] Brace arrowstyle Mar 12, 2017
@tacaswell tacaswell modified the milestones: 2.2 (next next feature release), 2.1 (next point release) Aug 26, 2017
@tacaswell

Copy link
Copy Markdown
Member

@afvincent is this still WIP?

@afvincent

Copy link
Copy Markdown
Contributor Author

@tacaswell TBH, I am not currently working actively on it at the moment. So this PR has kind of stalled. I am equally fine tagging it for a release in a far future or even simply close it if your point is tidying the open PRs.

@afvincent

Copy link
Copy Markdown
Contributor Author

Closing this PR as I did not find time to work on it at all since my last comment and unfortunately this situation is unlikely to evolve.

@afvincent afvincent closed this Nov 24, 2019
@story645 story645 removed this from the future releases milestone Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants