{"id":1092509,"date":"2025-01-08T14:21:51","date_gmt":"2025-01-08T06:21:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1092509.html"},"modified":"2025-01-08T14:21:53","modified_gmt":"2025-01-08T06:21:53","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e5%9b%be%e4%b8%ad%e7%94%bb%e7%ab%96%e7%9b%b4%e7%9a%84%e7%ba%bf-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1092509.html","title":{"rendered":"python\u5982\u4f55\u5728\u56fe\u4e2d\u753b\u7ad6\u76f4\u7684\u7ebf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205512\/7decda02-d3f8-493a-ac32-791fd699b1f5.webp\" alt=\"python\u5982\u4f55\u5728\u56fe\u4e2d\u753b\u7ad6\u76f4\u7684\u7ebf\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5728\u56fe\u4e2d\u753b\u7ad6\u76f4\u7684\u7ebf\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Matplotlib\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5173\u952e\u6b65\u9aa4\uff1a\u9996\u5148\uff0c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff1b\u5176\u6b21\uff0c\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61\uff1b\u6700\u540e\uff0c\u4f7f\u7528 <code>axvline<\/code> \u6216 <code>vlines<\/code> \u51fd\u6570\u7ed8\u5236\u7ad6\u76f4\u7ebf\u3002<strong>\u5e38\u7528\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528axvline\u51fd\u6570\u3001\u4f7f\u7528vlines\u51fd\u6570\u3001\u4f7f\u7528plot\u51fd\u6570\u3001\u4e3a\u7ad6\u76f4\u7ebf\u6dfb\u52a0\u6837\u5f0f<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528axvline\u51fd\u6570\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>axvline<\/code> \u662f Matplotlib \u5e93\u4e2d\u7528\u4e8e\u5728\u7279\u5b9a x \u5750\u6807\u5904\u7ed8\u5236\u7ad6\u76f4\u7ebf\u7684\u51fd\u6570\u3002\u5b83\u63a5\u53d7\u591a\u4e2a\u53c2\u6570\uff0c\u4f8b\u5982 <code>x<\/code>\uff0c\u8868\u793a\u7ad6\u76f4\u7ebf\u6240\u5728\u7684 x \u5750\u6807\uff1b<code>color<\/code>\uff0c\u8868\u793a\u7ebf\u6761\u7684\u989c\u8272\uff1b<code>linestyle<\/code>\uff0c\u8868\u793a\u7ebf\u6761\u7684\u6837\u5f0f\uff1b<code>linewidth<\/code>\uff0c\u8868\u793a\u7ebf\u6761\u7684\u5bbd\u5ea6\u7b49\u3002\u901a\u8fc7\u8bbe\u7f6e\u8fd9\u4e9b\u53c2\u6570\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u7ad6\u76f4\u7ebf\u7684\u6837\u5f0f\u3002\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 <code>axvline<\/code> \u51fd\u6570\u5728\u56fe\u4e2d\u7ed8\u5236\u4e00\u6761\u7ad6\u76f4\u7ebf\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>x = [0, 1, 2, 3, 4, 5]<\/p>\n<p>y = [0, 1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u4f7f\u7528 axvline \u5728 x=2 \u5904\u7ed8\u5236\u4e00\u6761\u7ad6\u76f4\u7ebf<\/strong><\/h2>\n<p>plt.axvline(x=2, color=&#39;r&#39;, linestyle=&#39;--&#39;, linewidth=2)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u793a\u4f8b\u4ee3\u7801\u5728 x=2 \u7684\u4f4d\u7f6e\u7ed8\u5236\u4e86\u4e00\u6761\u7ea2\u8272\u7684\u3001\u865a\u7ebf\u6837\u5f0f\u7684\u7ad6\u76f4\u7ebf\uff0c\u7ebf\u5bbd\u4e3a 2\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528axvline\u51fd\u6570<\/p>\n<\/p>\n<p><p>1\u3001\u57fa\u672c\u7528\u6cd5<\/p>\n<p><code>axvline<\/code> \u51fd\u6570\u662f Matplotlib \u5e93\u4e2d\u4e00\u4e2a\u5e38\u7528\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u5728\u7279\u5b9a x \u5750\u6807\u5904\u7ed8\u5236\u7ad6\u76f4\u7ebf\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u662f <code>plt.axvline(x, color, linestyle, linewidth)<\/code>\u3002\u5176\u4e2d\uff0c<code>x<\/code> \u662f\u7ad6\u76f4\u7ebf\u7684\u4f4d\u7f6e\uff0c<code>color<\/code> \u662f\u7ebf\u6761\u7684\u989c\u8272\uff0c<code>linestyle<\/code> \u662f\u7ebf\u6761\u7684\u6837\u5f0f\uff0c<code>linewidth<\/code> \u662f\u7ebf\u6761\u7684\u5bbd\u5ea6\u3002\u901a\u8fc7\u8bbe\u7f6e\u8fd9\u4e9b\u53c2\u6570\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u7ad6\u76f4\u7ebf\u7684\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>x = [0, 1, 2, 3, 4, 5]<\/p>\n<p>y = [0, 1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u4f7f\u7528 axvline \u5728 x=2 \u5904\u7ed8\u5236\u4e00\u6761\u7ad6\u76f4\u7ebf<\/strong><\/h2>\n<p>plt.axvline(x=2, color=&#39;r&#39;, linestyle=&#39;--&#39;, linewidth=2)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/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\u7b80\u5355\u7684\u6298\u7ebf\u56fe\uff0c\u7136\u540e\u4f7f\u7528 <code>plt.axvline(x=2, color=&#39;r&#39;, linestyle=&#39;--&#39;, linewidth=2)<\/code> \u5728 x=2 \u5904\u7ed8\u5236\u4e86\u4e00\u6761\u7ad6\u76f4\u7ebf\u3002\u7ad6\u76f4\u7ebf\u7684\u989c\u8272\u4e3a\u7ea2\u8272\uff0c\u6837\u5f0f\u4e3a\u865a\u7ebf\uff0c\u7ebf\u5bbd\u4e3a 2\u3002<\/p>\n<\/p>\n<p><p>2\u3001\u591a\u6761\u7ad6\u76f4\u7ebf<\/p>\n<p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5728\u56fe\u4e2d\u7ed8\u5236\u591a\u6761\u7ad6\u76f4\u7ebf\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u591a\u6b21\u8c03\u7528 <code>axvline<\/code> \u51fd\u6570\uff0c\u6bcf\u6b21\u8bbe\u7f6e\u4e0d\u540c\u7684 x \u5750\u6807\u548c\u6837\u5f0f\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>x = [0, 1, 2, 3, 4, 5]<\/p>\n<p>y = [0, 1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u4f7f\u7528 axvline \u7ed8\u5236\u591a\u6761\u7ad6\u76f4\u7ebf<\/strong><\/h2>\n<p>plt.axvline(x=1, color=&#39;r&#39;, linestyle=&#39;--&#39;, linewidth=2)<\/p>\n<p>plt.axvline(x=3, color=&#39;g&#39;, linestyle=&#39;:&#39;, linewidth=2)<\/p>\n<p>plt.axvline(x=4, color=&#39;b&#39;, linestyle=&#39;-&#39;, linewidth=2)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728 x=1\u3001x=3 \u548c x=4 \u5904\u5206\u522b\u7ed8\u5236\u4e86\u7ea2\u8272\u865a\u7ebf\u3001\u7eff\u8272\u70b9\u7ebf\u548c\u84dd\u8272\u5b9e\u7ebf\u3002\u901a\u8fc7\u591a\u6b21\u8c03\u7528 <code>axvline<\/code> \u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u56fe\u4e2d\u6dfb\u52a0\u591a\u6761\u7ad6\u76f4\u7ebf\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528vlines\u51fd\u6570<\/p>\n<\/p>\n<p><p>1\u3001\u57fa\u672c\u7528\u6cd5<\/p>\n<p><code>vlines<\/code> \u51fd\u6570\u662f Matplotlib \u5e93\u4e2d\u53e6\u4e00\u4e2a\u5e38\u7528\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u5728\u7279\u5b9a x \u5750\u6807\u5904\u7ed8\u5236\u7ad6\u76f4\u7ebf\u3002\u4e0e <code>axvline<\/code> \u51fd\u6570\u4e0d\u540c\uff0c<code>vlines<\/code> \u51fd\u6570\u9700\u8981\u6307\u5b9a\u7ad6\u76f4\u7ebf\u7684 y \u8303\u56f4\u3002\u5b83\u7684\u57fa\u672c\u8bed\u6cd5\u662f <code>plt.vlines(x, ymin, ymax, color, linestyle, linewidth)<\/code>\u3002\u5176\u4e2d\uff0c<code>x<\/code> \u662f\u7ad6\u76f4\u7ebf\u7684\u4f4d\u7f6e\uff0c<code>ymin<\/code> \u548c <code>ymax<\/code> \u662f\u7ad6\u76f4\u7ebf\u7684\u8d77\u59cb\u548c\u7ed3\u675f y \u5750\u6807\uff0c<code>color<\/code> \u662f\u7ebf\u6761\u7684\u989c\u8272\uff0c<code>linestyle<\/code> \u662f\u7ebf\u6761\u7684\u6837\u5f0f\uff0c<code>linewidth<\/code> \u662f\u7ebf\u6761\u7684\u5bbd\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>x = [0, 1, 2, 3, 4, 5]<\/p>\n<p>y = [0, 1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u4f7f\u7528 vlines \u5728 x=2 \u5904\u7ed8\u5236\u4e00\u6761\u7ad6\u76f4\u7ebf<\/strong><\/h2>\n<p>plt.vlines(x=2, ymin=0, ymax=25, color=&#39;r&#39;, linestyle=&#39;--&#39;, linewidth=2)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728 x=2 \u5904\u7ed8\u5236\u4e86\u4e00\u6761\u7ad6\u76f4\u7ebf\uff0c\u7ad6\u76f4\u7ebf\u7684 y \u8303\u56f4\u662f\u4ece 0 \u5230 25\u3002\u7ad6\u76f4\u7ebf\u7684\u989c\u8272\u4e3a\u7ea2\u8272\uff0c\u6837\u5f0f\u4e3a\u865a\u7ebf\uff0c\u7ebf\u5bbd\u4e3a 2\u3002<\/p>\n<\/p>\n<p><p>2\u3001\u591a\u6761\u7ad6\u76f4\u7ebf<\/p>\n<p>\u4e0e <code>axvline<\/code> \u51fd\u6570\u7c7b\u4f3c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>vlines<\/code> \u51fd\u6570\u7ed8\u5236\u591a\u6761\u7ad6\u76f4\u7ebf\u3002\u53ea\u9700\u591a\u6b21\u8c03\u7528 <code>vlines<\/code> \u51fd\u6570\uff0c\u5e76\u8bbe\u7f6e\u4e0d\u540c\u7684 x \u5750\u6807\u548c\u6837\u5f0f\u53c2\u6570\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>x = [0, 1, 2, 3, 4, 5]<\/p>\n<p>y = [0, 1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u4f7f\u7528 vlines \u7ed8\u5236\u591a\u6761\u7ad6\u76f4\u7ebf<\/strong><\/h2>\n<p>plt.vlines(x=[1, 3, 4], ymin=0, ymax=25, color=[&#39;r&#39;, &#39;g&#39;, &#39;b&#39;], linestyle=[&#39;--&#39;, &#39;:&#39;, &#39;-&#39;], linewidth=2)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728 x=1\u3001x=3 \u548c x=4 \u5904\u5206\u522b\u7ed8\u5236\u4e86\u7ea2\u8272\u865a\u7ebf\u3001\u7eff\u8272\u70b9\u7ebf\u548c\u84dd\u8272\u5b9e\u7ebf\u3002\u901a\u8fc7\u4e00\u6b21\u8c03\u7528 <code>vlines<\/code> \u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u56fe\u4e2d\u6dfb\u52a0\u591a\u6761\u7ad6\u76f4\u7ebf\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528plot\u51fd\u6570<\/p>\n<\/p>\n<p><p>1\u3001\u57fa\u672c\u7528\u6cd5<\/p>\n<p><code>plot<\/code> \u51fd\u6570\u662f Matplotlib \u5e93\u4e2d\u6700\u5e38\u7528\u7684\u51fd\u6570\u4e4b\u4e00\u3002\u867d\u7136 <code>plot<\/code> \u51fd\u6570\u4e3b\u8981\u7528\u4e8e\u7ed8\u5236\u6298\u7ebf\u56fe\uff0c\u4f46\u5b83\u4e5f\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u7ad6\u76f4\u7ebf\u3002\u53ea\u9700\u5728 <code>plot<\/code> \u51fd\u6570\u4e2d\u6307\u5b9a\u4e24\u4e2a\u70b9\u7684\u5750\u6807\uff0c\u5c31\u53ef\u4ee5\u7ed8\u5236\u4e00\u6761\u7ad6\u76f4\u7ebf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>x = [0, 1, 2, 3, 4, 5]<\/p>\n<p>y = [0, 1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u4f7f\u7528 plot \u5728 x=2 \u5904\u7ed8\u5236\u4e00\u6761\u7ad6\u76f4\u7ebf<\/strong><\/h2>\n<p>plt.plot([2, 2], [0, 25], color=&#39;r&#39;, linestyle=&#39;--&#39;, linewidth=2)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>plt.plot([2, 2], [0, 25], color=&#39;r&#39;, linestyle=&#39;--&#39;, linewidth=2)<\/code> \u5728 x=2 \u5904\u7ed8\u5236\u4e86\u4e00\u6761\u7ad6\u76f4\u7ebf\u3002\u7ad6\u76f4\u7ebf\u7684\u989c\u8272\u4e3a\u7ea2\u8272\uff0c\u6837\u5f0f\u4e3a\u865a\u7ebf\uff0c\u7ebf\u5bbd\u4e3a 2\u3002<\/p>\n<\/p>\n<p><p>2\u3001\u591a\u6761\u7ad6\u76f4\u7ebf<\/p>\n<p>\u540c\u6837\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>plot<\/code> \u51fd\u6570\u7ed8\u5236\u591a\u6761\u7ad6\u76f4\u7ebf\u3002\u53ea\u9700\u591a\u6b21\u8c03\u7528 <code>plot<\/code> \u51fd\u6570\uff0c\u5e76\u8bbe\u7f6e\u4e0d\u540c\u7684 x \u5750\u6807\u548c\u6837\u5f0f\u53c2\u6570\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>x = [0, 1, 2, 3, 4, 5]<\/p>\n<p>y = [0, 1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u4f7f\u7528 plot \u7ed8\u5236\u591a\u6761\u7ad6\u76f4\u7ebf<\/strong><\/h2>\n<p>plt.plot([1, 1], [0, 25], color=&#39;r&#39;, linestyle=&#39;--&#39;, linewidth=2)<\/p>\n<p>plt.plot([3, 3], [0, 25], color=&#39;g&#39;, linestyle=&#39;:&#39;, linewidth=2)<\/p>\n<p>plt.plot([4, 4], [0, 25], color=&#39;b&#39;, linestyle=&#39;-&#39;, linewidth=2)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728 x=1\u3001x=3 \u548c x=4 \u5904\u5206\u522b\u7ed8\u5236\u4e86\u7ea2\u8272\u865a\u7ebf\u3001\u7eff\u8272\u70b9\u7ebf\u548c\u84dd\u8272\u5b9e\u7ebf\u3002\u901a\u8fc7\u591a\u6b21\u8c03\u7528 <code>plot<\/code> \u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u56fe\u4e2d\u6dfb\u52a0\u591a\u6761\u7ad6\u76f4\u7ebf\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4e3a\u7ad6\u76f4\u7ebf\u6dfb\u52a0\u6837\u5f0f<\/p>\n<\/p>\n<p><p>1\u3001\u989c\u8272\u548c\u7ebf\u6761\u6837\u5f0f<\/p>\n<p>\u5728\u7ed8\u5236\u7ad6\u76f4\u7ebf\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e <code>color<\/code> \u548c <code>linestyle<\/code> \u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u7ad6\u76f4\u7ebf\u7684\u989c\u8272\u548c\u6837\u5f0f\u3002\u5e38\u89c1\u7684\u989c\u8272\u53c2\u6570\u6709 &#39;r&#39;\uff08\u7ea2\u8272\uff09\u3001&#39;g&#39;\uff08\u7eff\u8272\uff09\u3001&#39;b&#39;\uff08\u84dd\u8272\uff09\u7b49\u3002\u5e38\u89c1\u7684\u7ebf\u6761\u6837\u5f0f\u53c2\u6570\u6709 &#39;&#8211;&#39;\uff08\u865a\u7ebf\uff09\u3001&#39;:&#39;\uff08\u70b9\u7ebf\uff09\u3001&#39;-&#39;\uff08\u5b9e\u7ebf\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>x = [0, 1, 2, 3, 4, 5]<\/p>\n<p>y = [0, 1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u4f7f\u7528 axvline \u7ed8\u5236\u591a\u6761\u7ad6\u76f4\u7ebf\uff0c\u5e76\u8bbe\u7f6e\u989c\u8272\u548c\u7ebf\u6761\u6837\u5f0f<\/strong><\/h2>\n<p>plt.axvline(x=1, color=&#39;r&#39;, linestyle=&#39;--&#39;, linewidth=2)<\/p>\n<p>plt.axvline(x=3, color=&#39;g&#39;, linestyle=&#39;:&#39;, linewidth=2)<\/p>\n<p>plt.axvline(x=4, color=&#39;b&#39;, linestyle=&#39;-&#39;, linewidth=2)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728 x=1\u3001x=3 \u548c x=4 \u5904\u5206\u522b\u7ed8\u5236\u4e86\u7ea2\u8272\u865a\u7ebf\u3001\u7eff\u8272\u70b9\u7ebf\u548c\u84dd\u8272\u5b9e\u7ebf\u3002\u901a\u8fc7\u8bbe\u7f6e <code>color<\/code> \u548c <code>linestyle<\/code> \u53c2\u6570\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u7ad6\u76f4\u7ebf\u7684\u989c\u8272\u548c\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><p>2\u3001\u7ebf\u6761\u5bbd\u5ea6<\/p>\n<p>\u5728\u7ed8\u5236\u7ad6\u76f4\u7ebf\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e <code>linewidth<\/code> \u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u7ad6\u76f4\u7ebf\u7684\u5bbd\u5ea6\u3002<code>linewidth<\/code> \u53c2\u6570\u7684\u503c\u8d8a\u5927\uff0c\u7ebf\u6761\u8d8a\u7c97\uff1b\u503c\u8d8a\u5c0f\uff0c\u7ebf\u6761\u8d8a\u7ec6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>x = [0, 1, 2, 3, 4, 5]<\/p>\n<p>y = [0, 1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u4f7f\u7528 axvline \u7ed8\u5236\u591a\u6761\u7ad6\u76f4\u7ebf\uff0c\u5e76\u8bbe\u7f6e\u7ebf\u6761\u5bbd\u5ea6<\/strong><\/h2>\n<p>plt.axvline(x=1, color=&#39;r&#39;, linestyle=&#39;--&#39;, linewidth=1)<\/p>\n<p>plt.axvline(x=3, color=&#39;g&#39;, linestyle=&#39;:&#39;, linewidth=3)<\/p>\n<p>plt.axvline(x=4, color=&#39;b&#39;, linestyle=&#39;-&#39;, linewidth=5)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728 x=1\u3001x=3 \u548c x=4 \u5904\u5206\u522b\u7ed8\u5236\u4e86\u4e0d\u540c\u5bbd\u5ea6\u7684\u7ad6\u76f4\u7ebf\u3002\u901a\u8fc7\u8bbe\u7f6e <code>linewidth<\/code> \u53c2\u6570\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u7ad6\u76f4\u7ebf\u7684\u5bbd\u5ea6\u3002<\/p>\n<\/p>\n<p><p>3\u3001\u900f\u660e\u5ea6<\/p>\n<p>\u5728\u7ed8\u5236\u7ad6\u76f4\u7ebf\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e <code>alpha<\/code> \u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u7ad6\u76f4\u7ebf\u7684\u900f\u660e\u5ea6\u3002<code>alpha<\/code> \u53c2\u6570\u7684\u503c\u8303\u56f4\u662f 0 \u5230 1\uff0c\u503c\u8d8a\u5927\uff0c\u7ebf\u6761\u8d8a\u4e0d\u900f\u660e\uff1b\u503c\u8d8a\u5c0f\uff0c\u7ebf\u6761\u8d8a\u900f\u660e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>x = [0, 1, 2, 3, 4, 5]<\/p>\n<p>y = [0, 1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u4f7f\u7528 axvline \u7ed8\u5236\u591a\u6761\u7ad6\u76f4\u7ebf\uff0c\u5e76\u8bbe\u7f6e\u900f\u660e\u5ea6<\/strong><\/h2>\n<p>plt.axvline(x=1, color=&#39;r&#39;, linestyle=&#39;--&#39;, linewidth=2, alpha=0.5)<\/p>\n<p>plt.axvline(x=3, color=&#39;g&#39;, linestyle=&#39;:&#39;, linewidth=2, alpha=0.75)<\/p>\n<p>plt.axvline(x=4, color=&#39;b&#39;, linestyle=&#39;-&#39;, linewidth=2, alpha=1.0)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728 x=1\u3001x=3 \u548c x=4 \u5904\u5206\u522b\u7ed8\u5236\u4e86\u4e0d\u540c\u900f\u660e\u5ea6\u7684\u7ad6\u76f4\u7ebf\u3002\u901a\u8fc7\u8bbe\u7f6e <code>alpha<\/code> \u53c2\u6570\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u7ad6\u76f4\u7ebf\u7684\u900f\u660e\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684 <code>axvline<\/code>\u3001<code>vlines<\/code> \u548c <code>plot<\/code> \u51fd\u6570\u5728\u56fe\u4e2d\u7ed8\u5236\u7ad6\u76f4\u7ebf\u3002\u901a\u8fc7\u8bbe\u7f6e\u8fd9\u4e9b\u51fd\u6570\u7684\u53c2\u6570\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u7ad6\u76f4\u7ebf\u7684\u989c\u8272\u3001\u6837\u5f0f\u3001\u5bbd\u5ea6\u548c\u900f\u660e\u5ea6\u7b49\u5c5e\u6027\u3002\u65e0\u8bba\u662f\u7ed8\u5236\u5355\u6761\u7ad6\u76f4\u7ebf\u8fd8\u662f\u591a\u6761\u7ad6\u76f4\u7ebf\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u975e\u5e38\u65b9\u4fbf\u5b9e\u7528\u3002\u5e0c\u671b\u672c\u6559\u7a0b\u80fd\u5e2e\u52a9\u60a8\u638c\u63e1\u5728Python\u4e2d\u7ed8\u5236\u7ad6\u76f4\u7ebf\u7684\u65b9\u6cd5\u548c\u6280\u5de7\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5728\u56fe\u4e2d\u7ed8\u5236\u7ad6\u76f4\u7684\u7ebf\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u7ed8\u5236\u7ad6\u76f4\u7684\u7ebf\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u60a8\u5df2\u5b89\u88c5\u8be5\u5e93\u3002\u4f7f\u7528<code>plt.axvline()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u6dfb\u52a0\u7ad6\u76f4\u7ebf\u3002\u4f8b\u5982\uff0c<code>plt.axvline(x=\u4f4d\u7f6e, color=&#39;\u989c\u8272&#39;, linestyle=&#39;\u6837\u5f0f&#39;)<\/code>\uff0c\u5176\u4e2d<code>\u4f4d\u7f6e<\/code>\u662f\u60a8\u5e0c\u671b\u7ed8\u5236\u7ad6\u76f4\u7ebf\u7684x\u5750\u6807\uff0c<code>\u989c\u8272<\/code>\u548c<code>\u6837\u5f0f<\/code>\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u81ea\u5b9a\u4e49\u3002<\/p>\n<p><strong>\u5728\u7ed8\u56fe\u4e2d\u7ad6\u76f4\u7ebf\u7684\u989c\u8272\u548c\u6837\u5f0f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>color<\/code>\u548c<code>linestyle<\/code>\u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u7ad6\u76f4\u7ebf\u7684\u989c\u8272\u548c\u6837\u5f0f\u3002\u5e38\u89c1\u7684\u989c\u8272\u6709&#39;r&#39;\uff08\u7ea2\u8272\uff09\u3001&#39;g&#39;\uff08\u7eff\u8272\uff09\u3001&#39;b&#39;\uff08\u84dd\u8272\uff09\u7b49\uff0c\u800c\u6837\u5f0f\u53ef\u4ee5\u662f\u5b9e\u7ebf<code>&#39;-&#39;<\/code>\u3001\u865a\u7ebf<code>&#39;--&#39;<\/code>\u6216\u70b9\u7ebf<code>&#39;:&#39;<\/code>\u7b49\u3002\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u56fe\u8868\u7684\u4e3b\u9898\u6216\u9700\u6c42\u8fdb\u884c\u8c03\u6574\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u7ad6\u76f4\u7ebf\u65c1\u8fb9\u6dfb\u52a0\u6587\u672c\u6807\u7b7e\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u7ad6\u76f4\u7ebf\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>plt.text()<\/code>\u51fd\u6570\u6765\u6dfb\u52a0\u6587\u672c\u6807\u7b7e\u3002\u8be5\u51fd\u6570\u7684\u683c\u5f0f\u662f<code>plt.text(x, y, &#39;\u6587\u672c\u5185\u5bb9&#39;, fontsize=\u5927\u5c0f)<\/code>\uff0c\u5176\u4e2d<code>x<\/code>\u662f\u7ad6\u76f4\u7ebf\u7684\u4f4d\u7f6e\uff0c<code>y<\/code>\u662f\u6587\u672c\u7684y\u5750\u6807\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u8fd9\u4e9b\u53c2\u6570\u6765\u786e\u4fdd\u6587\u672c\u4e0e\u7ad6\u76f4\u7ebf\u7684\u5bf9\u9f50\u548c\u53ef\u8bfb\u6027\u3002\u901a\u8fc7\u8bbe\u7f6e<code>fontsize<\/code>\uff0c\u53ef\u4ee5\u589e\u5f3a\u6587\u672c\u7684\u53ef\u89c6\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5728\u56fe\u4e2d\u753b\u7ad6\u76f4\u7684\u7ebf\u3002\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Matplotlib\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5173\u952e\u6b65\u9aa4 [&hellip;]","protected":false},"author":3,"featured_media":1092516,"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\/1092509"}],"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=1092509"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1092509\/revisions"}],"predecessor-version":[{"id":1092519,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1092509\/revisions\/1092519"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1092516"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1092509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1092509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1092509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}