Title: Matplotlib would not try to apply all the font in font list to draw all characters in the given string. · Issue #18883 · matplotlib/matplotlib · GitHub
Open Graph Title: Matplotlib would not try to apply all the font in font list to draw all characters in the given string. · Issue #18883 · matplotlib/matplotlib
X Title: Matplotlib would not try to apply all the font in font list to draw all characters in the given string. · Issue #18883 · matplotlib/matplotlib
Description: Bug report Bug summary The document page Configuring the font family said that: You can explicitly set which font family is picked up for a given font style (e.g., 'serif', 'sans-serif', or 'monospace'). In the example below, we only all...
Open Graph Description: Bug report Bug summary The document page Configuring the font family said that: You can explicitly set which font family is picked up for a given font style (e.g., 'serif', 'sans-serif', or 'monosp...
X Description: Bug report Bug summary The document page Configuring the font family said that: You can explicitly set which font family is picked up for a given font style (e.g., 'serif', 'sans-serif&...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/18883
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Matplotlib would not try to apply all the font in font list to draw all characters in the given string.","articleBody":"### Bug report\r\n\r\n**Bug summary**\r\n\r\nThe document page [Configuring the font family](https://matplotlib.org/3.1.0/gallery/text_labels_and_annotations/font_family_rc_sgskip.html) said that:\r\n\r\n\u003e You can explicitly set which font family is picked up for a given font style (e.g., 'serif', 'sans-serif', or 'monospace').\r\n\u003e In the example below, we only allow one font family (Tahoma) for the sans-serif font style. You the default family with the font.family rc param, e.g.,:\r\n\u003e ```python\r\n\u003e rcParams['font.family'] = 'sans-serif'\r\n\u003e ```\r\n\u003e and for the font.family you set a list of font styles to try to find in order:\r\n\u003e ```python\r\n\u003e rcParams['font.sans-serif'] = ['Tahoma', 'DejaVu Sans', 'Lucida Grande', 'Verdana']\r\n\u003e ```\r\n\r\nBut the mechanism behind the strings rendering with the font-family is not as same as what most users think.\r\n\r\n- The `matplotlib` just finds the font in the font list and apply the first valid one (can be found in the given path) to all characters in the given string.\r\n- We can use a list of fonts to the font-family settings in Visual Studio Code, Sublime Text, and websites. They have their own algorithm to deal with the problem of fonts fallback so that we can apply each font in the list in order to draw the correct character. Refer to [How do web browsers implement font fallback?](https://stackoverflow.com/questions/29241764/how-do-web-browsers-implement-font-fallback)\r\n\r\nWould `matplotlib` have any plan to implement this feature? There were so many users got into the trouble of missing fonts when we want to show the string mixed with CJK characters and Latin characters.\r\n\r\n**Code for reproduction**\r\n\r\nJust take a look at this [Colab Notebook](https://colab.research.google.com/drive/1kykBfJvV1X9XRgQP_iF2rdwjpKz1Ig_q?usp=sharing).\r\n\r\n#### use `['DejaVu Sans', 'SimHei', 'sans-serif']`\r\n\r\n\u003cimg align=\"right\" width=\"450\" src=\"https://user-images.githubusercontent.com/26391143/98018127-248c2480-1e3b-11eb-8956-fd1c2b42364c.png\"\u003e\r\n\r\n```python\r\n# Setup the fonts\r\nmatplotlib.rcParams['font.sans-serif'] = ['DejaVu Sans', 'SimHei', 'sans-serif']\r\n\r\ndef model(x, p):\r\n return x ** (2 * p + 1) / (1 + x ** (2 * p))\r\n\r\nx = np.linspace(0.75, 1.25, 201)\r\n\r\nfig, ax = plt.subplots()\r\nfor p in [10, 15, 20, 30, 50, 100]:\r\n ax.plot(x, model(x, p), label=p)\r\nax.legend(title='Order')\r\nax.set(xlabel='電壓 Voltage (mV)')\r\nax.set(ylabel='電流 Current (A)')\r\nax.autoscale(tight=True)\r\n```\r\n\r\n- `電壓` and `電流` can't be correctly rendered with the current font `DejaVu Sans`.\r\n\r\n#### use `['SimHei', 'DejaVu Sans', 'sans-serif']`\r\n\r\n\u003cimg align=\"right\" width=\"450\" src=\"https://user-images.githubusercontent.com/26391143/98018374-6c12b080-1e3b-11eb-91b3-f1c663a72549.png\"\u003e\r\n\r\n```python\r\n# Setup the fonts\r\nmatplotlib.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans', 'sans-serif']\r\n\r\ndef model(x, p):\r\n return x ** (2 * p + 1) / (1 + x ** (2 * p))\r\n\r\nx = np.linspace(0.75, 1.25, 201)\r\n\r\nfig, ax = plt.subplots()\r\nfor p in [10, 15, 20, 30, 50, 100]:\r\n ax.plot(x, model(x, p), label=p)\r\nax.legend(title='Order')\r\nax.set(xlabel='電壓 Voltage (mV)')\r\nax.set(ylabel='電流 Current (A)')\r\nax.autoscale(tight=True)\r\n```\r\n\r\n- `電壓` and `電流` can be correctly rendered with the current font `SimHei`.\r\n- `Voltage` and `Current` are also be rendered with the current font `SimHei`.\r\n\r\n**Expected outcome**\r\n\r\nWe would like to apply the Chinese font `SimHei` to the Chinese characters and the English font `DejaVu Sans` to the English characters. Just like what we use `font-family` style in CSS:\r\n\r\n\u003cp align=\"center\"\u003e\r\n \u003cimg width=\"460\" src=\"https://user-images.githubusercontent.com/26391143/98018992-2f938480-1e3c-11eb-87a0-12a763fb2938.png\"\u003e\r\n\u003c/p\u003e\r\n\r\n### Similar Issues\r\n\r\n- https://github.com/matplotlib/matplotlib/issues/15260\r\n","author":{"url":"https://github.com/Hsins","@type":"Person","name":"Hsins"},"datePublished":"2020-11-03T17:27:25.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/18883/matplotlib/issues/18883"}
| 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:f581fd7f-707f-089f-1440-dd1e52ebf550 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E202:9F062:288E326:399B538:6A510429 |
| html-safe-nonce | a617bf1667e148944469c2f389ed500f441796fda330d7f493ee06bdc1dbc793 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMjAyOjlGMDYyOjI4OEUzMjY6Mzk5QjUzODo2QTUxMDQyOSIsInZpc2l0b3JfaWQiOiI4MDQxODUwOTk1MzQ3NjIwOTA1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 919a4dbdf207ff33f3df53087d69c66405932d1a9a6460d863eb456df8773354 |
| hovercard-subject-tag | issue:735507254 |
| 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/18883/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fea09f68bda18191bc1773be111834f558d6d4c3d4524f9f16d2b6787add9d70/matplotlib/matplotlib/issues/18883 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fea09f68bda18191bc1773be111834f558d6d4c3d4524f9f16d2b6787add9d70/matplotlib/matplotlib/issues/18883 |
| og:image:alt | Bug report Bug summary The document page Configuring the font family said that: You can explicitly set which font family is picked up for a given font style (e.g., 'serif', 'sans-serif', or 'monosp... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Hsins |
| hostname | github.com |
| expected-hostname | github.com |
| None | 38906a4da4e3e9fd88dfeacee232f5932c0c5f7495de3b4efc53ddfac7173d12 |
| 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 | 90441ea9b98e7491aa6ebcd135fccce4e2396f48 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width