{"id":976323,"date":"2024-12-27T06:20:39","date_gmt":"2024-12-26T22:20:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/976323.html"},"modified":"2024-12-27T06:20:41","modified_gmt":"2024-12-26T22:20:41","slug":"%e5%a6%82%e4%bd%95%e7%94%b5%e8%84%91%e8%bf%90%e8%a1%8cpython%e7%94%bb%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/976323.html","title":{"rendered":"\u5982\u4f55\u7535\u8111\u8fd0\u884cpython\u753b\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24201141\/750a23fd-41c2-4c95-8c0d-102a41d7824e.webp\" alt=\"\u5982\u4f55\u7535\u8111\u8fd0\u884cpython\u753b\u56fe\" \/><\/p>\n<p><p> <strong>\u8981\u5728\u7535\u8111\u4e0a\u8fd0\u884cPython\u8fdb\u884c\u7ed8\u56fe\uff0c\u4f60\u9700\u8981\u5b89\u88c5Python\u7f16\u8bd1\u5668\u3001\u5b89\u88c5\u7ed8\u56fe\u5e93\uff08\u5982Matplotlib\uff09\u3001\u7f16\u5199\u7ed8\u56fe\u4ee3\u7801\u3001\u6267\u884c\u4ee3\u7801\u5e76\u67e5\u770b\u56fe\u5f62\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u5b89\u88c5Python\u7f16\u8bd1\u5668<\/strong>\u662f\u9996\u8981\u6b65\u9aa4\uff0c\u56e0\u4e3a\u5b83\u662f\u8fd0\u884cPython\u4ee3\u7801\u7684\u57fa\u7840\u3002\u63a5\u4e0b\u6765\uff0c\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u5b8c\u6210\u6bcf\u4e00\u4e2a\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5Python\u7f16\u8bd1\u5668<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7ed8\u56fe\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u5728\u7535\u8111\u4e0a\u5b89\u88c5Python\u7f16\u8bd1\u5668\u3002Python\u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ef\u4ee5\u901a\u8fc7\u5176\u5b98\u65b9\u7684Python\u7f51\u7ad9\u4e0b\u8f7d\u548c\u5b89\u88c5\u3002\u5b89\u88c5Python\u65f6\uff0c\u52a1\u5fc5\u9009\u62e9\u201cAdd Python to PATH\u201d\u9009\u9879\uff0c\u8fd9\u5c06\u4f7fPython\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u4e2d\u88ab\u8bc6\u522b\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4e0b\u8f7dPython\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u8bbf\u95eePython\u7684\u5b98\u65b9\u7f51\u7ad9 <a href=\"https:\/\/www.python.org\/\">python.org<\/a>\u3002<\/li>\n<li>\u5728\u4e0b\u8f7d\u9875\u9762\u9009\u62e9\u9002\u5408\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u7684Python\u7248\u672c\uff08\u5efa\u8bae\u9009\u62e9\u6700\u65b0\u7a33\u5b9a\u7248\u672c\uff09\u3002<\/li>\n<li>\u4e0b\u8f7d\u5b8c\u6210\u540e\uff0c\u8fd0\u884c\u5b89\u88c5\u7a0b\u5e8f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5Python\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\uff0c\u9009\u62e9\u201cAdd Python to PATH\u201d\u9009\u9879\u3002<\/li>\n<li>\u6309\u7167\u5b89\u88c5\u5411\u5bfc\u7684\u6307\u793a\u5b8c\u6210\u5b89\u88c5\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u9a8c\u8bc1\u5b89\u88c5\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\uff08Windows\uff09\u6216\u7ec8\u7aef\uff08Mac\/Linux\uff09\u3002<\/li>\n<li>\u8f93\u5165<code>python --version<\/code>\u6216<code>python3 --version<\/code>\uff0c\u67e5\u770b\u662f\u5426\u663e\u793aPython\u7248\u672c\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u5b89\u88c5\u7ed8\u56fe\u5e93\uff08Matplotlib\uff09<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u7528\u4e8e\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u5f62\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u6765\u5b89\u88c5\u5b83\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Matplotlib\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\u3002<\/li>\n<li>\u8f93\u5165<code>pip install matplotlib<\/code>\u5e76\u6309Enter\u952e\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u9a8c\u8bc1\u5b89\u88c5\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728Python\u4ea4\u4e92\u5f0f\u73af\u5883\uff08\u5982IDLE\uff09\u4e2d\u8f93\u5165<code>import matplotlib<\/code>\uff0c\u5982\u679c\u6ca1\u6709\u9519\u8bef\u63d0\u793a\uff0c\u5219\u5b89\u88c5\u6210\u529f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u7f16\u5199\u7ed8\u56fe\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u6709\u4e86Python\u548cMatplotlib\uff0c\u4f60\u53ef\u4ee5\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u6765\u751f\u6210\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u7528\u4e8e\u7ed8\u5236\u6298\u7ebf\u56fe\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efaPython\u6587\u4ef6\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\uff08\u5982VS Code\u3001Sublime Text\uff09\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\uff0c\u547d\u540d\u4e3a<code>plot_example.py<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7f16\u5199\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\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>\u521b\u5efa\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;Simple Line Plot&#39;)<\/p>\n<p>plt.xlabel(&#39;X Axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y Axis&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u6267\u884c\u4ee3\u7801\u5e76\u67e5\u770b\u56fe\u5f62<\/p>\n<\/p>\n<p><p>\u7f16\u5199\u5b8c\u6210\u4ee3\u7801\u540e\uff0c\u4f60\u9700\u8981\u8fd0\u884c\u5b83\u4ee5\u751f\u6210\u5e76\u67e5\u770b\u56fe\u5f62\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8fd0\u884c\u4ee3\u7801\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u6216\u7ec8\u7aef\u4e2d\uff0c\u5bfc\u822a\u5230\u4fdd\u5b58<code>plot_example.py<\/code>\u6587\u4ef6\u7684\u76ee\u5f55\u3002<\/li>\n<li>\u8f93\u5165<code>python plot_example.py<\/code>\u6216<code>python3 plot_example.py<\/code>\u5e76\u6309Enter\u952e\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u67e5\u770b\u56fe\u5f62\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4ee3\u7801\u8fd0\u884c\u540e\uff0cMatplotlib\u5c06\u6253\u5f00\u4e00\u4e2a\u7a97\u53e3\uff0c\u663e\u793a\u4f60\u521b\u5efa\u7684\u6298\u7ebf\u56fe\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u63a2\u7d22\u66f4\u591a\u7ed8\u56fe\u529f\u80fd<\/p>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5e93\uff0c\u652f\u6301\u591a\u79cd\u56fe\u5f62\u7c7b\u578b\u548c\u6837\u5f0f\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8fdb\u4e00\u6b65\u63a2\u7d22\u5176\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7ed8\u5236\u4e0d\u540c\u7c7b\u578b\u7684\u56fe\u5f62\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>Matplotlib\u652f\u6301\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u6563\u70b9\u56fe\u3001\u997c\u56fe\u7b49\u591a\u79cd\u7c7b\u578b\u3002<\/li>\n<li>\u4f8b\u5982\uff0c\u4f7f\u7528<code>plt.bar()<\/code>\u53ef\u4ee5\u7ed8\u5236\u67f1\u72b6\u56fe\uff0c\u4f7f\u7528<code>plt.scatter()<\/code>\u53ef\u4ee5\u7ed8\u5236\u6563\u70b9\u56fe\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u56fe\u5f62\u6837\u5f0f\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u989c\u8272\u3001\u7ebf\u6761\u7c7b\u578b\u3001\u6807\u8bb0\u7b49\u5c5e\u6027\u6765\u81ea\u5b9a\u4e49\u56fe\u5f62\u7684\u5916\u89c2\u3002<\/li>\n<li>\u4f7f\u7528<code>plt.style.use(&#39;style_name&#39;)<\/code>\u53ef\u4ee5\u5e94\u7528\u9884\u8bbe\u7684\u6837\u5f0f\u4e3b\u9898\uff0c\u5982<code>ggplot<\/code>\u6216<code>seaborn<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u6ce8\u91ca\u548c\u6587\u672c\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>plt.text()<\/code>\u53ef\u4ee5\u5728\u56fe\u5f62\u4e0a\u6dfb\u52a0\u6587\u672c\u6ce8\u91ca\uff0c\u5e2e\u52a9\u89e3\u91ca\u6570\u636e\u3002<\/li>\n<li><code>plt.annotate()<\/code>\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u6ce8\u91ca\u529f\u80fd\uff0c\u53ef\u4ee5\u7ed8\u5236\u7bad\u5934\u548c\u5176\u4ed6\u6807\u8bb0\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u591a\u56fe\u5e03\u5c40\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>plt.subplot()<\/code>\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u7a97\u53e3\u4e2d\u7ed8\u5236\u591a\u4e2a\u5b50\u56fe\u3002<\/li>\n<li>\u8fd9\u5bf9\u4e8e\u6bd4\u8f83\u4e0d\u540c\u6570\u636e\u96c6\u6216\u5c55\u793a\u591a\u79cd\u89c6\u89d2\u7279\u522b\u6709\u7528\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4fdd\u5b58\u56fe\u5f62\uff1a<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528<code>plt.savefig(&#39;filename.png&#39;)<\/code>\u53ef\u4ee5\u5c06\u56fe\u5f62\u4fdd\u5b58\u4e3a\u6587\u4ef6\u683c\u5f0f\uff0c\u5982PNG\u3001PDF\u7b49\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5728\u7535\u8111\u4e0a\u6210\u529f\u8fd0\u884cPython\u5e76\u4f7f\u7528Matplotlib\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\u3002\u968f\u7740\u4f60\u5bf9Python\u548cMatplotlib\u7684\u719f\u6089\uff0c\u4f60\u5c06\u80fd\u521b\u5efa\u66f4\u590d\u6742\u3001\u66f4\u4e13\u4e1a\u7684\u56fe\u8868\u6765\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u7ee7\u7eed\u6df1\u5165\u7814\u7a76\u548c\u5b9e\u8df5\uff0c\u4f60\u4f1a\u53d1\u73b0Python\u5728\u6570\u636e\u53ef\u89c6\u5316\u65b9\u9762\u7684\u5de8\u5927\u6f5c\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u7535\u8111\u4e0a\u5b89\u88c5Python\u4ee5\u4fbf\u8fdb\u884c\u7ed8\u56fe\uff1f<\/strong><br \/>\u8981\u5728\u7535\u8111\u4e0a\u8fdb\u884cPython\u7ed8\u56fe\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5Python\u3002\u53ef\u4ee5\u8bbf\u95eePython\u7684\u5b98\u65b9\u7f51\u7ad9\uff0c\u4e0b\u8f7d\u9002\u5408\u60a8\u64cd\u4f5c\u7cfb\u7edf\u7684\u5b89\u88c5\u5305\u5e76\u6309\u7167\u63d0\u793a\u5b8c\u6210\u5b89\u88c5\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5efa\u8bae\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5\u7ed8\u56fe\u5e93\uff0c\u5982Matplotlib\u6216Seaborn\uff0c\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u7ed8\u56fe\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u5e38\u7528\u7684Python\u7ed8\u56fe\u5e93\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u7ed8\u56fe\u5e93\u53ef\u4ee5\u9009\u62e9\u3002\u6700\u5e38\u7528\u7684\u5305\u62ecMatplotlib\uff0c\u5b83\u662f\u529f\u80fd\u5f3a\u5927\u7684\u57fa\u7840\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u5236\u4f5c\u9759\u6001\u56fe\u8868\uff1bSeaborn\uff0c\u5b83\u57fa\u4e8eMatplotlib\uff0c\u63d0\u4f9b\u4e86\u66f4\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\u548c\u66f4\u7b80\u4fbf\u7684\u63a5\u53e3\uff1b\u8fd8\u6709Plotly\uff0c\u9002\u5408\u5236\u4f5c\u4ea4\u4e92\u5f0f\u56fe\u8868\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u6839\u636e\u60a8\u7684\u5177\u4f53\u9700\u6c42\u548c\u7ed8\u56fe\u590d\u6742\u5ea6\u6765\u51b3\u5b9a\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u7ed8\u5236\u7b80\u5355\u7684\u56fe\u5f62\uff1f<\/strong><br \/>\u7ed8\u5236\u7b80\u5355\u7684\u56fe\u5f62\u5728Python\u4e2d\u975e\u5e38\u5bb9\u6613\u3002\u4ee5Matplotlib\u4e3a\u4f8b\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u7684\u6298\u7ebf\u56fe\uff1a  <\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\nx = [1, 2, 3, 4]\ny = [10, 15, 7, 10]\nplt.plot(x, y)\nplt.title(&#39;\u7b80\u5355\u6298\u7ebf\u56fe&#39;)\nplt.xlabel(&#39;X\u8f74&#39;)\nplt.ylabel(&#39;Y\u8f74&#39;)\nplt.show()\n<\/code><\/pre>\n<p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u540e\uff0c\u60a8\u5c06\u770b\u5230\u4e00\u4e2a\u5305\u542b\u6570\u636e\u70b9\u7684\u6298\u7ebf\u56fe\u3002\u8fd9\u662f\u5f00\u59cb\u5b66\u4e60Python\u7ed8\u56fe\u7684\u57fa\u7840\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728\u7535\u8111\u4e0a\u8fd0\u884cPython\u8fdb\u884c\u7ed8\u56fe\uff0c\u4f60\u9700\u8981\u5b89\u88c5Python\u7f16\u8bd1\u5668\u3001\u5b89\u88c5\u7ed8\u56fe\u5e93\uff08\u5982Matplotlib\uff09\u3001\u7f16\u5199\u7ed8 [&hellip;]","protected":false},"author":3,"featured_media":976330,"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\/976323"}],"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=976323"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/976323\/revisions"}],"predecessor-version":[{"id":976331,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/976323\/revisions\/976331"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/976330"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=976323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=976323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=976323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}