{"id":1102107,"date":"2025-01-08T15:56:25","date_gmt":"2025-01-08T07:56:25","guid":{"rendered":""},"modified":"2025-01-08T15:56:32","modified_gmt":"2025-01-08T07:56:32","slug":"%e5%a6%82%e4%bd%95%e8%ae%a9python%e6%98%be%e7%a4%ba%e7%9a%84%e5%9b%be%e7%89%87%e6%a0%87%e9%a2%98%e4%b8%ba%e4%b8%ad%e6%96%87-3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1102107.html","title":{"rendered":"\u5982\u4f55\u8ba9python\u663e\u793a\u7684\u56fe\u7247\u6807\u9898\u4e3a\u4e2d\u6587"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064546\/9ea45adb-e1c1-46dd-90f3-6b16f5f0b767.webp\" alt=\"\u5982\u4f55\u8ba9python\u663e\u793a\u7684\u56fe\u7247\u6807\u9898\u4e3a\u4e2d\u6587\" \/><\/p>\n<p><p> \u8981\u4f7fPython\u663e\u793a\u7684\u56fe\u7247\u6807\u9898\u4e3a\u4e2d\u6587\uff0c\u4e3b\u8981\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Matplotlib\u5e93\u6765\u5b9e\u73b0\u3002<strong>\u786e\u4fdd\u6b63\u786e\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53\u3001\u4f7f\u7528matplotlib\u5e93\u3001\u6307\u5b9a\u5b57\u4f53\u8def\u5f84<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u8fdb\u884c\u8fd9\u4e9b\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u76f8\u5173\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165Matplotlib\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u786e\u4fdd\u6b63\u786e\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53<\/h3>\n<\/p>\n<p><p>Matplotlib\u9ed8\u8ba4\u4e0d\u652f\u6301\u4e2d\u6587\u5b57\u4f53\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u624b\u52a8\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5b57\u4f53\u8def\u5f84\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u627e\u5230\u4e2d\u6587\u5b57\u4f53\u6587\u4ef6<\/strong>\uff1a\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u4e2d\u6587\u5b57\u4f53\u6587\u4ef6\u901a\u5e38\u4f4d\u4e8e<code>C:\\Windows\\Fonts<\/code>\u76ee\u5f55\u4e0b\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u9009\u62e9<code>simhei.ttf<\/code>\uff08\u9ed1\u4f53\uff09\u6216<code>msyh.ttf<\/code>\uff08\u5fae\u8f6f\u96c5\u9ed1\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u5b57\u4f53\u8def\u5f84<\/strong>\uff1a\u901a\u8fc7Matplotlib\u8bbe\u7f6e\u5b57\u4f53\u8def\u5f84\uff0c\u786e\u4fdd\u56fe\u8868\u53ef\u4ee5\u6b63\u786e\u663e\u793a\u4e2d\u6587\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>from matplotlib.font_manager import FontProperties<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b57\u4f53\u8def\u5f84<\/strong><\/h2>\n<p>font_path = &#39;C:\/Windows\/Fonts\/simhei.ttf&#39;  # \u66ff\u6362\u4e3a\u4f60\u7684\u5b57\u4f53\u8def\u5f84<\/p>\n<p>font_prop = FontProperties(fname=font_path)<\/p>\n<h2><strong>\u793a\u4f8b\uff1a\u7ed8\u5236\u56fe\u8868\u5e76\u8bbe\u7f6e\u4e2d\u6587\u6807\u9898<\/strong><\/h2>\n<p>plt.plot([1, 2, 3], [4, 5, 6])<\/p>\n<p>plt.title(&#39;\u793a\u4f8b\u56fe\u8868\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.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u793a\u4f8b\u4ee3\u7801\u8be6\u7ec6\u8bf4\u660e<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u6bb5\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u6f14\u793a\u5982\u4f55\u5728Matplotlib\u4e2d\u663e\u793a\u4e2d\u6587\u6807\u9898\u548c\u6807\u7b7e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>from matplotlib.font_manager import FontProperties<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b57\u4f53\u8def\u5f84<\/strong><\/h2>\n<p>font_path = &#39;C:\/Windows\/Fonts\/simhei.ttf&#39;  # \u66ff\u6362\u4e3a\u4f60\u7684\u5b57\u4f53\u8def\u5f84<\/p>\n<p>font_prop = FontProperties(fname=font_path)<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [10, 15, 20, 25, 30]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u8bbe\u7f6e\u4e2d\u6587\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;\u793a\u4f8b\u56fe\u8868\u6807\u9898&#39;, fontproperties=font_prop)<\/p>\n<p>plt.xlabel(&#39;X\u8f74\u6807\u7b7e&#39;, fontproperties=font_prop)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74\u6807\u7b7e&#39;, fontproperties=font_prop)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u786e\u4fdd\u4e2d\u6587\u5b57\u4f53\u7684\u517c\u5bb9\u6027<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5373\u4f7f\u8bbe\u7f6e\u4e86\u5b57\u4f53\u8def\u5f84\uff0c\u4e2d\u6587\u4ecd\u7136\u53ef\u80fd\u65e0\u6cd5\u6b63\u786e\u663e\u793a\u3002\u8fd9\u53ef\u80fd\u662f\u7531\u4e8e\u5b57\u4f53\u6587\u4ef6\u7684\u517c\u5bb9\u6027\u95ee\u9898\u3002\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u4e0d\u540c\u7684\u5b57\u4f53\u6587\u4ef6\uff0c\u4f8b\u5982<code>msyh.ttf<\/code>\uff08\u5fae\u8f6f\u96c5\u9ed1\uff09\u6216\u5176\u4ed6\u652f\u6301\u4e2d\u6587\u7684\u5b57\u4f53\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u5176\u4ed6\u5e93\uff08\u4f8b\u5982Seaborn\uff09<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Matplotlib\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff08\u5982Seaborn\uff09\u6765\u7ed8\u5236\u56fe\u8868\u5e76\u8bbe\u7f6e\u4e2d\u6587\u6807\u9898\u3002Seaborn\u662f\u4e00\u4e2a\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u5e93\uff0c\u5177\u6709\u66f4\u7b80\u6d01\u7684API\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>from matplotlib.font_manager import FontProperties<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b57\u4f53\u8def\u5f84<\/strong><\/h2>\n<p>font_path = &#39;C:\/Windows\/Fonts\/simhei.ttf&#39;  # \u66ff\u6362\u4e3a\u4f60\u7684\u5b57\u4f53\u8def\u5f84<\/p>\n<p>font_prop = FontProperties(fname=font_path)<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = sns.load_dataset(&#39;iris&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u8868<\/strong><\/h2>\n<p>sns.scatterplot(data=data, x=&#39;sepal_length&#39;, y=&#39;sepal_width&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u4e2d\u6587\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;\u9e22\u5c3e\u82b1\u6570\u636e\u96c6\u6563\u70b9\u56fe&#39;, fontproperties=font_prop)<\/p>\n<p>plt.xlabel(&#39;\u82b1\u843c\u957f\u5ea6&#39;, fontproperties=font_prop)<\/p>\n<p>plt.ylabel(&#39;\u82b1\u843c\u5bbd\u5ea6&#39;, fontproperties=font_prop)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u89e3\u51b3\u5e38\u89c1\u95ee\u9898<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5b57\u4f53\u8def\u5f84\u9519\u8bef<\/strong>\uff1a\u786e\u4fdd\u5b57\u4f53\u8def\u5f84\u6b63\u786e\u65e0\u8bef\uff0c\u53ef\u4ee5\u901a\u8fc7\u6587\u4ef6<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u5668\u9a8c\u8bc1\u8def\u5f84\u3002<\/li>\n<li><strong>\u5b57\u4f53\u6587\u4ef6\u635f\u574f<\/strong>\uff1a\u5c1d\u8bd5\u4f7f\u7528\u5176\u4ed6\u4e2d\u6587\u5b57\u4f53\u6587\u4ef6\uff0c\u786e\u4fdd\u6587\u4ef6\u5b8c\u6574\u3002<\/li>\n<li><strong>Matplotlib\u7248\u672c\u95ee\u9898<\/strong>\uff1a\u786e\u4fdd\u4f7f\u7528\u6700\u65b0\u7248\u672c\u7684Matplotlib\uff0c\u67d0\u4e9b\u65e7\u7248\u672c\u53ef\u80fd\u5b58\u5728\u95ee\u9898\u3002<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u6b63\u786e\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53\u8def\u5f84\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Matplotlib\u4e2d\u663e\u793a\u4e2d\u6587\u6807\u9898\u548c\u6807\u7b7e\u3002\u672c\u6587\u4ecb\u7ecd\u4e86\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u5e2e\u52a9\u4f60\u5728\u6570\u636e\u53ef\u89c6\u5316\u8fc7\u7a0b\u4e2d\u5b9e\u73b0\u4e2d\u6587\u663e\u793a\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<p><p><strong>\u786e\u4fdd\u6b63\u786e\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53\u3001\u4f7f\u7528matplotlib\u5e93\u3001\u6307\u5b9a\u5b57\u4f53\u8def\u5f84<\/strong>\u662f\u5173\u952e\u6b65\u9aa4\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u5b9e\u73b0\u4e2d\u6587\u663e\u793a\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u56fe\u7247\u6807\u9898\u4e3a\u4e2d\u6587\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u663e\u793a\u56fe\u7247\u5e76\u8bbe\u7f6e\u4e2d\u6587\u6807\u9898\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5Matplotlib\u5e93\u3002\u63a5\u4e0b\u6765\uff0c\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53\u4ee5\u907f\u514d\u4e71\u7801\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport matplotlib.font_manager as fm\n\n# \u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53\nfont_path = &#39;path\/to\/your\/chinese\/font.ttf&#39;  # \u66ff\u6362\u4e3a\u4f60\u7684\u5b57\u4f53\u8def\u5f84\nmy_font = fm.FontProperties(fname=font_path)\n\n# \u7ed8\u5236\u56fe\u5f62\nplt.plot([1, 2, 3], [4, 5, 6])\nplt.title(&#39;\u4e2d\u6587\u6807\u9898&#39;, fontproperties=my_font)\nplt.show()\n<\/code><\/pre>\n<p>\u786e\u4fdd\u66ff\u6362\u5b57\u4f53\u8def\u5f84\u4e3a\u4f60\u7cfb\u7edf\u4e2d\u53ef\u7528\u7684\u4e2d\u6587\u5b57\u4f53\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u663e\u793a\u4e2d\u6587\u6807\u9898\u65f6\uff0c\u5982\u4f55\u89e3\u51b3\u4e71\u7801\u95ee\u9898\uff1f<\/strong><br \/>\u51fa\u73b0\u4e71\u7801\u95ee\u9898\u901a\u5e38\u662f\u56e0\u4e3a\u672a\u8bbe\u7f6e\u6b63\u786e\u7684\u5b57\u4f53\u3002\u4f7f\u7528Matplotlib\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u5b57\u4f53\u5c5e\u6027\u6765\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u3002\u786e\u4fdd\u5b57\u4f53\u6587\u4ef6\u652f\u6301\u4e2d\u6587\u5b57\u7b26\uff0c\u5e76\u5728\u7ed8\u5236\u56fe\u5f62\u4e4b\u524d\u901a\u8fc7<code>FontProperties<\/code>\u8bbe\u7f6e\u5b57\u4f53\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u56fe\u50cf\u6807\u9898\u6b63\u786e\u663e\u793a\u4e3a\u4e2d\u6587\uff0c\u800c\u4e0d\u4f1a\u51fa\u73b0\u95ee\u53f7\u6216\u65b9\u5757\u5b57\u7b26\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u7528\u6765\u663e\u793a\u4e2d\u6587\u6807\u9898\uff1f<\/strong><br \/>\u9664\u4e86Matplotlib\uff0cPython\u7684\u5176\u4ed6\u56fe\u5f62\u5e93\u5982Seaborn\u548cPillow\u4e5f\u53ef\u4ee5\u7528\u4e8e\u663e\u793a\u4e2d\u6587\u6807\u9898\u3002Seaborn\u662f\u57fa\u4e8eMatplotlib\u6784\u5efa\u7684\uff0c\u56e0\u6b64\u4f7f\u7528\u65b9\u6cd5\u7c7b\u4f3c\u3002\u800cPillow\u5219\u66f4\u9002\u5408\u4e8e\u56fe\u50cf\u5904\u7406\u548c\u751f\u6210\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ImageDraw<\/code>\u6a21\u5757\u5728\u56fe\u50cf\u4e0a\u7ed8\u5236\u4e2d\u6587\u6587\u672c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from PIL import Image, ImageDraw, ImageFont\n\n# \u521b\u5efa\u56fe\u50cf\nimage = Image.new(&#39;RGB&#39;, (200, 100), (255, 255, 255))\ndraw = ImageDraw.Draw(image)\n\n# \u52a0\u8f7d\u4e2d\u6587\u5b57\u4f53\nfont_path = &#39;path\/to\/your\/chinese\/font.ttf&#39;  # \u66ff\u6362\u4e3a\u4f60\u7684\u5b57\u4f53\u8def\u5f84\nfont = ImageFont.truetype(font_path, 20)\n\n# \u6dfb\u52a0\u4e2d\u6587\u6807\u9898\ndraw.text((10, 10), &#39;\u4e2d\u6587\u6807\u9898&#39;, font=font, fill=&#39;black&#39;)\nimage.show()\n<\/code><\/pre>\n<p>\u4f7f\u7528\u8fd9\u4e9b\u5e93\u65f6\uff0c\u540c\u6837\u9700\u8981\u786e\u4fdd\u6240\u7528\u5b57\u4f53\u6587\u4ef6\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u4f7fPython\u663e\u793a\u7684\u56fe\u7247\u6807\u9898\u4e3a\u4e2d\u6587\uff0c\u4e3b\u8981\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Matplotlib\u5e93\u6765\u5b9e\u73b0\u3002\u786e\u4fdd\u6b63\u786e\u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1102116,"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\/1102107"}],"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=1102107"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1102107\/revisions"}],"predecessor-version":[{"id":1102120,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1102107\/revisions\/1102120"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1102116"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1102107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1102107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1102107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}