{"id":969875,"date":"2024-12-27T05:20:53","date_gmt":"2024-12-26T21:20:53","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/969875.html"},"modified":"2024-12-27T05:20:55","modified_gmt":"2024-12-26T21:20:55","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e5%9b%be%e8%bf%90%e8%a1%8c%e5%87%ba%e6%9d%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/969875.html","title":{"rendered":"Python\u5982\u4f55\u753b\u56fe\u8fd0\u884c\u51fa\u6765"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183938\/7d89c0ad-cd27-4300-ac26-32cac572636c.webp\" alt=\"Python\u5982\u4f55\u753b\u56fe\u8fd0\u884c\u51fa\u6765\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u753b\u56fe\u5e76\u8fd0\u884c\u51fa\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u5b89\u88c5\u6240\u9700\u7684\u5e93\u3001\u5bfc\u5165\u5e93\u5e76\u8bbe\u7f6e\u6570\u636e\u3001\u521b\u5efa\u56fe\u5f62\u3001\u663e\u793a\u56fe\u5f62\u3002<\/strong>\u5176\u4e2d\uff0c\u4ee5\u5b89\u88c5\u548c\u4f7f\u7528Matplotlib\u5e93\u4e3a\u4f8b\uff0c<strong>\u5b89\u88c5\u6240\u9700\u7684\u5e93<\/strong>\u662f\u6700\u57fa\u672c\u7684\u6b65\u9aa4\uff0c\u56e0\u4e3aMatplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u652f\u6301\u591a\u79cd\u56fe\u5f62\u548c\u56fe\u8868\u7684\u7ed8\u5236\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c<strong>\u5bfc\u5165\u5e93\u5e76\u8bbe\u7f6e\u6570\u636e<\/strong>\u662f\u5173\u952e\u7684\u4e00\u6b65\uff0c\u901a\u8fc7\u5bfc\u5165Matplotlib\u5e93\u548c\u5fc5\u8981\u7684\u6a21\u5757\uff0c\u7ed3\u5408\u6570\u636e\u7684\u51c6\u5907\uff0c\u4e3a\u540e\u7eed\u7684\u56fe\u5f62\u7ed8\u5236\u6253\u4e0b\u57fa\u7840\u3002\u63a5\u4e0b\u6765\uff0c<strong>\u521b\u5efa\u56fe\u5f62<\/strong>\u662f\u5b9e\u73b0\u53ef\u89c6\u5316\u7684\u6838\u5fc3\u6b65\u9aa4\uff0c\u901a\u8fc7\u8c03\u7528Matplotlib\u7684\u76f8\u5173\u51fd\u6570\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u81ea\u5b9a\u4e49\u56fe\u5f62\u7684\u6837\u5f0f\u3001\u989c\u8272\u7b49\u5c5e\u6027\u3002\u6700\u540e\uff0c<strong>\u663e\u793a\u56fe\u5f62<\/strong>\u5219\u662f\u6700\u7ec8\u5448\u73b0\u53ef\u89c6\u5316\u7ed3\u679c\u7684\u64cd\u4f5c\uff0c\u901a\u5e38\u4f7f\u7528Matplotlib\u7684<code>show()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u5c06\u5bf9\u8fd9\u4e9b\u6b65\u9aa4\u8fdb\u884c\u8be6\u7ec6\u8bf4\u660e\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u6240\u9700\u7684\u5e93<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cMatplotlib\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u7ed8\u56fe\u5e93\u3002\u8981\u4f7f\u7528\u5b83\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6267\u884c\u4e0a\u8ff0\u547d\u4ee4\u540e\uff0cMatplotlib\u5c06\u88ab\u5b89\u88c5\u5230\u60a8\u7684Python\u73af\u5883\u4e2d\u3002\u786e\u4fdd\u60a8\u7684\u7f51\u7edc\u8fde\u63a5\u6b63\u5e38\uff0c\u4ee5\u4fbf\u987a\u5229\u4e0b\u8f7d\u548c\u5b89\u88c5\u8be5\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5bfc\u5165\u5e93\u5e76\u8bbe\u7f6e\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u5b89\u88c5\u5b8cMatplotlib\u4e4b\u540e\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8be5\u5e93\uff0c\u5e76\u8bbe\u7f6e\u8981\u7ed8\u5236\u7684\u6570\u636e\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 numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u4e86<code>matplotlib.pyplot<\/code>\u6a21\u5757\uff0c\u901a\u5e38\u5c06\u5176\u7b80\u5199\u4e3a<code>plt<\/code>\uff0c\u8fd9\u662fMatplotlib\u4e2d\u6700\u5e38\u7528\u7684\u6a21\u5757\u4e4b\u4e00\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>numpy<\/code>\u5e93\u751f\u6210\u4e00\u4e9b\u6570\u636e\u3002<code>x<\/code>\u662f\u4ece0\u523010\u5747\u5300\u5206\u5e03\u7684100\u4e2a\u6570\u636e\u70b9\uff0c\u800c<code>y<\/code>\u662f\u8fd9\u4e9b\u70b9\u5bf9\u5e94\u7684\u6b63\u5f26\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u521b\u5efa\u56fe\u5f62<\/p>\n<\/p>\n<p><p>\u6709\u4e86\u6570\u636e\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u521b\u5efa\u56fe\u5f62\u3002\u5728Matplotlib\u4e2d\uff0c\u521b\u5efa\u56fe\u5f62\u901a\u5e38\u6d89\u53ca\u5230\u8bbe\u7f6e\u56fe\u5f62\u7684\u7c7b\u578b\u3001\u6837\u5f0f\u4ee5\u53ca\u5176\u4ed6\u89c6\u89c9\u5c5e\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u56fe\u5f62<\/p>\n<p>plt.figure(figsize=(10, 5))<\/p>\n<p>plt.plot(x, y, label=&#39;Sine Wave&#39;, color=&#39;b&#39;, linestyle=&#39;-&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;Sine Wave Example&#39;)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u56fe\u4f8b<\/strong><\/h2>\n<p>plt.legend()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u56fe\u5f62\uff0c\u5e76\u8bbe\u7f6e\u4e86\u5176\u5c3a\u5bf8\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>plot()<\/code>\u51fd\u6570\u7ed8\u5236\u4e86\u4e00\u6761\u6b63\u5f26\u66f2\u7ebf\uff0c\u5e76\u8bbe\u7f6e\u4e86\u66f2\u7ebf\u7684\u6807\u7b7e\u3001\u989c\u8272\u548c\u7ebf\u578b\u3002\u63a5\u7740\uff0c\u6dfb\u52a0\u4e86\u56fe\u5f62\u7684\u6807\u9898\u548c\u5750\u6807\u8f74\u7684\u6807\u7b7e\uff0c\u6700\u540e\u901a\u8fc7<code>legend()<\/code>\u51fd\u6570\u6dfb\u52a0\u4e86\u56fe\u4f8b\uff0c\u4ee5\u4fbf\u6807\u8bc6\u66f2\u7ebf\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u663e\u793a\u56fe\u5f62<\/p>\n<\/p>\n<p><p>\u6700\u540e\u4e00\u6b65\u662f\u663e\u793a\u56fe\u5f62\u3002\u5728\u5b8c\u6210\u56fe\u5f62\u7684\u521b\u5efa\u548c\u8bbe\u7f6e\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5c06\u5176\u663e\u793a\u51fa\u6765\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u663e\u793a\u56fe\u5f62<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>show()<\/code>\u51fd\u6570\u5c06\u6253\u5f00\u4e00\u4e2a\u7a97\u53e3\uff0c\u5e76\u663e\u793a\u6211\u4eec\u521b\u5efa\u7684\u56fe\u5f62\u3002\u5728\u67d0\u4e9b\u73af\u5883\u4e2d\uff08\u5982Jupyter Notebook\uff09\uff0c\u56fe\u5f62\u4f1a\u76f4\u63a5\u5d4c\u5165\u5728\u8f93\u51fa\u533a\u57df\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5176\u4ed6\u53ef\u9009\u6b65\u9aa4<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u57fa\u672c\u6b65\u9aa4\u5916\uff0cMatplotlib\u8fd8\u652f\u6301\u8bb8\u591a\u5176\u4ed6\u529f\u80fd\u548c\u81ea\u5b9a\u4e49\u9009\u9879\uff0c\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528<code>savefig()<\/code>\u51fd\u6570\u5c06\u56fe\u5f62\u4fdd\u5b58\u4e3a\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.savefig(&#39;sine_wave.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u56fe\u5f62\u6837\u5f0f<\/strong>\uff1aMatplotlib\u63d0\u4f9b\u4e86\u591a\u79cd\u9884\u5b9a\u4e49\u7684\u6837\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7<code>plt.style.use()<\/code>\u51fd\u6570\u5e94\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.style.use(&#39;ggplot&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u591a\u56fe\u5f62\u7ed8\u5236<\/strong>\uff1a\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u7a97\u53e3\u4e2d\u7ed8\u5236\u591a\u4e2a\u5b50\u56fe\u5f62\uff0c\u4f7f\u7528<code>subplot()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.subplot(1, 2, 1)  # \u521b\u5efa\u7b2c\u4e00\u4e2a\u5b50\u56fe<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.subplot(1, 2, 2)  # \u521b\u5efa\u7b2c\u4e8c\u4e2a\u5b50\u56fe<\/p>\n<p>plt.plot(x, np.cos(x))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4ea4\u4e92\u5f0f\u7ed8\u56fe<\/strong>\uff1a\u901a\u8fc7\u8bbe\u7f6e<code>plt.ion()<\/code>\u53ef\u4ee5\u542f\u7528\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u6a21\u5f0f\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u4ee3\u7801\u8fd0\u884c\u65f6\u52a8\u6001\u66f4\u65b0\u56fe\u5f62\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u6210\u529f\u7ed8\u5236\u5e76\u8fd0\u884c\u56fe\u5f62\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u60a8\u53ef\u4ee5\u8c03\u6574\u6570\u636e\u3001\u6837\u5f0f\u548c\u5e03\u5c40\uff0c\u4ee5\u5b9e\u73b0\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u5f62\u548c\u53ef\u89c6\u5316\u6548\u679c\u3002Matplotlib\u529f\u80fd\u5f3a\u5927\uff0c\u503c\u5f97\u6df1\u5165\u5b66\u4e60\u548c\u63a2\u7d22\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u7ed8\u5236\u56fe\u5f62\uff1f<\/strong><br \/>Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u4e8e\u7ed8\u56fe\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u5305\u62ecMatplotlib\u3001Seaborn\u548cPlotly\u3002Matplotlib\u662f\u6700\u57fa\u672c\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u521b\u5efa\u7b80\u5355\u76842D\u56fe\u5f62\uff1bSeaborn\u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u63a5\u53e3\uff0c\u66f4\u9002\u5408\u7edf\u8ba1\u56fe\u5f62\u7684\u7ed8\u5236\uff1b\u800cPlotly\u5219\u9002\u7528\u4e8e\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868\uff0c\u9002\u5408\u7f51\u9875\u5c55\u793a\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u4fdd\u5b58\u7ed8\u5236\u7684\u56fe\u5f62\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Matplotlib\u7ed8\u56fe\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>plt.savefig(&#39;filename.png&#39;)<\/code>\u6765\u4fdd\u5b58\u56fe\u5f62\u3002\u5728\u4fdd\u5b58\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u56fe\u50cf\u683c\u5f0f\uff0c\u5982PNG\u3001JPEG\u6216PDF\u7b49\u3002\u786e\u4fdd\u5728\u8c03\u7528<code>plt.show()<\/code>\u4e4b\u524d\u6267\u884c\u4fdd\u5b58\u547d\u4ee4\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u56fe\u5f62\u88ab\u6e05\u7a7a\u3002<\/p>\n<p><strong>\u5982\u4f55\u8c03\u6574\u56fe\u5f62\u7684\u6837\u5f0f\u548c\u5916\u89c2\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eMatplotlib\u7684\u6837\u5f0f\u53c2\u6570\u6765\u8c03\u6574\u56fe\u5f62\u7684\u5916\u89c2\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.style.use(&#39;ggplot&#39;)<\/code>\u6765\u5e94\u7528\u4e0d\u540c\u7684\u6837\u5f0f\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u989c\u8272\u3001\u7ebf\u578b\u3001\u6807\u8bb0\u548c\u5b57\u4f53\u7b49\u5c5e\u6027\u6765\u5b9a\u5236\u56fe\u5f62\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>plt.plot(x, y, color=&#39;red&#39;, linestyle=&#39;--&#39;, marker=&#39;o&#39;)<\/code>\u6765\u6539\u53d8\u7ebf\u6761\u7684\u989c\u8272\u3001\u6837\u5f0f\u548c\u6807\u8bb0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u753b\u56fe\u5e76\u8fd0\u884c\u51fa\u6765\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u5b89\u88c5\u6240\u9700\u7684\u5e93\u3001\u5bfc\u5165\u5e93\u5e76\u8bbe\u7f6e\u6570\u636e\u3001\u521b\u5efa\u56fe\u5f62\u3001\u663e\u793a\u56fe\u5f62\u3002\u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":969879,"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\/969875"}],"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=969875"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/969875\/revisions"}],"predecessor-version":[{"id":969880,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/969875\/revisions\/969880"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/969879"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=969875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=969875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=969875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}