{"id":1085284,"date":"2025-01-08T13:14:41","date_gmt":"2025-01-08T05:14:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1085284.html"},"modified":"2025-01-08T13:14:44","modified_gmt":"2025-01-08T05:14:44","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%bb%e4%b8%89%e7%bb%b4%e6%a4%ad%e7%90%83-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1085284.html","title":{"rendered":"\u5982\u4f55\u7528python\u753b\u4e09\u7ef4\u692d\u7403"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195235\/0424a625-c5c7-44fd-a4d9-2d819ca2aa28.webp\" alt=\"\u5982\u4f55\u7528python\u753b\u4e09\u7ef4\u692d\u7403\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u753b\u4e09\u7ef4\u692d\u7403\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u5b9e\u73b0\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u548c\u65b9\u4fbf\u7684\u662f\u901a\u8fc7<code>matplotlib<\/code>\u5e93\u8fdb\u884c\u7ed8\u5236\u3002<strong>\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u4e2d\u7684<code>Axes3D<\/code>\u6a21\u5757\u3001\u521b\u5efa\u692d\u7403\u7684\u53c2\u6570\u5316\u65b9\u7a0b\u3001\u7ed8\u5236\u4e09\u7ef4\u7f51\u683c<\/strong>\uff0c\u662f\u753b\u4e09\u7ef4\u692d\u7403\u7684\u5173\u952e\u6b65\u9aa4\u3002\u63a5\u4e0b\u6765\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86\u5fc5\u8981\u7684\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u5b83\u4eec\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5bfc\u5165\u5e93\u5e76\u8bbe\u7f6e\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u5e76\u8bbe\u7f6e\u692d\u7403\u7684\u53c2\u6570\u3002\u8fd9\u4e9b\u53c2\u6570\u5305\u62ec\u692d\u7403\u7684\u4e2d\u5fc3\u4f4d\u7f6e\u548c\u8f74\u7684\u957f\u5ea6\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<p>from mpl_toolkits.mplot3d import Axes3D<\/p>\n<h2><strong>\u692d\u7403\u7684\u4e2d\u5fc3\u4f4d\u7f6e<\/strong><\/h2>\n<p>center = [0, 0, 0]<\/p>\n<h2><strong>\u692d\u7403\u7684\u8f74\u957f<\/strong><\/h2>\n<p>axes_length = [5, 3, 2]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u521b\u5efa\u692d\u7403\u7684\u53c2\u6570\u5316\u65b9\u7a0b<\/h3>\n<\/p>\n<p><p>\u692d\u7403\u7684\u53c2\u6570\u5316\u65b9\u7a0b\u53ef\u4ee5\u7528\u4e24\u4e2a\u53c2\u6570<code>u<\/code>\u548c<code>v<\/code>\u6765\u8868\u793a\uff0c\u5b83\u4eec\u5206\u522b\u8868\u793a\u692d\u7403\u7684\u7ecf\u5ea6\u548c\u7eac\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u53c2\u6570<\/p>\n<p>u = np.linspace(0, 2 * np.pi, 100)<\/p>\n<p>v = np.linspace(0, np.pi, 100)<\/p>\n<h2><strong>\u521b\u5efa\u7f51\u683c<\/strong><\/h2>\n<p>u, v = np.meshgrid(u, v)<\/p>\n<h2><strong>\u8ba1\u7b97x, y, z\u5750\u6807<\/strong><\/h2>\n<p>x = center[0] + axes_length[0] * np.outer(np.cos(u), np.sin(v))<\/p>\n<p>y = center[1] + axes_length[1] * np.outer(np.sin(u), np.sin(v))<\/p>\n<p>z = center[2] + axes_length[2] * np.outer(np.ones(np.size(u)), np.cos(v))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ed8\u5236\u4e09\u7ef4\u692d\u7403<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u4e2d\u7684<code>Axes3D<\/code>\u6a21\u5757\u6765\u7ed8\u5236\u4e09\u7ef4\u692d\u7403\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u7ed8\u56fe\u5bf9\u8c61<\/p>\n<p>fig = plt.figure()<\/p>\n<p>ax = fig.add_subplot(111, projection=&#39;3d&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u692d\u7403<\/strong><\/h2>\n<p>ax.plot_surface(x, y, z, color=&#39;b&#39;, alpha=0.6)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6807\u7b7e<\/strong><\/h2>\n<p>ax.set_xlabel(&#39;X&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Y&#39;)<\/p>\n<p>ax.set_zlabel(&#39;Z&#39;)<\/p>\n<h2><strong>\u663e\u793a\u7ed8\u56fe<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u4e09\u7ef4\u692d\u7403\u7684\u56fe\u50cf\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u6bcf\u4e2a\u6b65\u9aa4\u7684\u7ec6\u8282\u548c\u53ef\u80fd\u7684\u6539\u8fdb\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u692d\u7403\u7684\u6570\u5b66\u57fa\u7840<\/h3>\n<\/p>\n<p><p>\u692d\u7403\u662f\u7531\u4ee5\u4e0b\u65b9\u7a0b\u63cf\u8ff0\u7684\uff1a<\/p>\n<\/p>\n<p><p>[ \\frac{x^2}{a^2} + \\frac{y^2}{b^2} + \\frac{z^2}{c^2} = 1 ]<\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>a<\/code>\u3001<code>b<\/code>\u548c<code>c<\/code>\u5206\u522b\u662f\u692d\u7403\u5728<code>x<\/code>\u3001<code>y<\/code>\u548c<code>z<\/code>\u65b9\u5411\u4e0a\u7684\u534a\u8f74\u957f\u3002\u901a\u8fc7\u53c2\u6570\u5316\u8fd9\u4e2a\u65b9\u7a0b\uff0c\u6211\u4eec\u5f97\u5230\u4e86\u7528\u4e8e\u7ed8\u5236\u692d\u7403\u7684\u53c2\u6570\u65b9\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u53c2\u6570\u5316\u65b9\u7a0b\u7684\u7ec6\u8282<\/h3>\n<\/p>\n<p><p>\u53c2\u6570\u5316\u692d\u7403\u7684\u5173\u952e\u5728\u4e8e\u4f7f\u7528\u4e24\u4e2a\u53c2\u6570\u6765\u8986\u76d6\u692d\u7403\u7684\u6574\u4e2a\u8868\u9762\u3002\u8fd9\u4e24\u4e2a\u53c2\u6570\u901a\u5e38\u662f<code>u<\/code>\u548c<code>v<\/code>\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>u<\/code>\uff1a\u8868\u793a\u692d\u7403\u7684\u7ecf\u5ea6\uff0c\u8303\u56f4\u662f[0, 2\u03c0]\u3002<\/li>\n<li><code>v<\/code>\uff1a\u8868\u793a\u692d\u7403\u7684\u7eac\u5ea6\uff0c\u8303\u56f4\u662f[0, \u03c0]\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u8fd9\u4e24\u4e2a\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u692d\u7403\u8868\u9762\u4e0a\u6bcf\u4e00\u70b9\u7684\u5750\u6807\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u6539\u8fdb\u7ed8\u56fe\u7684\u5916\u89c2<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u900f\u660e\u5ea6\u3001\u989c\u8272\u548c\u7f51\u683c\u7684\u5206\u8fa8\u7387\u6765\u6539\u8fdb\u7ed8\u56fe\u7684\u5916\u89c2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8c03\u6574\u900f\u660e\u5ea6\u548c\u989c\u8272<\/p>\n<p>ax.plot_surface(x, y, z, color=&#39;r&#39;, alpha=0.5)<\/p>\n<h2><strong>\u589e\u52a0\u7f51\u683c\u7ebf<\/strong><\/h2>\n<p>ax.plot_wireframe(x, y, z, color=&#39;k&#39;, linewidth=0.5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u6dfb\u52a0\u66f4\u591a\u7279\u6027<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u7ed8\u56fe\u66f4\u52a0\u4e30\u5bcc\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u66f4\u591a\u7279\u6027\uff0c\u6bd4\u5982\u5728\u692d\u7403\u4e0a\u6807\u8bb0\u4e00\u4e9b\u70b9\uff0c\u6216\u8005\u6dfb\u52a0\u5750\u6807\u8f74\u7684\u523b\u5ea6\u548c\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6807\u8bb0\u692d\u7403\u4e0a\u7684\u4e00\u4e2a\u70b9<\/p>\n<p>ax.scatter(center[0], center[1], center[2], color=&#39;g&#39;, s=100)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_xlim([-6, 6])<\/p>\n<p>ax.set_ylim([-6, 6])<\/p>\n<p>ax.set_zlim([-6, 6])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u4fdd\u5b58\u7ed8\u56fe<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u7ed8\u56fe\u4fdd\u5b58\u4e3a\u56fe\u50cf\u6587\u4ef6\uff0c\u4ee5\u4fbf\u8fdb\u4e00\u6b65\u4f7f\u7528\u6216\u5206\u4eab\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.savefig(&#39;ellipsoid.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u7684<code>matplotlib<\/code>\u5e93\u6765\u7ed8\u5236\u4e09\u7ef4\u692d\u7403\u3002<strong>\u5b9a\u4e49\u692d\u7403\u7684\u53c2\u6570\u3001\u521b\u5efa\u53c2\u6570\u5316\u65b9\u7a0b\u3001\u4f7f\u7528<code>matplotlib<\/code>\u7ed8\u5236\u4e09\u7ef4\u56fe\u50cf<\/strong>\uff0c\u662f\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u5173\u952e\u3002\u638c\u63e1\u8fd9\u4e9b\u6b65\u9aa4\u540e\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u692d\u7403\u7684\u53c2\u6570\u548c\u7ed8\u56fe\u7684\u5916\u89c2\uff0c\u751f\u6210\u7b26\u5408\u81ea\u5df1\u9700\u6c42\u7684\u4e09\u7ef4\u692d\u7403\u56fe\u50cf\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>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u4e09\u7ef4\u692d\u7403\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u7ed8\u5236\u4e09\u7ef4\u692d\u7403\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff0c\u8bbe\u7f6e\u692d\u7403\u7684\u53c2\u6570\uff08\u5982\u534a\u8f74\u957f\u5ea6\uff09\uff0c\u751f\u6210\u692d\u7403\u7684\u5750\u6807\u70b9\uff0c\u4ee5\u53ca\u4f7f\u7528\u7ed8\u56fe\u5e93\u8fdb\u884c\u53ef\u89c6\u5316\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecNumPy\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\uff0cMatplotlib\u7528\u4e8e\u7ed8\u56fe\u3002\u901a\u8fc7\u5b9a\u4e49\u53c2\u6570\u65b9\u7a0b\uff0c\u53ef\u4ee5\u5f97\u5230\u692d\u7403\u7684\u8868\u9762\u70b9\uff0c\u63a5\u7740\u5229\u7528<code>plot_surface<\/code>\u51fd\u6570\u7ed8\u5236\u51fa\u692d\u7403\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7ed8\u5236\u4e09\u7ef4\u692d\u7403\u65f6\u9700\u8981\u5b89\u88c5\u54ea\u4e9b\u5e93\uff1f<\/strong><br \/>\u4e3a\u4e86\u5728Python\u4e2d\u7ed8\u5236\u4e09\u7ef4\u692d\u7403\uff0c\u60a8\u9700\u8981\u5b89\u88c5NumPy\u548cMatplotlib\u8fd9\u4e24\u4e2a\u5e93\u3002NumPy\u7528\u4e8e\u5904\u7406\u6570\u7ec4\u548c\u6570\u5b66\u8fd0\u7b97\uff0c\u800cMatplotlib\u5219\u8d1f\u8d23\u7ed8\u5236\u4e09\u7ef4\u56fe\u5f62\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u8f7b\u677e\u5b89\u88c5\u8fd9\u4e24\u4e2a\u5e93\uff0c\u4f8b\u5982\uff1a<code>pip install numpy matplotlib<\/code>\u3002<\/p>\n<p><strong>\u5982\u4f55\u8c03\u6574\u7ed8\u5236\u7684\u4e09\u7ef4\u692d\u7403\u7684\u89c6\u89d2\u548c\u989c\u8272\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Matplotlib\u7ed8\u5236\u4e09\u7ef4\u692d\u7403\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u89c6\u89d2\uff08azim\u548celev\u53c2\u6570\uff09\u6765\u8c03\u6574\u56fe\u5f62\u7684\u89c2\u5bdf\u89d2\u5ea6\u3002\u540c\u65f6\uff0c\u60a8\u53ef\u4ee5\u5728\u7ed8\u5236\u692d\u7403\u65f6\u901a\u8fc7<code>color<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u692d\u7403\u7684\u989c\u8272\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>alpha<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u692d\u7403\u7684\u900f\u660e\u5ea6\uff0c\u4ee5\u589e\u5f3a\u89c6\u89c9\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u753b\u4e09\u7ef4\u692d\u7403\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u5b9e\u73b0\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u548c\u65b9\u4fbf\u7684\u662f\u901a\u8fc7matplotlib\u5e93\u8fdb\u884c\u7ed8\u5236\u3002\u4f7f\u7528m [&hellip;]","protected":false},"author":3,"featured_media":1085290,"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\/1085284"}],"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=1085284"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1085284\/revisions"}],"predecessor-version":[{"id":1085294,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1085284\/revisions\/1085294"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1085290"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1085284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1085284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1085284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}