{"id":1010711,"date":"2024-12-27T11:23:37","date_gmt":"2024-12-27T03:23:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1010711.html"},"modified":"2024-12-27T11:23:39","modified_gmt":"2024-12-27T03:23:39","slug":"python%e7%bb%98%e5%9b%be%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e4%b8%ad%e6%96%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1010711.html","title":{"rendered":"python\u7ed8\u56fe\u5982\u4f55\u6dfb\u52a0\u4e2d\u6587"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085141\/01d5f368-00a5-4385-82cd-8931944d4743.webp\" alt=\"python\u7ed8\u56fe\u5982\u4f55\u6dfb\u52a0\u4e2d\u6587\" \/><\/p>\n<p><p> \u5728Python\u7ed8\u56fe\u4e2d\u6dfb\u52a0\u4e2d\u6587\u901a\u5e38\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<strong>\u9009\u62e9\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\u3001\u8bbe\u7f6e\u5b57\u4f53\u8def\u5f84\u3001\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801<\/strong>\u3002\u5176\u4e2d\uff0c\u9009\u62e9\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a\u9ed8\u8ba4\u7684\u7ed8\u56fe\u5e93\u901a\u5e38\u4e0d\u652f\u6301\u4e2d\u6587\u5b57\u4f53\u3002\u4ee5\u4e0b\u662f\u5173\u4e8e\u5982\u4f55\u5728Python\u4e2d\u7ed8\u56fe\u65f6\u6dfb\u52a0\u4e2d\u6587\u7684\u8be6\u7ec6\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u9009\u62e9\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53<\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684Matplotlib\u5e93\u662f\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u4f46\u5176\u9ed8\u8ba4\u5b57\u4f53\u4e0d\u652f\u6301\u4e2d\u6587\u3002\u8981\u663e\u793a\u4e2d\u6587\uff0c\u9996\u5148\u9700\u8981\u9009\u62e9\u4e00\u6b3e\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\uff0c\u4f8b\u5982SimHei\uff08\u9ed1\u4f53\uff09\u3001SimSun\uff08\u5b8b\u4f53\uff09\u7b49\u3002\u4f60\u53ef\u4ee5\u5728\u7f51\u4e0a\u4e0b\u8f7d\u8fd9\u4e9b\u5b57\u4f53\uff0c\u6216\u8005\u4f7f\u7528\u7cfb\u7edf\u81ea\u5e26\u7684\u5b57\u4f53\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u8bbe\u7f6e\u5b57\u4f53\u8def\u5f84<\/p>\n<\/p>\n<p><p>\u627e\u5230\u5408\u9002\u7684\u5b57\u4f53\u540e\uff0c\u63a5\u4e0b\u6765\u8981\u505a\u7684\u662f\u8bbe\u7f6e\u5b57\u4f53\u8def\u5f84\u3002\u8fd9\u6d89\u53ca\u5230\u5c06\u9009\u5b9a\u7684\u5b57\u4f53\u6587\u4ef6\u52a0\u8f7d\u5230Matplotlib\u4e2d\u3002\u901a\u5e38\uff0c\u4f60\u9700\u8981\u901a\u8fc7<code>matplotlib.font_manager<\/code>\u6a21\u5757\u6765\u5b8c\u6210\u8fd9\u9879\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\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\u5b57\u4f53\u8def\u5f84<\/strong><\/h2>\n<p>font_path = &#39;\/usr\/share\/fonts\/truetype\/arphic\/SimSun.ttf&#39;  # \u793a\u4f8b\u8def\u5f84\uff0c\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u66f4\u6539<\/p>\n<p>font_prop = fm.FontProperties(fname=font_path)<\/p>\n<h2><strong>\u793a\u4f8b\u7ed8\u56fe<\/strong><\/h2>\n<p>plt.title(&#39;\u4e2d\u6587\u6807\u9898&#39;, fontproperties=font_prop)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;, fontproperties=font_prop)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;, fontproperties=font_prop)<\/p>\n<p>plt.plot([1, 2, 3], [4, 5, 6])<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801<\/p>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u7684Python\u811a\u672c\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\uff0c\u901a\u5e38\u662fUTF-8\u3002\u5728\u4f7f\u7528\u4e2d\u6587\u5b57\u7b26\u65f6\uff0c\u786e\u4fdd\u6587\u672c\u6587\u4ef6\u7684\u7f16\u7801\u8bbe\u7f6e\u4e3aUTF-8\uff0c\u4ee5\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002\u6b64\u5916\uff0cPython\u6e90\u6587\u4ef6\u7684\u5f00\u5934\u53ef\u4ee5\u6dfb\u52a0\u5982\u4e0b\u58f0\u660e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># -*- coding: utf-8 -*-<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u786e\u4fddPython\u6b63\u786e\u89e3\u91ca\u6587\u4ef6\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u8be6\u7ec6\u6b65\u9aa4\u4e0e\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4e0b\u8f7d\u5e76\u5b89\u88c5\u4e2d\u6587\u5b57\u4f53<\/strong>\uff1a\u5982\u679c\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u6ca1\u6709\u5408\u9002\u7684\u4e2d\u6587\u5b57\u4f53\uff0c\u53ef\u4ee5\u4ece\u7f51\u4e0a\u4e0b\u8f7d\u5e76\u5b89\u88c5\u3002\u4f8b\u5982\uff0cWindows\u7cfb\u7edf\u4e2d\u5e38\u89c1\u7684SimHei\u5b57\u4f53\u53ef\u4ee5\u5728<code>C:\\Windows\\Fonts<\/code>\u76ee\u5f55\u4e2d\u627e\u5230\uff0c\u800cLinux\u7cfb\u7edf\u4e2d\u53ef\u4ee5\u901a\u8fc7\u8f6f\u4ef6\u5305\u7ba1\u7406\u5668\u5b89\u88c5\u76f8\u5e94\u7684\u4e2d\u6587\u5b57\u4f53\u5305\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6eMatplotlib\u4f7f\u7528\u81ea\u5b9a\u4e49\u5b57\u4f53<\/strong>\uff1a\u901a\u8fc7<code>matplotlib.rcParams<\/code>\u6765\u5168\u5c40\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027\uff0c\u8fd9\u6837\u4f60\u5c31\u4e0d\u9700\u8981\u5728\u6bcf\u6b21\u7ed8\u56fe\u65f6\u90fd\u6307\u5b9a\u5b57\u4f53\u5c5e\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import matplotlib<\/p>\n<p>matplotlib.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;]  # \u6307\u5b9a\u9ed8\u8ba4\u5b57\u4f53<\/p>\n<p>matplotlib.rcParams[&#39;axes.unicode_minus&#39;] = False   # \u7528\u6765\u6b63\u5e38\u663e\u793a\u8d1f\u53f7<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\n<p><strong>\u89e3\u51b3\u5b57\u4f53\u7f13\u5b58\u95ee\u9898<\/strong>\uff1a\u6709\u65f6\u5019\u5373\u4f7f\u8bbe\u7f6e\u4e86\u5b57\u4f53\uff0c\u7ed8\u56fe\u65f6\u4f9d\u7136\u65e0\u6cd5\u663e\u793a\u4e2d\u6587\u3002\u8fd9\u53ef\u80fd\u662f\u7531\u4e8e\u5b57\u4f53\u7f13\u5b58\u7684\u539f\u56e0\u3002\u53ef\u4ee5\u901a\u8fc7\u5220\u9664Matplotlib\u7684\u5b57\u4f53\u7f13\u5b58\u6587\u4ef6\u6765\u89e3\u51b3\u6b64\u95ee\u9898\u3002\u7f13\u5b58\u6587\u4ef6\u901a\u5e38\u4f4d\u4e8e\u7528\u6237\u76ee\u5f55\u4e0b\u7684<code>.matplotlib<\/code>\u6587\u4ef6\u5939\u4e2d\uff0c\u4f60\u53ef\u4ee5\u5220\u9664\u5176\u4e2d\u7684<code>fontList.cache<\/code>\u6587\u4ef6\uff0c\u7136\u540e\u91cd\u65b0\u8fd0\u884c\u4f60\u7684Python\u811a\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528\u5176\u4ed6\u7ed8\u56fe\u5e93<\/strong>\uff1a\u9664\u4e86Matplotlib\uff0cPython\u4e2d\u8fd8\u6709\u5176\u4ed6\u7ed8\u56fe\u5e93\u5982Seaborn\u3001Plotly\u7b49\uff0c\u8fd9\u4e9b\u5e93\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u66f4\u5bb9\u6613\u5904\u7406\u4e2d\u6587\u663e\u793a\u95ee\u9898\u3002\u4f8b\u5982\uff0cPlotly\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u5e93\uff0c\u652f\u6301Web\u7aef\u5c55\u793a\uff0c\u901a\u5e38\u5bf9\u5b57\u4f53\u7684\u652f\u6301\u66f4\u4f73\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u6574\u5b57\u4f53\u5927\u5c0f\u548c\u6837\u5f0f<\/strong>\uff1a\u5728\u4e00\u4e9b\u590d\u6742\u7684\u56fe\u8868\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u8c03\u6574\u5b57\u4f53\u5927\u5c0f\u548c\u6837\u5f0f\u4ee5\u8fbe\u5230\u6700\u4f73\u7684\u53ef\u8bfb\u6027\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7<code>fontproperties<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u5b57\u4f53\u7684\u5927\u5c0f\u3001\u6837\u5f0f\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">plt.title(&#39;\u4e2d\u6587\u6807\u9898&#39;, fontproperties=font_prop, fontsize=14, fontstyle=&#39;italic&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"6\">\n<li><strong>\u5904\u7406\u591a\u8bed\u8a00\u6587\u672c<\/strong>\uff1a\u5728\u4e00\u4e9b\u9700\u8981\u540c\u65f6\u663e\u793a\u591a\u79cd\u8bed\u8a00\u7684\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684<code>Text<\/code>\u5bf9\u8c61\u6765\u624b\u52a8\u8bbe\u7f6e\u6bcf\u4e2a\u6587\u672c\u5143\u7d20\u7684\u5b57\u4f53\u5c5e\u6027\uff0c\u4ee5\u786e\u4fdd\u6240\u6709\u8bed\u8a00\u90fd\u80fd\u6b63\u786e\u663e\u793a\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u5728Python\u7ed8\u56fe\u4e2d\u6210\u529f\u6dfb\u52a0\u4e2d\u6587\u3002\u867d\u7136\u8bbe\u7f6e\u8fc7\u7a0b\u770b\u4f3c\u590d\u6742\uff0c\u4f46\u4e00\u65e6\u914d\u7f6e\u597d\u4ee5\u540e\uff0c\u5c31\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u5404\u7c7b\u56fe\u8868\u4e2d\u663e\u793a\u4e2d\u6587\u6587\u672c\uff0c\u4e3a\u4f60\u7684\u6570\u636e\u53ef\u89c6\u5316\u589e\u6dfb\u66f4\u591a\u7684\u8868\u73b0\u529b\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7ed8\u56fe\u4e2d\u663e\u793a\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u7ed8\u56fe\u4e2d\u663e\u793a\u4e2d\u6587\u5b57\u7b26\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\u3002\u53ef\u4ee5\u901a\u8fc7Matplotlib\u5e93\u4e2d\u7684<code>fontproperties<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u5b57\u4f53\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>matplotlib.font_manager<\/code>\u5bfc\u5165\u5b57\u4f53\u5e76\u8bbe\u7f6e\u4e3a\u4e2d\u6587\u5b57\u4f53\u3002\u786e\u4fdd\u5b57\u4f53\u6587\u4ef6\u7684\u8def\u5f84\u6b63\u786e\uff0c\u4ee5\u4fbf\u6210\u529f\u52a0\u8f7d\u3002<\/p>\n<p><strong>\u4f7f\u7528Matplotlib\u7ed8\u5236\u4e2d\u6587\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Matplotlib\u7ed8\u5236\u4e2d\u6587\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u5b57\u4f53\u672a\u627e\u5230\u3001\u5b57\u7b26\u65e0\u6cd5\u663e\u793a\u7b49\u3002\u8fd9\u901a\u5e38\u662f\u56e0\u4e3a\u6ca1\u6709\u6b63\u786e\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53\u3002\u786e\u4fdd\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u4e86\u76f8\u5e94\u7684\u5b57\u4f53\u8def\u5f84\uff0c\u5e76\u68c0\u67e5\u662f\u5426\u5b89\u88c5\u4e86\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\u5305\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u6dfb\u52a0\u4e2d\u6587\u6ce8\u91ca\u6216\u6807\u9898\uff1f<\/strong><br \/>\u8981\u5728Python\u7ed8\u56fe\u4e2d\u6dfb\u52a0\u4e2d\u6587\u6ce8\u91ca\u6216\u6807\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.title()<\/code>\u3001<code>plt.xlabel()<\/code>\u548c<code>plt.ylabel()<\/code>\u7b49\u51fd\u6570\uff0c\u5e76\u5728\u5176\u4e2d\u8bbe\u7f6e\u4e2d\u6587\u5b57\u7b26\u4e32\u3002\u786e\u4fdd\u5728\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\u4e4b\u524d\u5df2\u7ecf\u8bbe\u7f6e\u597d\u5b57\u4f53\uff0c\u4ee5\u907f\u514d\u4e2d\u6587\u4e71\u7801\u6216\u4e0d\u663e\u793a\u7684\u95ee\u9898\u3002\u4f7f\u7528Unicode\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u4e5f\u80fd\u6709\u6548\u89e3\u51b3\u5b57\u7b26\u663e\u793a\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u7ed8\u56fe\u4e2d\u6dfb\u52a0\u4e2d\u6587\u901a\u5e38\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a\u9009\u62e9\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\u3001\u8bbe\u7f6e\u5b57\u4f53\u8def\u5f84\u3001\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u3002\u5176\u4e2d\uff0c\u9009\u62e9 [&hellip;]","protected":false},"author":3,"featured_media":1010715,"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\/1010711"}],"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=1010711"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1010711\/revisions"}],"predecessor-version":[{"id":1010716,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1010711\/revisions\/1010716"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1010715"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1010711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1010711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1010711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}