Title: Matplotlib chooses the wrong font for unrecognized weights · Issue #8550 · matplotlib/matplotlib · GitHub
Open Graph Title: Matplotlib chooses the wrong font for unrecognized weights · Issue #8550 · matplotlib/matplotlib
X Title: Matplotlib chooses the wrong font for unrecognized weights · Issue #8550 · matplotlib/matplotlib
Description: Bug report Bug summary With a lot of the modern fonts containing exotic font weights like “Thin” or “Ultra bold”, matplotlib will sometimes pick the wrong font weight because it assumes that any font weight that it doesn’t recognize is e...
Open Graph Description: Bug report Bug summary With a lot of the modern fonts containing exotic font weights like “Thin” or “Ultra bold”, matplotlib will sometimes pick the wrong font weight because it assumes that any fo...
X Description: Bug report Bug summary With a lot of the modern fonts containing exotic font weights like “Thin” or “Ultra bold”, matplotlib will sometimes pick the wrong font weight because it assumes that any fo...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/8550
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Matplotlib chooses the wrong font for unrecognized weights","articleBody":"### Bug report\r\n\r\n**Bug summary**\r\n\r\nWith a lot of the modern fonts containing exotic font weights like “Thin” or “Ultra bold”, matplotlib will sometimes pick the wrong font weight because it assumes that any font weight that it doesn’t recognize is either normal (400) or bold (700), even though there is a “Regular” available. Ultimately which one matplotlib picks *seems* to be actually kind of random, probably depending on the order in which the fonts appear in the font cache.\r\n\r\nThis is what the [logic in `font_manager.py`](https://github.com/matplotlib/matplotlib/blob/734c09f164819854a13d77b128da283952eb08d4/lib/matplotlib/font_manager.py#L438-L443) looks like:\r\n\r\n```py\r\nweight = next((w for w in weight_dict if sfnt4.find(w) \u003e= 0), None)\r\nif not weight:\r\n if font.style_flags \u0026 ft2font.BOLD:\r\n weight = 700\r\n else:\r\n weight = 400\r\n```\r\n\r\n`sfnt4` appears to be a string that contains the full name of the font, including the weight. The problem with this approach is that if it doesn’t recognize the weight, it will simply assume the weight is either 400 or 700. [A “Thin” font that was mistakenly labeled 400 might get picked over a “Regular” font.](http://stackoverflow.com/q/32091868)\r\n\r\nHere are all the [weights currently recognized](https://github.com/matplotlib/matplotlib/blob/734c09f164819854a13d77b128da283952eb08d4/lib/matplotlib/font_manager.py#L95-L109):\r\n\r\n```py\r\nweight_dict = {\r\n 'ultralight' : 100,\r\n 'light' : 200,\r\n 'normal' : 400,\r\n 'regular' : 400,\r\n 'book' : 400,\r\n 'medium' : 500,\r\n 'roman' : 500,\r\n 'semibold' : 600,\r\n 'demibold' : 600,\r\n 'demi' : 600,\r\n 'bold' : 700,\r\n 'heavy' : 800,\r\n 'extra bold' : 800,\r\n 'black' : 900}\r\n```\r\n\r\nI imagine we could add “Thin” and some others into the list, but I don’t think this solves the fundamental problem that fonts with unrecognized weights are *presumed* “Regular”/“Bold” and do not have lower precedence than fonts that are *definitely* “Regular”/“Bold”.\r\n\r\nTL;DR: It would be nice to have a more robust way to detect font weights and handle unknown font weights.\r\n\r\n**Matplotlib version**\r\n\u003c!--Please specify your platform and versions of the relevant libraries you are using:--\u003e\r\n * Operating System: Arch Linux\r\n * Matplotlib Version: HEAD\r\n * Python Version: 3.6.0\r\n * Jupyter Version (if applicable): n/a\r\n * Other Libraries: n/a","author":{"url":"https://github.com/Rufflewind","@type":"Person","name":"Rufflewind"},"datePublished":"2017-04-29T23:04:15.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/8550/matplotlib/issues/8550"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:785c4951-bc79-e0dd-18c0-06827ec6cbb5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9F02:217E0D:F82A8:144C2B:6A523958 |
| html-safe-nonce | 79201b5036832030ad696968193c4dc37cbde18cc42ecfcb9405109057c95670 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RjAyOjIxN0UwRDpGODJBODoxNDRDMkI6NkE1MjM5NTgiLCJ2aXNpdG9yX2lkIjoiMTAyMzY4NDQ1NDM5NTI5NjA4OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 5f7b406649b4e5b3c895660ed1df8c5a16b743a6caedf5886b60451a01d0e17a |
| hovercard-subject-tag | issue:225291285 |
| github-keyboard-shortcuts | repository,issues,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/_view_fragments/issues/show/matplotlib/matplotlib/8550/issue_layout |
| twitter:image | https://opengraph.githubassets.com/516ad9d625294efad84aabe6ced080a708de9e482e1e35d1e69aeccc91b11190/matplotlib/matplotlib/issues/8550 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/516ad9d625294efad84aabe6ced080a708de9e482e1e35d1e69aeccc91b11190/matplotlib/matplotlib/issues/8550 |
| og:image:alt | Bug report Bug summary With a lot of the modern fonts containing exotic font weights like “Thin” or “Ultra bold”, matplotlib will sometimes pick the wrong font weight because it assumes that any fo... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Rufflewind |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| 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 |
| disable-turbo | false |
| 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