{"id":1118460,"date":"2025-01-08T18:35:33","date_gmt":"2025-01-08T10:35:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1118460.html"},"modified":"2025-01-08T18:35:36","modified_gmt":"2025-01-08T10:35:36","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8plot%e5%9b%be%e4%b8%ad%e6%a0%87%e6%b3%a8%e6%9f%90%e9%a2%9c%e8%89%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1118460.html","title":{"rendered":"python\u5982\u4f55\u5728plot\u56fe\u4e2d\u6807\u6ce8\u67d0\u989c\u8272"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081943\/31a3fc6c-6956-4253-b2c4-7833d6f8a96b.webp\" alt=\"python\u5982\u4f55\u5728plot\u56fe\u4e2d\u6807\u6ce8\u67d0\u989c\u8272\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u7684plot\u56fe\u4e2d\u6807\u6ce8\u67d0\u989c\u8272\uff0c\u53ef\u4ee5\u4f7f\u7528matplotlib\u5e93\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528legend\u3001annotate\u3001text\u7b49\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528legend\u51fd\u6570\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728matplotlib\u4e2d\uff0clegend\u51fd\u6570\u7528\u4e8e\u4e3a\u56fe\u4e2d\u7684\u5143\u7d20\u6dfb\u52a0\u56fe\u4f8b\uff0c\u901a\u8fc7\u8bbe\u7f6elabel\u53c2\u6570\uff0c\u53ef\u4ee5\u4e3a\u4e0d\u540c\u989c\u8272\u7684\u56fe\u5f62\u5143\u7d20\u6dfb\u52a0\u6807\u6ce8\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528legend\u51fd\u6570\u5728plot\u56fe\u4e2d\u6807\u6ce8\u67d0\u989c\u8272\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u4e0e\u5bfc\u5165\u5fc5\u8981\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528matplotlib\u4e4b\u524d\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u8be5\u5e93\u3002\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>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\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<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u521b\u5efa\u6570\u636e\u5e76\u7ed8\u5236\u56fe\u5f62<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u751f\u6210\u4e00\u4e9b\u793a\u4f8b\u6570\u636e\u5e76\u7ed8\u5236\u56fe\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6570\u636e<\/p>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y1 = np.sin(x)<\/p>\n<p>y2 = np.cos(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y1, label=&#39;sin(x)&#39;, color=&#39;blue&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;cos(x)&#39;, color=&#39;red&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u6dfb\u52a0\u56fe\u4f8b<\/p>\n<\/p>\n<p><p>\u4f7f\u7528legend\u51fd\u6570\u4e3a\u4e0d\u540c\u989c\u8272\u7684\u56fe\u5f62\u5143\u7d20\u6dfb\u52a0\u56fe\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u56fe\u4f8b<\/p>\n<p>plt.legend()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5c55\u793a\u56fe\u5f62<\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4f7f\u7528show\u51fd\u6570\u5c55\u793a\u56fe\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c55\u793a\u56fe\u5f62<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\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>y1 = np.sin(x)<\/p>\n<p>y2 = np.cos(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y1, label=&#39;sin(x)&#39;, color=&#39;blue&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;cos(x)&#39;, color=&#39;red&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u56fe\u4f8b<\/strong><\/h2>\n<p>plt.legend()<\/p>\n<h2><strong>\u5c55\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u662f\u4f7f\u7528legend\u51fd\u6570\u5728plot\u56fe\u4e2d\u6807\u6ce8\u67d0\u989c\u8272\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4ed6\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528annotate\u548ctext\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528annotate\u51fd\u6570\u6807\u6ce8\u989c\u8272<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528legend\u51fd\u6570\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528annotate\u51fd\u6570\u5728\u56fe\u4e2d\u6dfb\u52a0\u6807\u6ce8\u3002annotate\u51fd\u6570\u53ef\u4ee5\u5728\u6307\u5b9a\u4f4d\u7f6e\u6dfb\u52a0\u6ce8\u91ca\uff0c\u5e76\u53ef\u4ee5\u8bbe\u7f6e\u6ce8\u91ca\u7684\u989c\u8272\u3002<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u6570\u636e\u5e76\u7ed8\u5236\u56fe\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6570\u636e<\/p>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y1 = np.sin(x)<\/p>\n<p>y2 = np.cos(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y1, color=&#39;blue&#39;)<\/p>\n<p>plt.plot(x, y2, color=&#39;red&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528annotate\u51fd\u6570\u6dfb\u52a0\u6807\u6ce8\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u6807\u6ce8<\/p>\n<p>plt.annotate(&#39;sin(x)&#39;, xy=(5, 0), xytext=(5, 0.5),<\/p>\n<p>             arrowprops=dict(facecolor=&#39;black&#39;, shrink=0.05),<\/p>\n<p>             color=&#39;blue&#39;)<\/p>\n<p>plt.annotate(&#39;cos(x)&#39;, xy=(5, -1), xytext=(5, -0.5),<\/p>\n<p>             arrowprops=dict(facecolor=&#39;black&#39;, shrink=0.05),<\/p>\n<p>             color=&#39;red&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c55\u793a\u56fe\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c55\u793a\u56fe\u5f62<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\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>y1 = np.sin(x)<\/p>\n<p>y2 = np.cos(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y1, color=&#39;blue&#39;)<\/p>\n<p>plt.plot(x, y2, color=&#39;red&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u6ce8<\/strong><\/h2>\n<p>plt.annotate(&#39;sin(x)&#39;, xy=(5, 0), xytext=(5, 0.5),<\/p>\n<p>             arrowprops=dict(facecolor=&#39;black&#39;, shrink=0.05),<\/p>\n<p>             color=&#39;blue&#39;)<\/p>\n<p>plt.annotate(&#39;cos(x)&#39;, xy=(5, -1), xytext=(5, -0.5),<\/p>\n<p>             arrowprops=dict(facecolor=&#39;black&#39;, shrink=0.05),<\/p>\n<p>             color=&#39;red&#39;)<\/p>\n<h2><strong>\u5c55\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528text\u51fd\u6570\u6807\u6ce8\u989c\u8272<\/p>\n<\/p>\n<p><p>\u4f7f\u7528text\u51fd\u6570\u4e5f\u53ef\u4ee5\u5728\u6307\u5b9a\u4f4d\u7f6e\u6dfb\u52a0\u6587\u672c\u6807\u6ce8\uff0c\u5e76\u53ef\u4ee5\u8bbe\u7f6e\u6587\u672c\u7684\u989c\u8272\u3002<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u6570\u636e\u5e76\u7ed8\u5236\u56fe\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6570\u636e<\/p>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y1 = np.sin(x)<\/p>\n<p>y2 = np.cos(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y1, color=&#39;blue&#39;)<\/p>\n<p>plt.plot(x, y2, color=&#39;red&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528text\u51fd\u6570\u6dfb\u52a0\u6807\u6ce8\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u6807\u6ce8<\/p>\n<p>plt.text(2, 0.5, &#39;sin(x)&#39;, color=&#39;blue&#39;)<\/p>\n<p>plt.text(8, -0.5, &#39;cos(x)&#39;, color=&#39;red&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c55\u793a\u56fe\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c55\u793a\u56fe\u5f62<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\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>y1 = np.sin(x)<\/p>\n<p>y2 = np.cos(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y1, color=&#39;blue&#39;)<\/p>\n<p>plt.plot(x, y2, color=&#39;red&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u6ce8<\/strong><\/h2>\n<p>plt.text(2, 0.5, &#39;sin(x)&#39;, color=&#39;blue&#39;)<\/p>\n<p>plt.text(8, -0.5, &#39;cos(x)&#39;, color=&#39;red&#39;)<\/p>\n<h2><strong>\u5c55\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728Python\u7684plot\u56fe\u4e2d\u6807\u6ce8\u67d0\u989c\u8272\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u4f7f\u7528legend\u51fd\u6570\u6dfb\u52a0\u56fe\u4f8b<\/li>\n<li>\u4f7f\u7528annotate\u51fd\u6570\u6dfb\u52a0\u6ce8\u91ca<\/li>\n<li>\u4f7f\u7528text\u51fd\u6570\u6dfb\u52a0\u6587\u672c\u6807\u6ce8<\/li>\n<\/ul>\n<p><p>\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u6807\u6ce8\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0cmatplotlib\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728plot\u56fe\u4e2d\u6807\u6ce8\u989c\u8272\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u53ef\u4ee5\u4f7f\u56fe\u5f62\u66f4\u52a0\u6e05\u6670\u3001\u76f4\u89c2\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u4e86\u89e3\u5982\u4f55\u5728Python\u7684plot\u56fe\u4e2d\u6807\u6ce8\u67d0\u989c\u8272\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7684\u7ed8\u56fe\u4e2d\u6dfb\u52a0\u7279\u5b9a\u989c\u8272\u7684\u6807\u6ce8\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u56fe\u4e2d\u6dfb\u52a0\u7279\u5b9a\u989c\u8272\u7684\u6807\u6ce8\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>plt.text()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u6307\u5b9a\u6807\u6ce8\u7684\u5750\u6807\u548c\u989c\u8272\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5c06<code>color<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a\u60a8\u5e0c\u671b\u7684\u989c\u8272\u540d\u79f0\u6216\u5341\u516d\u8fdb\u5236\u8272\u503c\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6807\u6ce8\u5c06\u4e0e\u56fe\u5f62\u7684\u6574\u4f53\u8272\u8c03\u534f\u8c03\u4e00\u81f4\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u5728\u56fe\u4e2d\u7a81\u51fa\u663e\u793a\u7279\u5b9a\u989c\u8272\u7684\u533a\u57df\uff1f<\/strong><br \/>\u8981\u7a81\u51fa\u663e\u793a\u7279\u5b9a\u989c\u8272\u7684\u533a\u57df\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.fill_between()<\/code>\u6216<code>plt.axhspan()<\/code>\u7b49\u51fd\u6570\u3002\u8fd9\u4e9b\u51fd\u6570\u5141\u8bb8\u60a8\u5728\u6307\u5b9a\u7684x\u6216y\u8303\u56f4\u5185\u586b\u5145\u989c\u8272\uff0c\u4ece\u800c\u5438\u5f15\u89c2\u4f17\u7684\u6ce8\u610f\u529b\u3002\u9009\u62e9\u9002\u5f53\u7684\u989c\u8272\u53ef\u4ee5\u5e2e\u52a9\u5f3a\u8c03\u6570\u636e\u7684\u67d0\u4e9b\u7279\u5f81\uff0c\u6bd4\u5982\u91cd\u8981\u7684\u9608\u503c\u6216\u53d8\u5316\u8d8b\u52bf\u3002<\/p>\n<p><strong>\u5982\u4f55\u4e3a\u4e0d\u540c\u7684\u6570\u636e\u7cfb\u5217\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\u6807\u6ce8\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u591a\u6761\u6570\u636e\u7cfb\u5217\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4e3a\u6bcf\u4e2a\u7cfb\u5217\u6307\u5b9a\u4e0d\u540c\u7684\u989c\u8272\u6765\u5b9e\u73b0\u4e0d\u540c\u7684\u989c\u8272\u6807\u6ce8\u3002\u5728\u8c03\u7528\u7ed8\u56fe\u51fd\u6570\uff08\u5982<code>plt.plot()<\/code>\uff09\u65f6\uff0c\u4f7f\u7528<code>color<\/code>\u53c2\u6570\u4e3a\u6bcf\u6761\u7ebf\u6307\u5b9a\u4e0d\u540c\u7684\u989c\u8272\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u56fe\u4f8b\uff08<code>plt.legend()<\/code>\uff09\u53ef\u4ee5\u6709\u6548\u5730\u6807\u660e\u6bcf\u6761\u7ebf\u7684\u542b\u4e49\uff0c\u4f7f\u5f97\u56fe\u8868\u66f4\u5177\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u7684plot\u56fe\u4e2d\u6807\u6ce8\u67d0\u989c\u8272\uff0c\u53ef\u4ee5\u4f7f\u7528matplotlib\u5e93\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528legend\u3001ann [&hellip;]","protected":false},"author":3,"featured_media":1118466,"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\/1118460"}],"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=1118460"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1118460\/revisions"}],"predecessor-version":[{"id":1118467,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1118460\/revisions\/1118467"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1118466"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1118460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1118460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1118460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}