Skip to content

Unable to change cap style on annotation. #4332

Closed

Description

@breedlun

As shown below, I have tried to change the arrow cap style on an annotation using rcParams, changing the cap style on the arrow path directly, and specifying the cap style in the arrowprops dictionary. None of them worked.

import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['lines.solid_capstyle'] = 'butt'

fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])

anno = ax.annotate('5 mm', xytext = [0.5, 0.25], textcoords = 'axes fraction', \
            xy = [0.0, 0.25], xycoords = 'axes fraction', clip_on = False, \
            arrowprops=dict(arrowstyle='-', connectionstyle='arc3, rad=0.0', \
            fc='none', shrinkA = 10, shrinkB = 0.0, \
            linewidth = 8))
anno.arrow_patch.set_capstyle('butt')

anno = ax.annotate('5 mm', xytext = [0.5, 0.5], textcoords = 'axes fraction', \
            xy = [0.0, 0.5], xycoords = 'axes fraction', clip_on = False, \
            arrowprops=dict(arrowstyle='-', connectionstyle='arc3, rad=0.0', \
            fc='none', shrinkA = 10, shrinkB = 0.0, \
            linewidth = 8, capstyle = 'butt'))

fig.canvas.draw()
fig.savefig('scale_bar_issue.png')

anno = ax.annotate('5 mm', xytext = [0.5, 0.75], textcoords = 'axes fraction', \
            xy = [0.0, 0.75], xycoords = 'axes fraction', clip_on = False, \
            arrowprops=dict(arrowstyle='-', connectionstyle='arc3, rad=0.0', \
            fc='none', shrinkA = 10, shrinkB = 0.0, \
            linewidth = 8, solid_capstyle = 'butt'))

Right after the figure.canvas.draw() the window looks like this

scale_bar_issue

Clearly, I still have rounded caps on my lines (arrows).

Furthermore, in the last attempt, I specified solid_capstyle = 'butt' in the arrowprops dictionary, and it failed with the following traceback

File "/Users/breedlu/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 651, in annotate
a = mtext.Annotation(*args, **kwargs)
File "/Users/breedlu/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/text.py", line 1812, in __init__
**arrowprops)
File "/Users/breedlu/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/patches.py", line 3888, in __init__
Patch.__init__(self, **kwargs)
File "/Users/breedlu/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/patches.py", line 118, in __init__
self.update(kwargs)
File "/Users/breedlu/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/artist.py", line 757, in update
raise AttributeError('Unknown property %s' % k)
AttributeError: Unknown property solid_capstyle

It appears that the arrowprops dictionary can include patch properties, such as capstyle, but not line properties, such as solid_capstyle. This seems to be counter to the documentation here, where it says that "arrowprops, if not None, is a dictionary of line properties (see matplotlib.lines.Line2D) for the arrow that connects annotation to the point."

Activity

  1. added
    API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …
    on Apr 13, 2015
  2. added this to the milestone on Apr 13, 2015
  3. modified the milestones: , on Jul 17, 2015
  4. modified the milestones: , on Oct 26, 2015
  5. added a commit that references this issue on Oct 29, 2015
    0167f9a
  6. self-assigned this
    on Oct 29, 2015
  7. added a commit that references this issue on Oct 29, 2015
    a8d8877
  8. tacaswell commented on Oct 29, 2015

    @tacaswell
    Member

    mis-aimed commit message

  9. modified the milestones: , on Mar 21, 2016
  10. added
    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
    on Mar 21, 2016
  11. mtiberti commented on Jun 14, 2017

    @mtiberti

    Any news or workaround on this? I'm facing this same problem.

  12. breedlun commented on Jun 14, 2017

    @breedlun
    ContributorAuthor

    No workarounds I know of, other than to use a 2D line instead of an annotation.

  13. modified the milestones: , on Oct 3, 2017
  14. Dhrumil0 commented on Mar 14, 2018

    @Dhrumil0

    This no longer appears to be a bug and can probably be closed. Maybe there were changes to the API since this was posted. The arrowprops dictionary only takes patch properties so line properties such as solid_capstyle will not work.

    @breedlun's code snippets are also using the arrowstyle property in arrowprops dictionary and this is why the capstyle property is being ignored. According to the API, if arrowprops contains arrowstyle, it will ignore patch properties and this is exactly the case. If arrowstyle is not in arrowprops, the capstyle is changed correctly.

  15. mpetroff commented on Oct 17, 2019

    @mpetroff
    Contributor

    It looks like this was fixed in #13647, which is targeted for v3.2.

    Specifically, setting capstyle and joinstyle in arrowprops now works.

  16. jklymak commented on Oct 17, 2019

    @jklymak
    Member

    Thanks for checking!

  17. removed this from the future releases milestone on Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions