{"id":1101704,"date":"2025-01-08T15:52:15","date_gmt":"2025-01-08T07:52:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1101704.html"},"modified":"2025-01-08T15:52:17","modified_gmt":"2025-01-08T07:52:17","slug":"%e5%a6%82%e4%bd%95%e7%94%a8%e7%94%a8python%e7%94%bb%e4%b8%80%e6%ae%b5%e6%9b%b2%e7%ba%bf-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1101704.html","title":{"rendered":"\u5982\u4f55\u7528\u7528Python\u753b\u4e00\u6bb5\u66f2\u7ebf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064340\/f156301a-8f76-4830-9350-e32f6a3f39f8.webp\" alt=\"\u5982\u4f55\u7528\u7528Python\u753b\u4e00\u6bb5\u66f2\u7ebf\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u753b\u4e00\u6bb5\u66f2\u7ebf\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Matplotlib\u3001NumPy\u3001Seaborn\u7b49\u5e93\u3001\u901a\u8fc7plt.plot\u51fd\u6570\u7ed8\u5236\u3001\u8bbe\u7f6e\u6807\u9898\u3001\u6807\u7b7e\u548c\u56fe\u4f8b\u7b49\u3002<\/strong> \u5176\u4e2d\uff0cMatplotlib\u662f\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u753b\u4e00\u6bb5\u66f2\u7ebf\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u6240\u9700\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Matplotlib\u548cNumPy\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib numpy<\/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\u5165\u8fd9\u4e9b\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><h3>\u4e8c\u3001\u751f\u6210\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u753b\u4e00\u6bb5\u66f2\u7ebf\uff0c\u4f60\u9700\u8981\u4e00\u7ec4\u6570\u636e\u70b9\u3002\u4f60\u53ef\u4ee5\u624b\u52a8\u521b\u5efa\u8fd9\u4e9b\u6570\u636e\u70b9\uff0c\u6216\u8005\u4f7f\u7528NumPy\u5e93\u751f\u6210\u6570\u636e\u70b9\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528NumPy\u751f\u6210\u6570\u636e\u70b9\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = np.linspace(0, 10, 100)  # \u751f\u62100\u523010\u4e4b\u95f4\u7684100\u4e2a\u7b49\u95f4\u8ddd\u6570\u636e\u70b9<\/p>\n<p>y = np.sin(x)  # \u8ba1\u7b97\u6bcf\u4e2ax\u70b9\u5bf9\u5e94\u7684y\u503c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u751f\u6210\u4e86100\u4e2a\u57280\u523010\u4e4b\u95f4\u7b49\u95f4\u8ddd\u7684x\u503c\uff0c\u5e76\u4e14\u8ba1\u7b97\u4e86\u6bcf\u4e2ax\u503c\u5bf9\u5e94\u7684y\u503c\uff08\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u4e86sin\u51fd\u6570\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed8\u5236\u66f2\u7ebf<\/h3>\n<\/p>\n<p><p>\u73b0\u5728\u6211\u4eec\u6709\u4e86\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684<code>plt.plot<\/code>\u51fd\u6570\u6765\u7ed8\u5236\u66f2\u7ebf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.plot(x, y, label=&#39;sin(x)&#39;)  # \u7ed8\u5236\u66f2\u7ebf\u5e76\u8bbe\u7f6e\u6807\u7b7e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u53c2\u6570\u6765\u5b9a\u5236\u66f2\u7ebf\u7684\u6837\u5f0f\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u53ef\u4ee5\u6539\u53d8\u66f2\u7ebf\u7684\u989c\u8272\u548c\u7ebf\u6761\u6837\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.plot(x, y, color=&#39;blue&#39;, linestyle=&#39;--&#39;, linewidth=2, marker=&#39;o&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6dfb\u52a0\u6807\u9898\u3001\u6807\u7b7e\u548c\u56fe\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u8ba9\u56fe\u8868\u66f4\u52a0\u6613\u8bfb\uff0c\u4f60\u53ef\u4ee5\u6dfb\u52a0\u6807\u9898\u3001\u5750\u6807\u8f74\u6807\u7b7e\u548c\u56fe\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.title(&#39;Sine Wave&#39;)  # \u6dfb\u52a0\u6807\u9898<\/p>\n<p>plt.xlabel(&#39;X Axis&#39;)  # \u6dfb\u52a0x\u8f74\u6807\u7b7e<\/p>\n<p>plt.ylabel(&#39;Y Axis&#39;)  # \u6dfb\u52a0y\u8f74\u6807\u7b7e<\/p>\n<p>plt.legend()  # \u663e\u793a\u56fe\u4f8b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u663e\u793a\u56fe\u8868<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4f7f\u7528<code>plt.show()<\/code>\u51fd\u6570\u6765\u663e\u793a\u56fe\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4fdd\u5b58\u56fe\u8868<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u4fdd\u5b58\u56fe\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.savefig<\/code>\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.savefig(&#39;sine_wave.png&#39;)  # \u4fdd\u5b58\u56fe\u8868\u4e3aPNG\u683c\u5f0f<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u5c06\u4e0a\u8ff0\u6240\u6709\u6b65\u9aa4\u6574\u5408\u5728\u4e00\u8d77\uff0c\u5b8c\u6574\u7684\u793a\u4f8b\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>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u7ed8\u5236\u66f2\u7ebf<\/strong><\/h2>\n<p>plt.plot(x, y, color=&#39;blue&#39;, linestyle=&#39;--&#39;, linewidth=2, marker=&#39;o&#39;, label=&#39;sin(x)&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u3001\u6807\u7b7e\u548c\u56fe\u4f8b<\/strong><\/h2>\n<p>plt.title(&#39;Sine Wave&#39;)<\/p>\n<p>plt.xlabel(&#39;X Axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y Axis&#39;)<\/p>\n<p>plt.legend()<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5176\u4ed6\u9ad8\u7ea7\u529f\u80fd<\/h3>\n<\/p>\n<p><p><strong>1. \u591a\u6761\u66f2\u7ebf\u7ed8\u5236<\/strong><\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u56fe\u8868\u4e2d\u7ed8\u5236\u591a\u6761\u66f2\u7ebf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">y2 = np.cos(x)<\/p>\n<p>plt.plot(x, y, label=&#39;sin(x)&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;cos(x)&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u5b50\u56fe<\/strong><\/p>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>plt.subplot<\/code>\u51fd\u6570\u5728\u540c\u4e00\u4e2a\u7a97\u53e3\u4e2d\u521b\u5efa\u591a\u4e2a\u5b50\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.subplot(2, 1, 1)  # (\u884c\u6570, \u5217\u6570, \u7b2c\u51e0\u4e2a\u5b50\u56fe)<\/p>\n<p>plt.plot(x, y, label=&#39;sin(x)&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.subplot(2, 1, 2)<\/p>\n<p>plt.plot(x, y2, label=&#39;cos(x)&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u52a8\u6001\u66f4\u65b0<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u52a8\u6001\u66f4\u65b0\u56fe\u8868\uff0c\u4f8b\u5982\u5728\u5b9e\u65f6\u6570\u636e\u7ed8\u5236\u4e2d\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>matplotlib.animation<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.animation as animation<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>line, = ax.plot([], [], lw=2)<\/p>\n<p>def init():<\/p>\n<p>    ax.set_xlim(0, 10)<\/p>\n<p>    ax.set_ylim(-1, 1)<\/p>\n<p>    return line,<\/p>\n<p>def update(frame):<\/p>\n<p>    x = np.linspace(0, 10, 100)<\/p>\n<p>    y = np.sin(x + frame \/ 10.0)<\/p>\n<p>    line.set_data(x, y)<\/p>\n<p>    return line,<\/p>\n<p>ani = animation.FuncAnimation(fig, update, frames=100, init_func=init, blit=True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528Seaborn\u5e93<\/h3>\n<\/p>\n<p><p>Seaborn\u662f\u4e00\u4e2a\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u7f8e\u89c2\u548c\u7b80\u6d01\u7684\u7ed8\u56fe\u63a5\u53e3\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Seaborn\u6765\u7ed8\u5236\u66f2\u7ebf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<p>data = {&#39;x&#39;: x, &#39;y&#39;: y}<\/p>\n<h2><strong>\u4f7f\u7528Seaborn\u7ed8\u5236\u66f2\u7ebf<\/strong><\/h2>\n<p>sns.lineplot(x=&#39;x&#39;, y=&#39;y&#39;, data=data)<\/p>\n<p>plt.title(&#39;Sine Wave&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u7ed8\u5236\u5404\u79cd\u7c7b\u578b\u7684\u66f2\u7ebf\u56fe\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u66f2\u7ebf\u7ed8\u5236\uff0c\u8fd8\u662f\u590d\u6742\u7684\u52a8\u6001\u66f4\u65b0\u548c\u5b50\u56fe\uff0cPython\u4e2d\u7684Matplotlib\u548cSeaborn\u5e93\u90fd\u80fd\u591f\u6ee1\u8db3\u4f60\u7684\u9700\u6c42\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4f7f\u7528Python\u753b\u66f2\u7ebf\u9700\u8981\u54ea\u4e9b\u5e93\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u7ed8\u5236\u66f2\u7ebf\u901a\u5e38\u4f7f\u7528Matplotlib\u5e93\u3002\u8fd9\u4e2a\u5e93\u529f\u80fd\u5f3a\u5927\uff0c\u80fd\u591f\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u5f62\uff0c\u5305\u62ec\u7ebf\u56fe\u3001\u6563\u70b9\u56fe\u548c\u66f2\u7ebf\u56fe\u3002\u9664\u4e86Matplotlib\uff0cNumPy\u5e93\u4e5f\u5e38\u7528\u4e8e\u751f\u6210\u6570\u636e\u70b9\uff0c\u4f7f\u7ed8\u56fe\u66f4\u52a0\u4fbf\u6377\u548c\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bbe\u7f6e\u66f2\u7ebf\u7684\u6837\u5f0f\u548c\u989c\u8272\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u8f7b\u677e\u8bbe\u7f6e\u66f2\u7ebf\u7684\u6837\u5f0f\u548c\u989c\u8272\u3002\u4f7f\u7528<code>plot()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u4f20\u9012<code>linestyle<\/code>\u3001<code>color<\/code>\u548c<code>linewidth<\/code>\u7b49\u53c2\u6570\u3002\u4f8b\u5982\uff0c<code>plt.plot(x, y, linestyle=&#39;--&#39;, color=&#39;r&#39;, linewidth=2)<\/code>\u53ef\u4ee5\u7ed8\u5236\u4e00\u6761\u7ea2\u8272\u865a\u7ebf\uff0c\u7ebf\u5bbd\u4e3a2\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u66f2\u7ebf\u56fe\u4e2d\u6dfb\u52a0\u6807\u7b7e\u548c\u6807\u9898\uff1f<\/strong><br \/>\u6dfb\u52a0\u6807\u7b7e\u548c\u6807\u9898\u53ef\u4ee5\u63d0\u9ad8\u56fe\u5f62\u7684\u53ef\u8bfb\u6027\u3002\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.title()<\/code>\u4e3a\u56fe\u5f62\u6dfb\u52a0\u6807\u9898\uff0c\u4f7f\u7528<code>plt.xlabel()<\/code>\u548c<code>plt.ylabel()<\/code>\u4e3a\u5750\u6807\u8f74\u6dfb\u52a0\u6807\u7b7e\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>plt.legend()<\/code>\u53ef\u4ee5\u4e3a\u66f2\u7ebf\u6dfb\u52a0\u56fe\u4f8b\uff0c\u4ee5\u4fbf\u4e8e\u533a\u5206\u4e0d\u540c\u7684\u6570\u636e\u7cfb\u5217\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u753b\u4e00\u6bb5\u66f2\u7ebf\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Matplotlib\u3001NumPy\u3001Seaborn\u7b49\u5e93\u3001\u901a\u8fc7plt.plo [&hellip;]","protected":false},"author":3,"featured_media":1101710,"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\/1101704"}],"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=1101704"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101704\/revisions"}],"predecessor-version":[{"id":1101712,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101704\/revisions\/1101712"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1101710"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1101704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1101704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1101704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}