{"id":998653,"date":"2024-12-27T09:34:57","date_gmt":"2024-12-27T01:34:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/998653.html"},"modified":"2024-12-27T09:34:59","modified_gmt":"2024-12-27T01:34:59","slug":"python%e7%94%bb%e5%9b%be%e5%a6%82%e4%bd%95%e6%a0%87%e4%b8%8b%e6%a0%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/998653.html","title":{"rendered":"python\u753b\u56fe\u5982\u4f55\u6807\u4e0b\u6807"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074012\/0395049b-1482-4eff-b9b4-9fe39b653abf.webp\" alt=\"python\u753b\u56fe\u5982\u4f55\u6807\u4e0b\u6807\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u7ed8\u5236\u56fe\u5f62\u5e76\u6dfb\u52a0\u4e0b\u6807\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3002\u4f7f\u7528Matplotlib\u5e93\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728\u6807\u7b7e\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u6570\u5b66\u8868\u8fbe\u5f0f\u8bed\u6cd5\u6765\u6dfb\u52a0\u4e0b\u6807\u3002\u5177\u4f53\u64cd\u4f5c\u5305\u62ec\u4f7f\u7528<code>\u7b26\u53f7\u5c06\u6570\u5b66\u8868\u8fbe\u5f0f\u5305\u56f4\u8d77\u6765\uff0c\u4f7f\u7528\u53cd\u659c\u6760<\/code>`\u6765\u8f6c\u4e49\u4e0b\u6807\u7b26\u53f7\uff0c\u4ee5\u53ca\u5728\u4e0b\u6807\u5b57\u7b26\u524d\u4f7f\u7528\u4e0b\u5212\u7ebf<code>_<\/code>\u3002<\/strong> \u4f8b\u5982\uff0c\u53ef\u4ee5\u5728\u6807\u7b7e\u4e2d\u7f16\u5199<code>$H_2O$<\/code>\u6765\u8868\u793a\u5316\u5b66\u5f0f\u6c34\u7684\u4e0b\u6807\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u56fe\u5f62\u5e76\u6807\u8bb0\u4e0b\u6807\uff0c\u5305\u62ec\u4f7f\u7528Matplotlib\u5e93\u7684\u5177\u4f53\u6b65\u9aa4\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165Matplotlib<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7ed8\u56fe\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff0c\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>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728\u4f60\u7684Python\u811a\u672c\u6216Jupyter Notebook\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><p>\u4e8c\u3001\u57fa\u672c\u7ed8\u56fe\u6d41\u7a0b<\/p>\n<\/p>\n<p><p>\u5728Matplotlib\u4e2d\uff0c\u7ed8\u56fe\u901a\u5e38\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61\u3001\u7ed8\u5236\u6570\u636e\u3001\u6dfb\u52a0\u6807\u7b7e\u548c\u6807\u9898\u3001\u663e\u793a\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7ed8\u56fe\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/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>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<h2><strong>\u7ed8\u5236\u6570\u636e<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u7b7e\u548c\u6807\u9898<\/strong><\/h2>\n<p>plt.xlabel(&#39;X\u8f74\u6807\u7b7e&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74\u6807\u7b7e&#39;)<\/p>\n<p>plt.title(&#39;\u7b80\u5355\u6298\u7ebf\u56fe&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5728\u6807\u7b7e\u4e2d\u6dfb\u52a0\u4e0b\u6807<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728\u6807\u7b7e\u4e2d\u6dfb\u52a0\u4e0b\u6807\uff0c\u4f60\u53ef\u4ee5\u5728\u5b57\u7b26\u4e32\u4e2d\u4f7f\u7528\u6570\u5b66\u8868\u8fbe\u5f0f\u8bed\u6cd5\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u6570\u5b66\u8868\u8fbe\u5f0f\u8bed\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u5728Matplotlib\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>$<\/code>\u7b26\u53f7\u5c06\u6570\u5b66\u8868\u8fbe\u5f0f\u5305\u56f4\u8d77\u6765\uff0c\u5e76\u4f7f\u7528\u53cd\u659c\u6760<code><\/code>\u52a0\u4e0b\u5212\u7ebf<code>_<\/code>\u6765\u8868\u793a\u4e0b\u6807\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.xlabel(&#39;X\u8f74\u6807\u7b7e $X_1$&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74\u6807\u7b7e $Y_2$&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7ed3\u5408\u5b9e\u9645\u5e94\u7528<\/strong><\/li>\n<\/ol>\n<p><p>\u5c06\u4e0a\u8ff0\u65b9\u6cd5\u5e94\u7528\u5230\u7ed8\u56fe\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u5728\u56fe\u5f62\u7684\u8f74\u6807\u7b7e\u4e2d\u6dfb\u52a0\u4e0b\u6807\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/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>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<h2><strong>\u7ed8\u5236\u6570\u636e<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u6dfb\u52a0\u5e26\u6709\u4e0b\u6807\u7684\u6807\u7b7e\u548c\u6807\u9898<\/strong><\/h2>\n<p>plt.xlabel(&#39;\u65f6\u95f4 $t_1$&#39;)<\/p>\n<p>plt.ylabel(&#39;\u6d53\u5ea6 $C_2$&#39;)<\/p>\n<p>plt.title(&#39;\u5316\u5b66\u53cd\u5e94\u8fc7\u7a0b\u4e2d\u7684\u6d53\u5ea6\u53d8\u5316&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5728\u5176\u4ed6\u6587\u672c\u4e2d\u6dfb\u52a0\u4e0b\u6807<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u8f74\u6807\u7b7e\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5728\u5176\u4ed6\u6587\u672c\u5143\u7d20\u4e2d\u4f7f\u7528\u4e0b\u6807\uff0c\u4f8b\u5982\u56fe\u4f8b\u3001\u6587\u672c\u6ce8\u91ca\u7b49\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728\u56fe\u4f8b\u4e2d\u6dfb\u52a0\u4e0b\u6807\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/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>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<h2><strong>\u7ed8\u5236\u6570\u636e<\/strong><\/h2>\n<p>plt.plot(x, y1, label=&#39;\u6570\u636e $A_1$&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;\u6570\u636e $B_2$&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u56fe\u4f8b<\/strong><\/h2>\n<p>plt.legend()<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3\u548c\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Matplotlib\u7ed8\u5236\u56fe\u5f62\u5e76\u6807\u8bb0\u4e0b\u6807\u65f6\uff0c\u6709\u51e0\u4e2a\u6ce8\u610f\u4e8b\u9879\u9700\u8981\u7262\u8bb0\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6570\u5b66\u8868\u8fbe\u5f0f\u8bed\u6cd5<\/strong>\uff1a\u786e\u4fdd\u5728\u4e0b\u6807\u8868\u8fbe\u5f0f\u524d\u540e\u4f7f\u7528<code>$<\/code>\u7b26\u53f7\u5305\u56f4\uff0c\u5e76\u4f7f\u7528\u53cd\u659c\u6760<code><\/code>\u548c\u4e0b\u5212\u7ebf<code>_<\/code>\u6b63\u786e\u6807\u8bb0\u4e0b\u6807\u3002<\/li>\n<li><strong>\u5b57\u4f53\u548c\u683c\u5f0f<\/strong>\uff1a\u5728\u9700\u8981\u5f3a\u8c03\u6216\u66f4\u6539\u5b57\u4f53\u65f6\uff0c\u53ef\u4ee5\u7ed3\u5408Matplotlib\u7684\u6587\u672c\u5c5e\u6027\u8bbe\u7f6e\u3002<\/li>\n<li><strong>\u663e\u793a\u6b63\u786e\u7684\u683c\u5f0f<\/strong>\uff1a\u6709\u4e9b\u60c5\u51b5\u4e0b\uff0c\u7279\u522b\u662f\u6d89\u53ca\u590d\u6742\u8868\u8fbe\u5f0f\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u8c03\u6574\u4e0b\u6807\u548c\u4e0a\u6807\u7684\u4f4d\u7f6e\uff0c\u4ee5\u786e\u4fdd\u5176\u5728\u56fe\u5f62\u4e2d\u7684\u53ef\u8bfb\u6027\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\u548c\u793a\u4f8b\uff0c\u4f60\u5e94\u8be5\u80fd\u591f\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u7ed8\u5236\u56fe\u5f62\u5e76\u6210\u529f\u6dfb\u52a0\u4e0b\u6807\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u8c03\u6574\u6807\u7b7e\u548c\u56fe\u5f62\u8bbe\u7f6e\uff0c\u4ee5\u83b7\u5f97\u6700\u4f73\u663e\u793a\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7ed8\u56fe\u4e2d\u6dfb\u52a0\u4e0b\u6807\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u7ed8\u5236\u56fe\u5f62\u5e76\u6dfb\u52a0\u4e0b\u6807\u3002\u901a\u8fc7<code>plt.text()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u6307\u5b9a\u7684\u4f4d\u7f6e\u6dfb\u52a0\u6587\u672c\uff0c\u7ed3\u5408\u6570\u5b66\u8868\u8fbe\u5f0f\u683c\u5f0f\u6765\u5b9e\u73b0\u4e0b\u6807\u6548\u679c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>&#39;$A_{1}$&#39;<\/code>\u7684\u683c\u5f0f\u6765\u8868\u793a\u4e0b\u6807\u3002<\/p>\n<p><strong>\u4f7f\u7528\u54ea\u4e2a\u5e93\u7ed8\u56fe\u6700\u65b9\u4fbf\uff1f<\/strong><br \/>Matplotlib\u662f\u6700\u6d41\u884c\u7684Python\u7ed8\u56fe\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u5f62\u3002\u9664\u4e86Matplotlib\u5916\uff0cSeaborn\u548cPlotly\u4e5f\u5f88\u53d7\u6b22\u8fce\uff0c\u5c24\u5176\u5728\u6570\u636e\u53ef\u89c6\u5316\u548c\u4ea4\u4e92\u5f0f\u56fe\u8868\u65b9\u9762\u8868\u73b0\u51fa\u8272\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u56fe\u4f8b\u4e2d\u6dfb\u52a0\u4e0b\u6807\u6587\u672c\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Matplotlib\u7ed8\u5236\u56fe\u4f8b\u65f6\uff0c\u53ef\u4ee5\u540c\u6837\u4f7f\u7528\u6570\u5b66\u8868\u8fbe\u5f0f\u683c\u5f0f\u6765\u6dfb\u52a0\u4e0b\u6807\u3002\u901a\u8fc7\u5728\u56fe\u4f8b\u6807\u7b7e\u4e2d\u4f7f\u7528<code>&#39;$A_{1}$&#39;<\/code>\u683c\u5f0f\uff0c\u60a8\u53ef\u4ee5\u5728\u56fe\u4f8b\u4e2d\u76f4\u89c2\u5730\u663e\u793a\u4e0b\u6807\u4fe1\u606f\uff0c\u4ece\u800c\u4f7f\u56fe\u8868\u66f4\u52a0\u6e05\u6670\u6613\u61c2\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\u53ef\u4ee5\u53c2\u8003\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728Python\u4e2d\u7ed8\u56fe\u5e76\u6dfb\u52a0\u4e0b\u6807\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\nx = [1, 2, 3, 4]\ny = [10, 20, 25, 30]\n\nplt.plot(x, y, label=&#39;$y = A_{1}x$&#39;)  # \u6dfb\u52a0\u4e0b\u6807\nplt.xlabel(&#39;X\u8f74&#39;)\nplt.ylabel(&#39;Y\u8f74&#39;)\nplt.title(&#39;\u793a\u4f8b\u56fe&#39;)\nplt.legend()\nplt.show()\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u56fe\u4f8b\u4e2d\u5305\u542b\u4e86\u4e0b\u6807A1\u7684\u8868\u8fbe\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u7ed8\u5236\u56fe\u5f62\u5e76\u6dfb\u52a0\u4e0b\u6807\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3002\u4f7f\u7528Matplotlib\u5e93\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728\u6807 [&hellip;]","protected":false},"author":3,"featured_media":998663,"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\/998653"}],"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=998653"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998653\/revisions"}],"predecessor-version":[{"id":998668,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/998653\/revisions\/998668"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/998663"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=998653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=998653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=998653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}