{"id":1006707,"date":"2024-12-27T10:46:51","date_gmt":"2024-12-27T02:46:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1006707.html"},"modified":"2024-12-27T10:46:54","modified_gmt":"2024-12-27T02:46:54","slug":"python%e4%bd%9c%e5%9b%be%e5%a6%82%e4%bd%95%e5%8a%a0%e6%a8%aa%e7%ba%bf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1006707.html","title":{"rendered":"python\u4f5c\u56fe\u5982\u4f55\u52a0\u6a2a\u7ebf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082720\/77b43d9d-0a65-417a-9a68-f6efc9df6d0f.webp\" alt=\"python\u4f5c\u56fe\u5982\u4f55\u52a0\u6a2a\u7ebf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4f5c\u56fe\u65f6\u6dfb\u52a0\u6a2a\u7ebf\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528Matplotlib\u5e93\u7684<code>axhline()<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>plot()<\/code>\u51fd\u6570\u7ed8\u5236\u6c34\u5e73\u7ebf\u3001\u4ee5\u53ca\u901a\u8fc7<code>fill_between()<\/code>\u51fd\u6570\u521b\u5efa\u5e26\u6709\u586b\u5145\u7684\u6c34\u5e73\u5e26\u3002\u4f7f\u7528<code>axhline()<\/code>\u662f\u6700\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>axhline()<\/code>\u51fd\u6570\u7ed8\u5236\u6a2a\u7ebf\u65f6\uff0c\u53ea\u9700\u6307\u5b9a\u6a2a\u7ebf\u7684\u4f4d\u7f6e\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u8981\u5728y=0.5\u7684\u4f4d\u7f6e\u6dfb\u52a0\u4e00\u6761\u6a2a\u7ebf\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.axhline(y=0.5, color=&#39;r&#39;, linestyle=&#39;--&#39;)<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u7b80\u5355\u660e\u4e86\uff0c\u9002\u5408\u5728\u73b0\u6709\u56fe\u8868\u4e0a\u5feb\u901f\u6dfb\u52a0\u53c2\u8003\u7ebf\u3002\u5728\u5177\u4f53\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u56fe\u5f62\u5143\u7d20\uff0c\u5982\u6563\u70b9\u56fe\u3001\u67f1\u72b6\u56fe\u7b49\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u4f20\u8fbe\u6570\u636e\u7684\u542b\u4e49\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001MATPLOTLIB\u5e93\u7b80\u4ecb<\/h2>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u7528\u4e8e\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\uff0c\u8fd8\u662f\u590d\u6742\u76843D\u56fe\u5f62\uff0cMatplotlib\u90fd\u80fd\u80dc\u4efb\u3002\u7279\u522b\u662f\u5728\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u9886\u57df\uff0cMatplotlib\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h3>1.1 \u5b89\u88c5\u4e0e\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\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\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><h3>1.2 \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>Matplotlib\u7684\u57fa\u672c\u7ed8\u56fe\u903b\u8f91\u662f\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61\uff0c\u7136\u540e\u5411\u5176\u4e2d\u6dfb\u52a0\u5404\u79cd\u5143\u7d20\uff0c\u5982\u7ebf\u6761\u3001\u6587\u5b57\u3001\u6807\u7b7e\u7b49\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6298\u7ebf\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;\u7b80\u5355\u6298\u7ebf\u56fe&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b\u6807\u9898\u548c\u5750\u6807\u8f74\u6807\u7b7e\u7684\u6298\u7ebf\u56fe\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528AXHLINE()\u51fd\u6570\u7ed8\u5236\u6a2a\u7ebf<\/h2>\n<\/p>\n<p><p><code>axhline()<\/code>\u51fd\u6570\u662fMatplotlib\u4e2d\u7528\u4e8e\u7ed8\u5236\u6c34\u5e73\u7ebf\u7684\u4e00\u4e2a\u4fbf\u6377\u5de5\u5177\u3002\u5b83\u5141\u8bb8\u7528\u6237\u6307\u5b9a\u6c34\u5e73\u7ebf\u7684\u4f4d\u7f6e\u3001\u989c\u8272\u3001\u7ebf\u578b\u7b49\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h3>2.1 \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u8981\u5728\u56fe\u4e2d\u6dfb\u52a0\u4e00\u6761\u6c34\u5e73\u7ebf\uff0c<code>axhline()<\/code>\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.axhline(y=0.5, color=&#39;r&#39;, linestyle=&#39;--&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>y<\/code>\u53c2\u6570\u6307\u5b9a\u6c34\u5e73\u7ebf\u7684\u4f4d\u7f6e\uff1b<\/li>\n<li><code>color<\/code>\u53c2\u6570\u5b9a\u4e49\u7ebf\u6761\u7684\u989c\u8272\uff1b<\/li>\n<li><code>linestyle<\/code>\u53c2\u6570\u7528\u4e8e\u8bbe\u5b9a\u7ebf\u578b\uff0c\u5982\u5b9e\u7ebf<code>&#39;-&#39;<\/code>\u3001\u865a\u7ebf<code>&#39;--&#39;<\/code>\u7b49\u3002<\/li>\n<\/ul>\n<p><h3>2.2 \u5b9e\u9645\u5e94\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u6298\u7ebf\u56fe\uff0c\u5e76\u5e0c\u671b\u5728y=5\u7684\u4f4d\u7f6e\u6dfb\u52a0\u4e00\u6761\u6c34\u5e73\u7ebf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 4, 6, 8, 10]<\/p>\n<p>plt.plot(x, y, label=&#39;\u6570\u636e\u7ebf&#39;)<\/p>\n<p>plt.axhline(y=5, color=&#39;r&#39;, linestyle=&#39;--&#39;, label=&#39;y=5&#39;)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;\u6dfb\u52a0\u6c34\u5e73\u7ebf\u7684\u6298\u7ebf\u56fe&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4e0d\u4ec5\u7ed8\u5236\u4e86\u4e00\u4e2a\u57fa\u672c\u7684\u6298\u7ebf\u56fe\uff0c\u8fd8\u901a\u8fc7<code>axhline()<\/code>\u51fd\u6570\u5728y=5\u7684\u4f4d\u7f6e\u6dfb\u52a0\u4e86\u4e00\u6761\u7ea2\u8272\u865a\u7ebf\uff0c\u5e76\u4e3a\u5176\u6dfb\u52a0\u4e86\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528PLOT()\u51fd\u6570\u7ed8\u5236\u6c34\u5e73\u7ebf<\/h2>\n<\/p>\n<p><p>\u9664\u4e86<code>axhline()<\/code>\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5229\u7528<code>plot()<\/code>\u51fd\u6570\u7ed8\u5236\u6c34\u5e73\u7ebf\u3002\u8fd9\u79cd\u65b9\u6cd5\u7684\u7075\u6d3b\u6027\u66f4\u9ad8\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u6211\u4eec\u5728\u6c34\u5e73\u7ebf\u7684\u4e24\u7aef\u6307\u5b9a\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h3>3.1 \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7<code>plot()<\/code>\u51fd\u6570\u7ed8\u5236\u6c34\u5e73\u7ebf\u65f6\uff0c\u6211\u4eec\u9700\u8981\u6307\u5b9ax\u8f74\u7684\u8303\u56f4\u548cy\u8f74\u7684\u4f4d\u7f6e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.plot([xmin, xmax], [y, y], color=&#39;g&#39;, linestyle=&#39;-&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>[xmin, xmax]<\/code>\u5b9a\u4e49\u4e86\u6c34\u5e73\u7ebf\u5728x\u8f74\u4e0a\u7684\u8303\u56f4\uff1b<\/li>\n<li><code>[y, y]<\/code>\u786e\u4fdd\u7ebf\u6761\u662f\u6c34\u5e73\u7684\uff1b<\/li>\n<li><code>color<\/code>\u548c<code>linestyle<\/code>\u53c2\u6570\u7528\u4e8e\u81ea\u5b9a\u4e49\u7ebf\u6761\u7684\u5916\u89c2\u3002<\/li>\n<\/ul>\n<p><h3>3.2 \u5b9e\u9645\u5e94\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>plot()<\/code>\u51fd\u6570\u7ed8\u5236\u6c34\u5e73\u7ebf\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [3, 3, 3, 3, 3]<\/p>\n<p>plt.plot(x, y, label=&#39;\u6c34\u5e73\u7ebf&#39;, color=&#39;b&#39;)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;\u4f7f\u7528plot()\u7ed8\u5236\u6c34\u5e73\u7ebf&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6c34\u5e73\u7ebf\u7684y\u503c\u4e3a3\uff0c\u5e76\u4f7f\u7528\u84dd\u8272\u7ed8\u5236\u3002\u901a\u8fc7<code>plot()<\/code>\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u63a7\u5236\u6c34\u5e73\u7ebf\u7684\u8d77\u70b9\u548c\u7ec8\u70b9\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u4f7f\u7528FILL_BETWEEN()\u51fd\u6570\u521b\u5efa\u6c34\u5e73\u5e26<\/h2>\n<\/p>\n<p><p><code>fill_between()<\/code>\u51fd\u6570\u4e0d\u4ec5\u7528\u4e8e\u7ed8\u5236\u7b80\u5355\u7684\u6c34\u5e73\u7ebf\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u5e26\u6709\u586b\u5145\u7684\u6c34\u5e73\u5e26\u3002\u8fd9\u5728\u9700\u8981\u5f3a\u8c03\u67d0\u4e00\u533a\u57df\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>4.1 \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p><code>fill_between()<\/code>\u51fd\u6570\u7684\u57fa\u672c\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.fill_between(x, y1, y2, color=&#39;lightblue&#39;, alpha=0.5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>x<\/code>\u5b9a\u4e49\u4e86\u586b\u5145\u533a\u57df\u7684x\u8f74\u8303\u56f4\uff1b<\/li>\n<li><code>y1<\/code>\u548c<code>y2<\/code>\u5b9a\u4e49\u4e86\u586b\u5145\u533a\u57df\u7684\u4e0a\u4e0b\u8fb9\u754c\uff1b<\/li>\n<li><code>color<\/code>\u7528\u4e8e\u8bbe\u7f6e\u586b\u5145\u989c\u8272\uff1b<\/li>\n<li><code>alpha<\/code>\u63a7\u5236\u586b\u5145\u989c\u8272\u7684\u900f\u660e\u5ea6\u3002<\/li>\n<\/ul>\n<p><h3>4.2 \u5b9e\u9645\u5e94\u7528\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6570\u636e\u96c6\uff0c\u5e76\u5e0c\u671b\u5728y=2\u548cy=4\u4e4b\u95f4\u586b\u5145\u989c\u8272\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>x = np.arange(0, 10, 0.1)<\/p>\n<p>y1 = np.sin(x)<\/p>\n<p>y2 = np.cos(x)<\/p>\n<p>plt.plot(x, y1, label=&#39;sin(x)&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;cos(x)&#39;)<\/p>\n<p>plt.fill_between(x, 0.2, 0.4, color=&#39;lightgray&#39;, alpha=0.5, label=&#39;\u586b\u5145\u533a\u57df&#39;)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;\u4f7f\u7528fill_between()\u521b\u5efa\u6c34\u5e73\u5e26&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>fill_between()<\/code>\u51fd\u6570\u7528\u4e8e\u5728y=0.2\u548cy=0.4\u4e4b\u95f4\u521b\u5efa\u4e00\u4e2a\u7070\u8272\u7684\u586b\u5145\u533a\u57df\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u8bbe\u7f6e\u6a2a\u7ebf\u7684\u6837\u5f0f\u548c\u5c5e\u6027<\/h2>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u6c34\u5e73\u7ebf\u65f6\uff0c\u8bbe\u7f6e\u7ebf\u6761\u7684\u6837\u5f0f\u548c\u5c5e\u6027\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u4f20\u8fbe\u4fe1\u606f\u3002Matplotlib\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u9009\u9879\uff0c\u5141\u8bb8\u7528\u6237\u81ea\u5b9a\u4e49\u7ebf\u6761\u7684\u989c\u8272\u3001\u5bbd\u5ea6\u3001\u6837\u5f0f\u7b49\u3002<\/p>\n<\/p>\n<p><h3>5.1 \u989c\u8272\u548c\u6837\u5f0f<\/h3>\n<\/p>\n<p><p>Matplotlib\u652f\u6301\u591a\u79cd\u989c\u8272\u548c\u7ebf\u578b\uff0c\u53ef\u4ee5\u901a\u8fc7<code>color<\/code>\u548c<code>linestyle<\/code>\u53c2\u6570\u8fdb\u884c\u8bbe\u7f6e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.axhline(y=0.5, color=&#39;red&#39;, linestyle=&#39;-.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>color<\/code>\u53ef\u4ee5\u662f\u989c\u8272\u540d\u79f0\uff08\u5982&#39;red&#39;\uff09\u3001\u5341\u516d\u8fdb\u5236\u4ee3\u7801\uff08\u5982&#39;#FF0000&#39;\uff09\u6216\u7070\u5ea6\u503c\uff08\u5982&#39;0.5&#39;\uff09\uff1b<\/li>\n<li><code>linestyle<\/code>\u652f\u6301\u591a\u79cd\u7ebf\u578b\uff0c\u5305\u62ec\u5b9e\u7ebf<code>&#39;-&#39;<\/code>\u3001\u865a\u7ebf<code>&#39;--&#39;<\/code>\u3001\u70b9\u7ebf<code>&#39;:&#39;<\/code>\u3001\u70b9\u5212\u7ebf<code>&#39;-.&#39;<\/code>\u7b49\u3002<\/li>\n<\/ul>\n<p><h3>5.2 \u7ebf\u5bbd\u548c\u900f\u660e\u5ea6<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u989c\u8272\u548c\u6837\u5f0f\uff0c\u7ebf\u5bbd\u548c\u900f\u660e\u5ea6\u4e5f\u662f\u5e38\u7528\u7684\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.axhline(y=0.5, linewidth=2, alpha=0.7)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><code>linewidth<\/code>\u7528\u4e8e\u8bbe\u7f6e\u7ebf\u6761\u7684\u5bbd\u5ea6\uff0c\u9ed8\u8ba4\u4e3a1\uff1b<\/li>\n<li><code>alpha<\/code>\u7528\u4e8e\u8bbe\u7f6e\u7ebf\u6761\u7684\u900f\u660e\u5ea6\uff0c\u53d6\u503c\u8303\u56f4\u4e3a0\u52301\u3002<\/li>\n<\/ul>\n<p><h3>5.3 \u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u8bbe\u7f6e\u6c34\u5e73\u7ebf\u7684\u591a\u79cd\u5c5e\u6027\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [4, 7, 2, 9, 6]<\/p>\n<p>plt.plot(x, y, label=&#39;\u6570\u636e&#39;)<\/p>\n<p>plt.axhline(y=5, color=&#39;purple&#39;, linestyle=&#39;--&#39;, linewidth=2, alpha=0.8, label=&#39;\u53c2\u8003\u7ebf&#39;)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;\u8bbe\u7f6e\u6c34\u5e73\u7ebf\u7684\u6837\u5f0f\u548c\u5c5e\u6027&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728y=5\u7684\u4f4d\u7f6e\u6dfb\u52a0\u4e86\u4e00\u6761\u7d2b\u8272\u7684\u865a\u7ebf\uff0c\u5e76\u8bbe\u7f6e\u4e86\u7ebf\u5bbd\u548c\u900f\u660e\u5ea6\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6dfb\u52a0\u6c34\u5e73\u7ebf\u65f6\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\uff0c\u4ee5\u786e\u4fdd\u56fe\u5f62\u7684\u6b63\u786e\u6027\u548c\u7f8e\u89c2\u6027\u3002<\/p>\n<\/p>\n<p><h3>6.1 \u56fe\u5f62\u6bd4\u4f8b\u548c\u8f74\u8303\u56f4<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u6c34\u5e73\u7ebf\u4e4b\u524d\uff0c\u786e\u4fdd\u56fe\u5f62\u7684\u6bd4\u4f8b\u548c\u8f74\u8303\u56f4\u9002\u5f53\u8bbe\u7f6e\u3002\u53ef\u4ee5\u4f7f\u7528<code>plt.xlim()<\/code>\u548c<code>plt.ylim()<\/code>\u6765\u624b\u52a8\u8c03\u6574\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.xlim(0, 10)<\/p>\n<p>plt.ylim(0, 10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6.2 \u591a\u4e2a\u6a2a\u7ebf\u7684\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u540c\u4e00\u56fe\u5f62\u4e0a\u6dfb\u52a0\u591a\u4e2a\u6c34\u5e73\u7ebf\u65f6\uff0c\u786e\u4fdd\u5b83\u4eec\u7684\u989c\u8272\u548c\u6837\u5f0f\u8db3\u591f\u533a\u522b\uff0c\u4ee5\u514d\u9020\u6210\u6df7\u6dc6\u3002\u4f7f\u7528\u4e0d\u540c\u7684\u989c\u8272\u548c\u7ebf\u578b\u7ec4\u5408\u53ef\u4ee5\u6709\u6548\u533a\u5206\u8fd9\u4e9b\u7ebf\u6761\u3002<\/p>\n<\/p>\n<p><h3>6.3 \u4e0e\u5176\u4ed6\u56fe\u5f62\u5143\u7d20\u7684\u7ed3\u5408<\/h3>\n<\/p>\n<p><p>\u6c34\u5e73\u7ebf\u901a\u5e38\u7528\u4e8e\u4e0e\u5176\u4ed6\u56fe\u5f62\u5143\u7d20\u7ed3\u5408\u4f7f\u7528\uff0c\u5982\u6563\u70b9\u56fe\u3001\u67f1\u72b6\u56fe\u7b49\u3002\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u786e\u4fdd\u6c34\u5e73\u7ebf\u4e0d\u906e\u6321\u91cd\u8981\u7684\u6570\u636e\u70b9\u6216\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cMatplotlib\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u7528\u4e8e\u7ed8\u5236\u6c34\u5e73\u7ebf\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u53c2\u8003\u7ebf\uff0c\u8fd8\u662f\u590d\u6742\u7684\u5e26\u6709\u586b\u5145\u7684\u533a\u57df\uff0c\u7528\u6237\u90fd\u53ef\u4ee5\u901a\u8fc7\u5408\u7406\u8bbe\u7f6e\u7ebf\u6761\u7684\u5c5e\u6027\u6765\u6ee1\u8db3\u4e0d\u540c\u7684\u7ed8\u56fe\u9700\u6c42\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u8bfb\u8005\u80fd\u591f\u66f4\u597d\u5730\u5229\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u521b\u5efa\u51fa\u6e05\u6670\u3001\u7f8e\u89c2\u7684\u6570\u636e\u53ef\u89c6\u5316\u56fe\u8868\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\u6c34\u5e73\u7ebf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u56fe\u4e2d\u6dfb\u52a0\u6c34\u5e73\u7ebf\u3002\u53ef\u4ee5\u4f7f\u7528<code>plt.axhline()<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u8be5\u51fd\u6570\u5141\u8bb8\u60a8\u6307\u5b9a\u6c34\u5e73\u7ebf\u7684\u4f4d\u7f6e\u3001\u989c\u8272\u3001\u7ebf\u578b\u548c\u900f\u660e\u5ea6\u7b49\u53c2\u6570\u3002\u4f8b\u5982\uff0c<code>plt.axhline(y=0, color=&#39;r&#39;, linestyle=&#39;--&#39;)<\/code>\u5c06\u5728y=0\u7684\u4f4d\u7f6e\u6dfb\u52a0\u4e00\u6761\u7ea2\u8272\u865a\u7ebf\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5728Python\u7684\u54ea\u4e9b\u7ed8\u56fe\u5e93\u4e2d\u6dfb\u52a0\u6a2a\u7ebf\uff1f<\/strong><br \/>\u9664\u4e86Matplotlib\uff0cPython\u8fd8\u6709\u5176\u4ed6\u7ed8\u56fe\u5e93\u5982Seaborn\u3001Plotly\u548cBokeh\u7b49\u3002\u5728Seaborn\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684\u529f\u80fd\u6765\u6dfb\u52a0\u6a2a\u7ebf\uff1b\u5728Plotly\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>add_shape<\/code>\u529f\u80fd\u6765\u5b9e\u73b0\uff1bBokeh\u5219\u53ef\u4ee5\u4f7f\u7528<code>hline<\/code>\u65b9\u6cd5\u6765\u6dfb\u52a0\u6c34\u5e73\u7ebf\u3002\u6bcf\u79cd\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u8bed\u6cd5\u548c\u529f\u80fd\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u3002<\/p>\n<p><strong>\u6dfb\u52a0\u6a2a\u7ebf\u5bf9\u6570\u636e\u53ef\u89c6\u5316\u6709\u4ec0\u4e48\u5e2e\u52a9\uff1f<\/strong><br \/>\u5728\u6570\u636e\u53ef\u89c6\u5316\u4e2d\uff0c\u6dfb\u52a0\u6a2a\u7ebf\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u8d8b\u52bf\u548c\u91cd\u8981\u7684\u53c2\u8003\u503c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u6c34\u5e73\u7ebf\u6765\u8868\u793a\u5e73\u5747\u503c\u3001\u57fa\u51c6\u7ebf\u6216\u5176\u4ed6\u5173\u952e\u6307\u6807\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u6e05\u6670\u5730\u5c55\u793a\u6570\u636e\uff0c\u8fd8\u80fd\u5e2e\u52a9\u89c2\u4f17\u5feb\u901f\u8bc6\u522b\u51fa\u6570\u636e\u7684\u53d8\u5316\u548c\u91cd\u8981\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f5c\u56fe\u65f6\u6dfb\u52a0\u6a2a\u7ebf\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528Matplotlib\u5e93\u7684axhline()\u51fd\u6570\u3001\u4f7f\u7528p [&hellip;]","protected":false},"author":3,"featured_media":1006717,"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\/1006707"}],"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=1006707"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1006707\/revisions"}],"predecessor-version":[{"id":1006719,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1006707\/revisions\/1006719"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1006717"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1006707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1006707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1006707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}