{"id":1113330,"date":"2025-01-08T17:46:52","date_gmt":"2025-01-08T09:46:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1113330.html"},"modified":"2025-01-08T17:46:55","modified_gmt":"2025-01-08T09:46:55","slug":"python%e5%9b%be%e4%be%8b%e5%a6%82%e4%bd%95%e5%8a%a0%e4%b8%ad%e6%96%87%e5%ad%97%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1113330.html","title":{"rendered":"python\u56fe\u4f8b\u5982\u4f55\u52a0\u4e2d\u6587\u5b57\u7b26"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074955\/d1f9c0a3-cdc4-4240-a539-5fa761d61309.webp\" alt=\"python\u56fe\u4f8b\u5982\u4f55\u52a0\u4e2d\u6587\u5b57\u7b26\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u56fe\u4f8b\u4e2d\u6dfb\u52a0\u4e2d\u6587\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u8bbe\u7f6e\u5b57\u4f53\u3001\u8c03\u6574\u663e\u793a\u3001\u4f7f\u7528\u4e2d\u6587\u5b57\u4f53\u5e93\u3002<\/strong>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528matplotlib\u5e93\u6765\u7ed8\u5236\u56fe\u50cf\u5e76\u6dfb\u52a0\u4e2d\u6587\u5b57\u7b26\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u8bbe\u7f6e\u5b57\u4f53\u8def\u5f84\u3001\u4f7f\u7528\u5b57\u4f53\u7ba1\u7406\u5668\u4ee5\u53ca\u8c03\u6574\u663e\u793a\u65b9\u5f0f\u6765\u786e\u4fdd\u4e2d\u6587\u5b57\u7b26\u80fd\u6b63\u786e\u663e\u793a\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u4e2a\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u8bbe\u7f6e\u5b57\u4f53\u8def\u5f84<\/strong>\uff1a\u9996\u5148\u9700\u8981\u4e0b\u8f7d\u4e00\u4e2a\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\u6587\u4ef6\uff0c\u6bd4\u5982SimHei.ttf\uff08\u9ed1\u4f53\uff09\uff0c\u5e76\u5c06\u5b57\u4f53\u6587\u4ef6\u653e\u7f6e\u5728\u5408\u9002\u7684\u4f4d\u7f6e\u3002\u7136\u540e\u5728\u4ee3\u7801\u4e2d\u6307\u5b9a\u5b57\u4f53\u8def\u5f84\uff0c\u4f7f\u5f97matplotlib\u80fd\u591f\u4f7f\u7528\u8be5\u5b57\u4f53\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u8bbe\u7f6e\u5b57\u4f53\u8def\u5f84<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u4f7f\u7528matplotlib\u7ed8\u56fe\u65f6\uff0c\u5982\u679c\u76f4\u63a5\u5728\u56fe\u4f8b\u4e2d\u6dfb\u52a0\u4e2d\u6587\u5b57\u7b26\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e71\u7801\u6216\u65e0\u6cd5\u663e\u793a\u7684\u60c5\u51b5\u3002\u8fd9\u662f\u56e0\u4e3amatplotlib\u9ed8\u8ba4\u4f7f\u7528\u7684\u5b57\u4f53\u4e0d\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u624b\u52a8\u8bbe\u7f6e\u5b57\u4f53\u8def\u5f84\uff0c\u6765\u4f7f\u7528\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.font_manager as fm<\/p>\n<h2><strong>\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53<\/strong><\/h2>\n<p>font_path = &#39;path\/to\/SimHei.ttf&#39;  # \u6307\u5b9a\u4e2d\u6587\u5b57\u4f53\u8def\u5f84<\/p>\n<p>my_font = fm.FontProperties(fname=font_path)<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u50cf<\/strong><\/h2>\n<p>plt.plot(x, y, label=&#39;\u6570\u636e&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u56fe\u4f8b\u5e76\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027<\/strong><\/h2>\n<p>plt.legend(prop=my_font)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u7cfb\u7edf\u5b57\u4f53<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u6307\u5b9a\u5b57\u4f53\u6587\u4ef6\u5916\uff0c\u8fd8\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u7cfb\u7edf\u4e2d\u5df2\u7ecf\u5b89\u88c5\u7684\u4e2d\u6587\u5b57\u4f53\u3002\u901a\u8fc7matplotlib\u7684<code>font_manager<\/code>\u6a21\u5757\uff0c\u6211\u4eec\u53ef\u4ee5\u5217\u51fa\u6240\u6709\u53ef\u7528\u7684\u5b57\u4f53\uff0c\u5e76\u9009\u62e9\u5408\u9002\u7684\u5b57\u4f53\u6765\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.font_manager as fm<\/p>\n<h2><strong>\u83b7\u53d6\u7cfb\u7edf\u4e2d\u6240\u6709\u53ef\u7528\u5b57\u4f53<\/strong><\/h2>\n<p>font_list = fm.findSystemFonts(fontpaths=None, fontext=&#39;ttf&#39;)<\/p>\n<p>for font in font_list:<\/p>\n<p>    if &#39;SimHei&#39; in font:  # \u67e5\u627e\u9ed1\u4f53\u5b57\u4f53<\/p>\n<p>        font_path = font<\/p>\n<p>        break<\/p>\n<h2><strong>\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53<\/strong><\/h2>\n<p>my_font = fm.FontProperties(fname=font_path)<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u50cf<\/strong><\/h2>\n<p>plt.plot(x, y, label=&#39;\u6570\u636e&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u56fe\u4f8b\u5e76\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027<\/strong><\/h2>\n<p>plt.legend(prop=my_font)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528matplotlib\u5185\u7f6e\u529f\u80fd<\/h2>\n<\/p>\n<p><p>matplotlib 3.1.0\u53ca\u4ee5\u4e0a\u7248\u672c\u5df2\u7ecf\u652f\u6301\u76f4\u63a5\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53\uff0c\u901a\u8fc7\u4f7f\u7528<code>rcParams<\/code>\u6765\u5168\u5c40\u8bbe\u7f6e\u5b57\u4f53\uff0c\u907f\u514d\u6bcf\u6b21\u7ed8\u56fe\u65f6\u90fd\u9700\u8981\u624b\u52a8\u6307\u5b9a\u5b57\u4f53\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u5168\u5c40\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53<\/strong><\/h2>\n<p>plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;]  # \u8bbe\u7f6e\u7b80\u9ed1\u5b57\u4f53<\/p>\n<p>plt.rcParams[&#39;axes.unicode_minus&#39;] = False  # \u89e3\u51b3\u8d1f\u53f7\u663e\u793a\u95ee\u9898<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u50cf<\/strong><\/h2>\n<p>plt.plot(x, y, label=&#39;\u6570\u636e&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u56fe\u4f8b<\/strong><\/h2>\n<p>plt.legend()<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u66f4\u591a\u7684\u5b57\u4f53\u9009\u62e9\u548c\u4f7f\u7528<\/h2>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u65f6\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u4f7f\u7528\u4e0d\u540c\u7684\u4e2d\u6587\u5b57\u4f53\u6765\u589e\u5f3a\u56fe\u8868\u7684\u7f8e\u89c2\u6027\u548c\u53ef\u8bfb\u6027\u3002\u4f8b\u5982\uff0c\u6977\u4f53\u3001\u5b8b\u4f53\u7b49\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u9009\u62e9\u548c\u4f7f\u7528\u4e0d\u540c\u5b57\u4f53\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import matplotlib.font_manager as fm<\/p>\n<h2><strong>\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53<\/strong><\/h2>\n<p>font_path = &#39;path\/to\/STK<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>TI.TTF&#39;  # \u6307\u5b9a\u6977\u4f53\u5b57\u4f53\u8def\u5f84<\/p>\n<p>my_font = fm.FontProperties(fname=font_path)<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [2, 3, 5, 7, 11]<\/p>\n<p>y2 = [1, 4, 6, 8, 10]<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u50cf<\/strong><\/h2>\n<p>plt.plot(x, y1, label=&#39;\u6570\u636e1&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;\u6570\u636e2&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u56fe\u4f8b\u5e76\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027<\/strong><\/h2>\n<p>plt.legend(prop=my_font)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u6977\u4f53\u5b57\u4f53\u6765\u7ed8\u5236\u56fe\u4f8b\uff0c\u8fd9\u6837\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u4e0d\u540c\u7684\u4e2d\u6587\u5b57\u4f53\uff0c\u4ee5\u786e\u4fdd\u56fe\u8868\u7684\u7f8e\u89c2\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u56fe\u4f8b\u4e2d\u6dfb\u52a0\u4e2d\u6587\u5b57\u7b26\u3002\u65e0\u8bba\u662f\u8bbe\u7f6e\u5b57\u4f53\u8def\u5f84\u3001\u4f7f\u7528\u7cfb\u7edf\u5b57\u4f53\uff0c\u8fd8\u662f\u4f7f\u7528matplotlib\u5185\u7f6e\u529f\u80fd\uff0c\u90fd\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u89e3\u51b3\u4e2d\u6587\u5b57\u7b26\u663e\u793a\u7684\u95ee\u9898\u3002\u9009\u62e9\u9002\u5408\u7684\u5b57\u4f53\u548c\u65b9\u6cd5\uff0c\u5c06\u4f7f\u5f97\u6570\u636e\u53ef\u89c6\u5316\u7684\u7ed3\u679c\u66f4\u52a0\u51c6\u786e\u548c\u7f8e\u89c2\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4e3a\u56fe\u4f8b\u6dfb\u52a0\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u7684\u7ed8\u56fe\u5e93\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5b57\u4f53\u6765\u4e3a\u56fe\u4f8b\u6dfb\u52a0\u4e2d\u6587\u5b57\u7b26\u3002\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u5982Matplotlib\u5141\u8bb8\u7528\u6237\u901a\u8fc7\u6307\u5b9a\u5b57\u4f53\u8def\u5f84\u6216\u4f7f\u7528\u7cfb\u7edf\u5b57\u4f53\u6765\u663e\u793a\u4e2d\u6587\u3002\u786e\u4fdd\u5728\u4f7f\u7528\u524d\u5b89\u88c5\u6240\u9700\u7684\u5b57\u4f53\uff0c\u5e76\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u76f8\u5e94\u7684\u5b57\u4f53\u5c5e\u6027\u3002<\/p>\n<p><strong>\u4f7f\u7528Matplotlib\u65f6\uff0c\u5982\u4f55\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7684\u663e\u793a\u95ee\u9898\uff1f<\/strong><br \/>Matplotlib\u9ed8\u8ba4\u4e0d\u652f\u6301\u4e2d\u6587\u5b57\u7b26\uff0c\u5982\u679c\u76f4\u63a5\u5728\u56fe\u4f8b\u4e2d\u6dfb\u52a0\u4e2d\u6587\uff0c\u4f1a\u51fa\u73b0\u4e71\u7801\u3002\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u7684\u5e38\u7528\u65b9\u6cd5\u662f\u4f7f\u7528<code>matplotlib.rcParams<\/code>\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nplt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;]  # \u8bbe\u7f6e\u9ed1\u4f53\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u4e2d\u6587\u5b57\u7b26\u6b63\u5e38\u663e\u793a\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u63a8\u8350\u7684\u5b57\u4f53\u5e93\u53ef\u4ee5\u5904\u7406\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><br \/>\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7684Python\u56fe\u5f62\u5e93\uff0c\u63a8\u8350\u4f7f\u7528<code>SimHei<\/code>\u6216<code>Microsoft YaHei<\/code>\u7b49\u5e38\u7528\u4e2d\u6587\u5b57\u4f53\u3002\u8fd9\u4e9b\u5b57\u4f53\u5728\u5927\u591a\u6570\u7cfb\u7edf\u4e2d\u8f83\u4e3a\u5e38\u89c1\uff0c\u80fd\u591f\u6709\u6548\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>matplotlib.font_manager<\/code>\u6a21\u5757\u6765\u52a0\u8f7d\u81ea\u5b9a\u4e49\u7684\u4e2d\u6587\u5b57\u4f53\uff0c\u786e\u4fdd\u56fe\u4f8b\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u80fd\u591f\u6b63\u786e\u663e\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u56fe\u4f8b\u4e2d\u6dfb\u52a0\u4e2d\u6587\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u8bbe\u7f6e\u5b57\u4f53\u3001\u8c03\u6574\u663e\u793a\u3001\u4f7f\u7528\u4e2d\u6587\u5b57\u4f53\u5e93\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528matp [&hellip;]","protected":false},"author":3,"featured_media":1113343,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113330"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1113330"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113330\/revisions"}],"predecessor-version":[{"id":1113345,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1113330\/revisions\/1113345"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1113343"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1113330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1113330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1113330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}