{"id":17011805,"url":"https://github.com/flaribbit/fix-matplotlib-chinese","last_synced_at":"2026-04-28T21:06:10.019Z","repository":{"id":137699312,"uuid":"421781887","full_name":"flaribbit/fix-matplotlib-chinese","owner":"flaribbit","description":"修复 matplotlib 中英混排问题","archived":false,"fork":false,"pushed_at":"2021-10-28T14:21:26.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-22T13:26:12.718Z","etag":null,"topics":["chinese","matplotlib"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/flaribbit.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-27T10:59:50.000Z","updated_at":"2021-10-28T14:21:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"2723924d-5f60-43e3-a71a-015750d0334c","html_url":"https://github.com/flaribbit/fix-matplotlib-chinese","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/flaribbit/fix-matplotlib-chinese","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flaribbit%2Ffix-matplotlib-chinese","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flaribbit%2Ffix-matplotlib-chinese/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flaribbit%2Ffix-matplotlib-chinese/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flaribbit%2Ffix-matplotlib-chinese/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flaribbit","download_url":"https://codeload.github.com/flaribbit/fix-matplotlib-chinese/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flaribbit%2Ffix-matplotlib-chinese/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32399100,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-28T19:38:08.556Z","status":"ssl_error","status_checked_at":"2026-04-28T19:37:55.688Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chinese","matplotlib"],"created_at":"2024-10-14T06:08:05.599Z","updated_at":"2026-04-28T21:06:10.005Z","avatar_url":"https://github.com/flaribbit.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 修复 matplotlib 中英混排问题\n\n众所周知，matplotlib 默认字体不能显示中文，但论文通常要求 Times + 宋体，于是考虑这样写：\n\n```py\nplt.rcParams['font.sans-serif'] = ['Times New Roman', 'Simsun']\n```\n\n结果并没有什么卵用，还是只能 Times，于是翻阅了一下其源码，提出以下解决方案：\n\n找到文件 `_mathtext.py`，222行附近，增加一行中文字体\n```diff\nclass TruetypeFonts(Fonts):\n    \"\"\"\n    A generic base class for all font setups that use Truetype fonts\n    (through FT2Font).\n    \"\"\"\n    def __init__(self, default_font_prop, mathtext_backend):\n        super().__init__(default_font_prop, mathtext_backend)\n        self.glyphd = {}\n        self._fonts = {}\n\n        filename = findfont(default_font_prop)\n        default_font = get_font(filename)\n        self._fonts['default'] = default_font\n        self._fonts['regular'] = default_font\n+       self._fonts['chinese'] = get_font(findfont('Simsun'))\n```\n\n530行附近，增加一条判断语句\n```diff\n        if not found_symbol:\n            if self.cm_fallback:\n                if (fontname in ('it', 'regular')\n                        and isinstance(self.cm_fallback, StixFonts)):\n                    fontname = 'rm'\n\n                g = self.cm_fallback._get_glyph(fontname, font_class,\n                                                sym, fontsize)\n                fname = g[0].family_name\n                if fname in list(BakomaFonts._fontmap.values()):\n                    fname = \"Computer Modern\"\n                _log.info(\"Substituting symbol %s from %s\", sym, fname)\n                return g\n\n+           elif fontname != 'chinese':\n+               return self._get_glyph('chinese', font_class, sym, fontsize)\n\n            else:\n                if (fontname in ('it', 'regular')\n                        and isinstance(self, StixFonts)):\n                    return self._get_glyph('rm', font_class, sym, fontsize)\n```\n\n这样一来会把默认字体找不到的符号用宋体代替，如果还找不到才渲染成框。\n\n![demo](test.svg)\n\n最后，这只是个临时解决方案，坐等[这条PR](https://github.com/matplotlib/matplotlib/pull/20740)合并。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflaribbit%2Ffix-matplotlib-chinese","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflaribbit%2Ffix-matplotlib-chinese","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflaribbit%2Ffix-matplotlib-chinese/lists"}