{"id":1121189,"date":"2025-01-08T19:06:32","date_gmt":"2025-01-08T11:06:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1121189.html"},"modified":"2025-01-08T19:06:34","modified_gmt":"2025-01-08T11:06:34","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e4%b8%80%e6%9d%a1%e9%98%bf%e5%9f%ba%e7%b1%b3%e5%be%b7%e8%9e%ba%e7%ba%bf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1121189.html","title":{"rendered":"python\u5982\u4f55\u753b\u4e00\u6761\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083532\/a8657c74-c114-4f94-90ce-b2dd1da42839.webp\" alt=\"python\u5982\u4f55\u753b\u4e00\u6761\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u7ed8\u5236\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u548c\u56fe\u5f62\u5e93\uff0c\u5982Matplotlib\uff0c\u6765\u5b9e\u73b0\u3002<\/strong> \u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u7684\u516c\u5f0f\u662f\uff1ar = a + b\u03b8\uff0c\u5176\u4e2dr\u662f\u534a\u5f84\uff0c\u03b8\u662f\u89d2\u5ea6\uff0ca\u548cb\u662f\u5e38\u6570\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u3002<\/p>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u4ec0\u4e48\u662f\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf<\/strong><\/li>\n<li><strong>Matplotlib\u7b80\u4ecb<\/strong><\/li>\n<li><strong>\u7ed8\u5236\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u7684\u6b65\u9aa4<\/strong><\/li>\n<li><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/li>\n<li><strong>\u6269\u5c55\u5e94\u7528<\/strong><\/li>\n<\/ol>\n<p><h3>\u4e00\u3001\u4ec0\u4e48\u662f\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf<\/h3>\n<\/p>\n<p><p><strong>\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u662f\u4e00\u79cd\u5728\u6781\u5750\u6807\u7cfb\u4e2d\u63cf\u8ff0\u7684\u87ba\u7ebf\uff0c\u5176\u7279\u5f81\u662f\u534a\u5f84r\u4e0e\u89d2\u5ea6\u03b8\u6210\u7ebf\u6027\u5173\u7cfb\u3002<\/strong> \u516c\u5f0f\u4e3a\uff1ar = a + b\u03b8\uff0c\u5176\u4e2da\u548cb\u662f\u5e38\u6570\u3002\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u7684\u7279\u70b9\u662f\u6bcf\u8f6c\u4e00\u5708\u534a\u5f84\u90fd\u4f1a\u589e\u52a0\u4e00\u4e2a\u56fa\u5b9a\u7684\u8ddd\u79bb\u3002<\/p>\n<\/p>\n<p><p>\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u5728\u79d1\u5b66\u548c\u5de5\u7a0b\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u4f8b\u5982\u7528\u4e8e\u8bbe\u8ba1\u6da1\u8f6e\u673a\u53f6\u7247\u3001\u5929\u7ebf\u548c\u5176\u4ed6\u9700\u8981\u87ba\u65cb\u5f62\u72b6\u7684\u673a\u68b0\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001Matplotlib\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p><strong>Matplotlib\u662f\u4e00\u4e2aPython\u76842D\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u5f62\u3002<\/strong> \u5b83\u7279\u522b\u9002\u5408\u7528\u4e8e\u7ed8\u5236\u79d1\u5b66\u548c\u5de5\u7a0b\u56fe\u5f62\u3002Matplotlib\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u521b\u5efa\u3001\u5b9a\u5236\u548c\u5c55\u793a\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h4>Matplotlib\u7684\u5b89\u88c5<\/h4>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Matplotlib\uff0c\u4f60\u9700\u8981\u5148\u5b89\u88c5\u5b83\u3002\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><h3>\u4e09\u3001\u7ed8\u5236\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u7684\u6b65\u9aa4<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u6240\u9700\u5e93<\/strong><\/li>\n<li><strong>\u5b9a\u4e49\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u7684\u53c2\u6570<\/strong><\/li>\n<li><strong>\u8ba1\u7b97\u87ba\u7ebf\u7684\u5750\u6807<\/strong><\/li>\n<li><strong>\u7ed8\u5236\u87ba\u7ebf<\/strong><\/li>\n<li><strong>\u5c55\u793a\u56fe\u5f62<\/strong><\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Python\u548cMatplotlib\u7ed8\u5236\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u5b9a\u4e49\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u7684\u53c2\u6570<\/strong><\/h2>\n<p>a = 0.5  # \u5e38\u6570a<\/p>\n<p>b = 0.2  # \u5e38\u6570b<\/p>\n<p>theta_max = 10 * np.pi  # \u89d2\u5ea6\u7684\u6700\u5927\u503c<\/p>\n<p>num_points = 1000  # \u70b9\u7684\u6570\u91cf<\/p>\n<h2><strong>\u8ba1\u7b97\u87ba\u7ebf\u7684\u5750\u6807<\/strong><\/h2>\n<p>theta = np.linspace(0, theta_max, num_points)<\/p>\n<p>r = a + b * theta<\/p>\n<h2><strong>\u5c06\u6781\u5750\u6807\u8f6c\u6362\u4e3a\u7b1b\u5361\u5c14\u5750\u6807<\/strong><\/h2>\n<p>x = r * np.cos(theta)<\/p>\n<p>y = r * np.sin(theta)<\/p>\n<h2><strong>\u7ed8\u5236\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf<\/strong><\/h2>\n<p>plt.figure(figsize=(8, 8))<\/p>\n<p>plt.plot(x, y, label=&#39;\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf&#39;)<\/p>\n<p>plt.title(&#39;\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf&#39;)<\/p>\n<p>plt.xlabel(&#39;X&#39;)<\/p>\n<p>plt.ylabel(&#39;Y&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.axis(&#39;equal&#39;)  # \u4fdd\u6301\u5750\u6807\u8f74\u6bd4\u4f8b<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6269\u5c55\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1. <strong>\u8c03\u6574\u87ba\u7ebf\u53c2\u6570<\/strong><\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u6539\u53d8a\u548cb\u7684\u503c\uff0c\u751f\u6210\u4e0d\u540c\u5f62\u72b6\u7684\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = 1<\/p>\n<p>b = 0.5<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. <strong>\u591a\u6761\u87ba\u7ebf\u7684\u53e0\u52a0<\/strong><\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u7ed8\u5236\u591a\u6761\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\uff0c\u4ee5\u89c2\u5bdf\u4e0d\u540c\u53c2\u6570\u5bf9\u87ba\u7ebf\u5f62\u72b6\u7684\u5f71\u54cd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e0d\u540c\u7684\u53c2\u6570<\/p>\n<p>params = [(0.5, 0.2), (1, 0.5), (0.2, 0.1)]<\/p>\n<p>plt.figure(figsize=(8, 8))<\/p>\n<p>for a, b in params:<\/p>\n<p>    r = a + b * theta<\/p>\n<p>    x = r * np.cos(theta)<\/p>\n<p>    y = r * np.sin(theta)<\/p>\n<p>    plt.plot(x, y, label=f&#39;a={a}, b={b}&#39;)<\/p>\n<p>plt.title(&#39;\u4e0d\u540c\u53c2\u6570\u7684\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf&#39;)<\/p>\n<p>plt.xlabel(&#39;X&#39;)<\/p>\n<p>plt.ylabel(&#39;Y&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. <strong>\u52a8\u753b\u5c55\u793a<\/strong><\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684\u52a8\u753b\u529f\u80fd\u6765\u52a8\u6001\u5c55\u793a\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u7684\u751f\u6210\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.animation as animation<\/p>\n<p>fig, ax = plt.subplots(figsize=(8, 8))<\/p>\n<p>line, = ax.plot([], [], lw=2)<\/p>\n<p>ax.set_xlim(-50, 50)<\/p>\n<p>ax.set_ylim(-50, 50)<\/p>\n<p>ax.grid(True)<\/p>\n<p>def init():<\/p>\n<p>    line.set_data([], [])<\/p>\n<p>    return line,<\/p>\n<p>def update(frame):<\/p>\n<p>    r = a + b * theta[:frame]<\/p>\n<p>    x = r * np.cos(theta[:frame])<\/p>\n<p>    y = r * np.sin(theta[:frame])<\/p>\n<p>    line.set_data(x, y)<\/p>\n<p>    return line,<\/p>\n<p>ani = animation.FuncAnimation(fig, update, frames=num_points, init_func=init, blit=True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>Python\u548cMatplotlib\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u6765\u7ed8\u5236\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u5f62\uff0c\u5305\u62ec\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u3002<\/strong> \u901a\u8fc7\u5b9a\u4e49\u53c2\u6570\u3001\u8ba1\u7b97\u5750\u6807\u5e76\u4f7f\u7528Matplotlib\u7ed8\u56fe\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u751f\u6210\u548c\u5c55\u793a\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u53c2\u6570\u3001\u53e0\u52a0\u591a\u6761\u87ba\u7ebf\u548c\u5236\u4f5c\u52a8\u753b\u7b49\u65b9\u5f0f\u8fdb\u4e00\u6b65\u63a2\u7d22\u548c\u5c55\u793a\u8fd9\u79cd\u87ba\u7ebf\u7684\u7279\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\uff1f<\/strong><br \/>\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u662f\u4e00\u4e2a\u7ecf\u5178\u7684\u6570\u5b66\u66f2\u7ebf\uff0c\u53ef\u4ee5\u901a\u8fc7\u6781\u5750\u6807\u65b9\u7a0br = a + b\u03b8\u6765\u7ed8\u5236\u3002\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528matplotlib\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u9996\u5148\uff0c\u786e\u4fdd\u60a8\u5df2\u5b89\u88c5matplotlib\u5e93\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u6765\u7ed8\u5236\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nimport matplotlib.pyplot as plt\n\n# \u53c2\u6570\u8bbe\u7f6e\na = 0  # \u8d77\u59cb\u534a\u5f84\nb = 1  # \u6bcf\u8f6c\u4e00\u5708\u589e\u52a0\u7684\u534a\u5f84\n\n# \u751f\u6210\u89d2\u5ea6\ntheta = np.linspace(0, 4 * np.pi, 1000)  # 0\u52304\u03c0\u7684\u89d2\u5ea6\n\n# \u8ba1\u7b97r\nr = a + b * theta\n\n# \u6781\u5750\u6807\u8f6c\u6362\u4e3a\u7b1b\u5361\u5c14\u5750\u6807\nx = r * np.cos(theta)\ny = r * np.sin(theta)\n\n# \u7ed8\u5236\u56fe\u5f62\nplt.figure(figsize=(8, 8))\nplt.plot(x, y)\nplt.title(&quot;\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf&quot;)\nplt.xlabel(&quot;X\u8f74&quot;)\nplt.ylabel(&quot;Y\u8f74&quot;)\nplt.axis(&#39;equal&#39;)  # \u4fdd\u6301\u5750\u6807\u8f74\u6bd4\u4f8b\nplt.grid()\nplt.show()\n<\/code><\/pre>\n<p><strong>\u6211\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9bPython\u5e93\u6765\u7ed8\u5236\u6570\u5b66\u56fe\u5f62\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u4e2a\u5f3a\u5927\u7684\u5e93\u7528\u4e8e\u7ed8\u5236\u6570\u5b66\u56fe\u5f62\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ecmatplotlib\u3001numpy\u3001seaborn\u548cplotly\u3002matplotlib\u662f\u6700\u57fa\u7840\u548c\u6d41\u884c\u7684\u9009\u62e9\uff0c\u9002\u5408\u4e8e\u7b80\u5355\u76842D\u7ed8\u56fe\u3002\u800c\u5982\u679c\u9700\u8981\u4ea4\u4e92\u5f0f\u56fe\u5f62\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528plotly\u3002\u6b64\u5916\uff0cseaborn\u662f\u4e00\u4e2a\u57fa\u4e8ematplotlib\u7684\u9ad8\u7ea7\u63a5\u53e3\uff0c\u63d0\u4f9b\u66f4\u7f8e\u89c2\u7684\u56fe\u5f62\u6837\u5f0f\u3002<\/p>\n<p><strong>\u7ed8\u5236\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u53c2\u6570\u8bbe\u7f6e\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u65f6\uff0c\u4e3b\u8981\u5173\u6ce8\u8d77\u59cb\u534a\u5f84\u548c\u6bcf\u5708\u589e\u52a0\u7684\u534a\u5f84\u3002\u8d77\u59cb\u534a\u5f84\u51b3\u5b9a\u4e86\u87ba\u7ebf\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u800c\u6bcf\u5708\u589e\u52a0\u7684\u534a\u5f84\u5219\u5f71\u54cd\u87ba\u7ebf\u7684\u6269\u5c55\u901f\u5ea6\u3002\u901a\u8fc7\u8c03\u6574\u8fd9\u4e24\u4e2a\u53c2\u6570\uff0c\u60a8\u53ef\u4ee5\u751f\u6210\u4e0d\u540c\u5f62\u72b6\u548c\u5bc6\u5ea6\u7684\u87ba\u7ebf\u3002\u6b64\u5916\uff0c\u9009\u62e9\u5408\u9002\u7684\u89d2\u5ea6\u8303\u56f4\u548c\u70b9\u7684\u6570\u91cf\u4e5f\u4f1a\u5f71\u54cd\u56fe\u5f62\u7684\u5e73\u6ed1\u5ea6\u548c\u6e05\u6670\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u7ed8\u5236\u963f\u57fa\u7c73\u5fb7\u87ba\u7ebf\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u548c\u56fe\u5f62\u5e93\uff0c\u5982Matplotlib\uff0c\u6765\u5b9e\u73b0\u3002 \u963f\u57fa\u7c73\u5fb7\u87ba [&hellip;]","protected":false},"author":3,"featured_media":1121196,"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\/1121189"}],"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=1121189"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1121189\/revisions"}],"predecessor-version":[{"id":1121197,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1121189\/revisions\/1121197"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1121196"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1121189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1121189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1121189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}