René's URL Explorer Experiment


Title: [Bug]: Text rotation leads to characters being misplaced within their bounding boxes. Attempted solution provided. · Issue #23021 · matplotlib/matplotlib · GitHub

Open Graph Title: [Bug]: Text rotation leads to characters being misplaced within their bounding boxes. Attempted solution provided. · Issue #23021 · matplotlib/matplotlib

X Title: [Bug]: Text rotation leads to characters being misplaced within their bounding boxes. Attempted solution provided. · Issue #23021 · matplotlib/matplotlib

Description: Bug summary plt.text(...) is not rotating text correctly. This becomes clear when looking at their bounding boxes, where you can see that a character's position within the bounding box differs depending on the rotation (and sometimes, th...

Open Graph Description: Bug summary plt.text(...) is not rotating text correctly. This becomes clear when looking at their bounding boxes, where you can see that a character's position within the bounding box differs depe...

X Description: Bug summary plt.text(...) is not rotating text correctly. This becomes clear when looking at their bounding boxes, where you can see that a character's position within the bounding box differs ...

Opengraph URL: https://github.com/matplotlib/matplotlib/issues/23021

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: Text rotation leads to characters being misplaced within their bounding boxes. Attempted solution provided.","articleBody":"### Bug summary\r\n\r\nplt.text(...) is not rotating text correctly. This becomes clear when looking at their bounding boxes, where you can see that a character's position within the bounding box differs depending on the rotation (and sometimes, the text will even exit the box).\r\n\r\n### Code for reproduction\r\n\r\n```python\r\nimport matplotlib.pyplot as plt\r\n\r\ndef plot_rotation_period(y, rotation, r):\r\n    c0 = plt.gca().annotate('.', xy=(0.5, y), xytext=(0.5, y), rotation=rotation, fontsize=80,\r\n                            rotation_mode='anchor', fontfamily='monospace', va='bottom', ha='center',\r\n                            transform_rotates_text = False)\r\n\r\n    bb0 = c0.get_window_extent(renderer=r).transformed(plt.gca().transData.inverted())\r\n    rect0 = plt.Rectangle((bb0.x0, bb0.y0), bb0.width, bb0.height,\r\n                         facecolor=\"C1\", alpha=0.3, zorder=2)\r\n    plt.gca().add_patch(rect0)\r\n\r\nif __name__ == '__main__':\r\n    fig = plt.gcf()\r\n    r = fig.canvas.get_renderer()\r\n    fig.set_size_inches(15, 15)\r\n\r\n    plt.xlim(0, 1)\r\n    plt.ylim(0, 1)\r\n\r\n    plot_rotation_period(0.8, 0, r)\r\n    print('1')\r\n    plot_rotation_period(0.6, 90, r)\r\n    print('2')\r\n    plot_rotation_period(0.4, 180, r)\r\n    print('3')\r\n    plot_rotation_period(0.2, 270, r)\r\n\r\n    plt.plot([0.5, 0.5], [0, 1], linestyle='--')\r\n    plt.show()\r\n\r\n# plt.savefig('example_C.png') # this issue becomes a bit clearer if you do plt.savefig()\r\n```\r\n\r\n\r\n### Actual outcome\r\n\r\nThese examples were created from the code above, which rotates the text 0/90/180/270 degrees and plots its bonding box. When the character is a \".\", it looks like this: https://imgur.com/dBgTRBA\r\n\r\nThe \".\" makes the issue obvious, although this issue appears for all characters, e.g., \"C\": https://imgur.com/a/u3UamAb\r\n\r\nChanging the horizontal/vertical alignment does not fix this. I tried every possible combination, and although it moves the text around, it never does so never in the correct way.\r\n\r\n### Expected outcome\r\n\r\nCorrect outcome for \"C\": https://imgur.com/2XT7wdA\r\nCorrect outcome for \".\": https://imgur.com/Zx22cV1\r\n\r\nHere, we can see that the character is in the same position relative to its bounding box for each possible rotation.\r\n\r\n### Additional information\r\n\r\nI took a stab at trying to fix this. It seems like the issue is related to the [Descent](https://freetype.org/freetype2/docs/glyphs/glyphs-3.html) and/or [Offset/Bearing](https://freetype.org/freetype2/docs/tutorial/step2.html) of the font.\r\n\r\nI took a look at backend_agg.py, where the text is being drawn. My solution may is quite crude and violates [Chesterton's fence](https://en.wiktionary.org/wiki/Chesterton%27s_fence#:~:text=Chesterton's%20fence%20(uncountable),state%20of%20affairs%20is%20understood.). However, I found that if I comment out some lines in backend_agg.RendererAgg.draw_text(...), this fixes the issue ([highlighted here](https://imgur.com/a/u3UamAb)):\r\n\r\n```Python\r\n    def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):\r\n        # docstring inherited\r\n\r\n        if ismath:\r\n            return self.draw_mathtext(gc, x, y, s, prop, angle)\r\n\r\n        flags = get_hinting_flag()\r\n        font = self._get_agg_font(prop)\r\n        if font is None:\r\n            return None\r\n        # We pass '0' for angle here, since it will be rotated (in raster\r\n        # space) in the following call to draw_text_image).\r\n\r\n        font.set_text(s, 0, flags=flags)\r\n        font.draw_glyphs_to_bitmap(\r\n            antialiased=mpl.rcParams['text.antialiased'])\r\n\r\n        d = font.get_descent() / 64.0\r\n        #The descent needs to be adjusted for the angle. [original comment from the package]\r\n        # xo, yo = font.get_bitmap_offset() [I deleted these next lines, commented out here]\r\n        # xo /= 64.0 \r\n        # yo /= 64.0 \r\n        # xd = d * sin(radians(angle)) \r\n        # yd = d * cos(radians(angle))\r\n        # x = round(x + xo + xd)\r\n        # y = round(y + yo + yd)\r\n        self._renderer.draw_text_image(font, x, y + 1, angle, gc)\r\n```\r\n\r\nBy commenting out those lines, I was able to generate those expected outcome pictures above. I tried this for serif fonts and different vertical/horizontal alignments, and it seems to work as expected. Should I do a PR (or would further tests be needed, if so which)?\r\n\r\nI'm having trouble understanding why this code would be here in the first place. Maybe there was some code changed upstream/downstream which already addresses the offset/descent issue, and so the code I pointed out is doing it again, leading to problems?\r\n\r\nI think someone before tried to come up with a solution for a related problem ([see issue](https://github.com/matplotlib/matplotlib/issues/13044)), although their fix was not accepted.\r\n\r\n### Operating system\r\n\r\nWindows 10\r\n\r\n### Matplotlib Version\r\n\r\n3.4.3\r\n\r\n### Matplotlib Backend\r\n\r\nQt5Agg\r\n\r\n### Python version\r\n\r\n3.8.12\r\n\r\n### Jupyter version\r\n\r\n_No response_\r\n\r\n### Installation\r\n\r\nconda","author":{"url":"https://github.com/paulcbogdan","@type":"Person","name":"paulcbogdan"},"datePublished":"2022-05-09T17:51:20.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/23021/matplotlib/issues/23021"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:640e63b2-5193-76e9-c9cf-c3db7618a5fd
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA30C:69D3C:279B999:34F07DF:6A52180C
html-safe-nonce60e1904b19bd23d6f598fb1fb7480ed1a5bab833a0765d5fa7129457b0f80d7e
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMzBDOjY5RDNDOjI3OUI5OTk6MzRGMDdERjo2QTUyMTgwQyIsInZpc2l0b3JfaWQiOiI4NzQyNTE1MjEzOTM5NjQ0NDI4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmac9cb282310f0349a32691efec1d6182581f20f49bcb86967ea08e54a0556d2238
hovercard-subject-tagissue:1230054485
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/23021/issue_layout
twitter:imagehttps://opengraph.githubassets.com/9a56e57a9d68a739da1a780a32436d381caafca5b022f1c244dbdfa27e06cd40/matplotlib/matplotlib/issues/23021
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/9a56e57a9d68a739da1a780a32436d381caafca5b022f1c244dbdfa27e06cd40/matplotlib/matplotlib/issues/23021
og:image:altBug summary plt.text(...) is not rotating text correctly. This becomes clear when looking at their bounding boxes, where you can see that a character's position within the bounding box differs depe...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamepaulcbogdan
hostnamegithub.com
expected-hostnamegithub.com
Noneb9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb
turbo-cache-controlno-preview
go-importgithub.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git
octolytics-dimension-user_id215947
octolytics-dimension-user_loginmatplotlib
octolytics-dimension-repository_id1385122
octolytics-dimension-repository_nwomatplotlib/matplotlib
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id1385122
octolytics-dimension-repository_network_root_nwomatplotlib/matplotlib
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release7aed05249554b889eb33d002851a973eebcc7e91
ui-targetcanary-1
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/matplotlib/matplotlib/issues/23021#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmatplotlib%2Fmatplotlib%2Fissues%2F23021
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmatplotlib%2Fmatplotlib%2Fissues%2F23021
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=matplotlib%2Fmatplotlib
Reloadhttps://github.com/matplotlib/matplotlib/issues/23021
Reloadhttps://github.com/matplotlib/matplotlib/issues/23021
Reloadhttps://github.com/matplotlib/matplotlib/issues/23021
Please reload this pagehttps://github.com/matplotlib/matplotlib/issues/23021
matplotlib https://github.com/matplotlib
matplotlibhttps://github.com/matplotlib/matplotlib
Please reload this pagehttps://github.com/matplotlib/matplotlib/issues/23021
Notifications https://github.com/login?return_to=%2Fmatplotlib%2Fmatplotlib
Fork 8.4k https://github.com/login?return_to=%2Fmatplotlib%2Fmatplotlib
Star 23k https://github.com/login?return_to=%2Fmatplotlib%2Fmatplotlib
Code https://github.com/matplotlib/matplotlib
Issues 1.1k https://github.com/matplotlib/matplotlib/issues
Pull requests 409 https://github.com/matplotlib/matplotlib/pulls
Actions https://github.com/matplotlib/matplotlib/actions
Projects https://github.com/matplotlib/matplotlib/projects
Wiki https://github.com/matplotlib/matplotlib/wiki
Security and quality 0 https://github.com/matplotlib/matplotlib/security
Insights https://github.com/matplotlib/matplotlib/pulse
Code https://github.com/matplotlib/matplotlib
Issues https://github.com/matplotlib/matplotlib/issues
Pull requests https://github.com/matplotlib/matplotlib/pulls
Actions https://github.com/matplotlib/matplotlib/actions
Projects https://github.com/matplotlib/matplotlib/projects
Wiki https://github.com/matplotlib/matplotlib/wiki
Security and quality https://github.com/matplotlib/matplotlib/security
Insights https://github.com/matplotlib/matplotlib/pulse
#13044https://github.com/matplotlib/matplotlib/issues/13044
#29199https://github.com/matplotlib/matplotlib/pull/29199
#13044https://github.com/matplotlib/matplotlib/issues/13044
[Bug]: Text rotation leads to characters being misplaced within their bounding boxes. Attempted solution provided.https://github.com/matplotlib/matplotlib/issues/23021#top
#29199https://github.com/matplotlib/matplotlib/pull/29199
status: duplicatehttps://github.com/matplotlib/matplotlib/issues?q=state%3Aopen%20label%3A%22status%3A%20duplicate%22
topic: texthttps://github.com/matplotlib/matplotlib/issues?q=state%3Aopen%20label%3A%22topic%3A%20text%22
v3.11.0https://github.com/matplotlib/matplotlib/milestone/96
https://github.com/paulcbogdan
paulcbogdanhttps://github.com/paulcbogdan
on May 9, 2022https://github.com/matplotlib/matplotlib/issues/23021#issue-1230054485
https://imgur.com/dBgTRBAhttps://imgur.com/dBgTRBA
https://imgur.com/a/u3UamAbhttps://imgur.com/a/u3UamAb
https://imgur.com/2XT7wdAhttps://imgur.com/2XT7wdA
https://imgur.com/Zx22cV1https://imgur.com/Zx22cV1
Descenthttps://freetype.org/freetype2/docs/glyphs/glyphs-3.html
Offset/Bearinghttps://freetype.org/freetype2/docs/tutorial/step2.html
Chesterton's fencehttps://en.wiktionary.org/wiki/Chesterton%27s_fence#:~:text=Chesterton's%20fence%20(uncountable),state%20of%20affairs%20is%20understood.
highlighted herehttps://imgur.com/a/u3UamAb
see issuehttps://github.com/matplotlib/matplotlib/issues/13044
status: duplicatehttps://github.com/matplotlib/matplotlib/issues?q=state%3Aopen%20label%3A%22status%3A%20duplicate%22
topic: texthttps://github.com/matplotlib/matplotlib/issues?q=state%3Aopen%20label%3A%22topic%3A%20text%22
v3.11.0https://github.com/matplotlib/matplotlib/milestone/96
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.