Title: Add legend handler and artist for FancyArrow by magedhennawy · Pull Request #10688 · matplotlib/matplotlib · GitHub
Open Graph Title: Add legend handler and artist for FancyArrow by magedhennawy · Pull Request #10688 · matplotlib/matplotlib
X Title: Add legend handler and artist for FancyArrow by magedhennawy · Pull Request #10688 · matplotlib/matplotlib
Description: closes #8236 I added new legend handlers for FancyArrowPatch, Annotation artists and HandlerAnnotation. HandlerAnnotation can draw FancyArrowPatch artist in the legend: if the annotation has an arrow, it uses HandlerFancyArrowPatch to draw an arrow Example code: import matplotlib.pylab as plt from matplotlib.patches import FancyArrowPatch from matplotlib.legend_handler import HandlerAnnotation fig, ax = plt.subplots(1) ax.plot([0, 1], [0, 0], label='line1') ax.plot([0, 1], [1, 1], label='line2') ax.annotate("", xy=(0.3,1.0), xytext=(0.3,0.0), arrowprops={'arrowstyle':'<->', 'color':'C7' }, label='distance') ax.legend() plt.show() **************** Added extra features. ****************** after merge with #8292 I added new legend handlers for Text, FancyArrowPatch and Annotation artists (HandlerText, HandlerFancyArrowPatch and HandlerAnnotation). HandlerAnnotation can draw different artists in the legend: if the annotation has no text and no arrow, it draws a white Rectangle if the annotation has only text, it uses HandlerText to draw a text. The text is replaced by 'Aa' if it is longer than one character. (This is configurable by setting rep_str and rep_maxlen.) if the annotation has only an arrow, it uses HandlerFancyArrowPatch to draw an arrow If the annotation has both text and arrow, it uses HandlerTuple to draw both. In HandlerTuple, I added the possibility of different width ratios for the artists. HandlerAnnotation uses a width_ratios=[1,4] (ie. the arrow is 4 times wider than the text). Example code: import matplotlib.pylab as plt from matplotlib.patches import FancyArrowPatch from matplotlib.legend_handler import HandlerAnnotation fig, ax = plt.subplots(1) ax.plot([0, 1], [0, 0], label='line1') ax.plot([0, 1], [1, 1], label='line2') # no text, no arrow ax.annotate("", xy=(0.1,0.5), xytext=(0.1,0.5), label='annotation (empty)') # text, no arrow my_annotation = ax.annotate("X", xy=(0.1,0.5), xytext=(0.1,0.5), color='C2', label='annotation (text, no arrow)') # no text, arrow ax.annotate("", xy=(0.3,1.0), xytext=(0.3,0.0), arrowprops={'arrowstyle':'<->', 'color':'C7' }, label='annotation (no text, arrow)') # text, arrow ax.annotate("Some long string", xy=(0.4,1.0), xytext=(0.35,0.1), arrowprops={'arrowstyle':'->', 'color':'C2' }, color='C1', label='annotation (text + arrow)') # Fancy arrow patch arrpatch = FancyArrowPatch([0.5, 0.8], [0.9, 0.9], arrowstyle='<|-', mutation_scale=20, color='C3', label='arrowpatch') ax.add_patch(arrpatch) # Long text, will not be used in legend ax.text(x=0.1, y=0.1, s='Hello', color='C5', label='text') # Short text, copied in legend ax.text(x=0.1, y=0.2, s='Z', color='C0', label='short text') ax.legend(handler_map={my_annotation: HandlerAnnotation(rep_str='Abcde', rep_maxlen=0)}) fig.show()
Open Graph Description: closes #8236 I added new legend handlers for FancyArrowPatch, Annotation artists and HandlerAnnotation. HandlerAnnotation can draw FancyArrowPatch artist in the legend: if the annotation has an arr...
X Description: closes #8236 I added new legend handlers for FancyArrowPatch, Annotation artists and HandlerAnnotation. HandlerAnnotation can draw FancyArrowPatch artist in the legend: if the annotation has an arr...
Opengraph URL: https://github.com/matplotlib/matplotlib/pull/10688
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:6b62acc6-bd4b-be75-d361-b31ffe7a0ad3 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | AA78:19F60D:151260D:1BD0405:6A51BDA9 |
| html-safe-nonce | 97301c354a3bc640ef851bb8101527fe0f5c95abffb9f4564da5a56faff6117d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQTc4OjE5RjYwRDoxNTEyNjBEOjFCRDA0MDU6NkE1MUJEQTkiLCJ2aXNpdG9yX2lkIjoiMzU2OTkxNzkzNDIxNzMxMzcwNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f44a6298089514b3eb5b06c655f35fc5a759f69f567accdfbdf5cc3f406acd59 |
| hovercard-subject-tag | pull_request:172980958 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/matplotlib/matplotlib/pull/10688/files |
| twitter:image | https://avatars.githubusercontent.com/u/14252814?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/14252814?s=400&v=4 |
| og:image:alt | closes #8236 I added new legend handlers for FancyArrowPatch, Annotation artists and HandlerAnnotation. HandlerAnnotation can draw FancyArrowPatch artist in the legend: if the annotation has an arr... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width