{"id":1046290,"date":"2024-12-31T13:31:57","date_gmt":"2024-12-31T05:31:57","guid":{"rendered":""},"modified":"2024-12-31T13:32:00","modified_gmt":"2024-12-31T05:32:00","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","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1046290.html","title":{"rendered":"\u5982\u4f55\u7528python\u753b\u4e09\u7ef4\u692d\u7403"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/09625bba-6328-4f30-9be1-51931be3f634.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u753b\u4e09\u7ef4\u692d\u7403\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u7ed8\u5236\u4e09\u7ef4\u692d\u7403\u901a\u5e38\u4f7f\u7528<code>matplotlib<\/code>\u548c<code>numpy<\/code>\u5e93\u3002<strong>\u4f60\u53ef\u4ee5\u901a\u8fc7\u751f\u6210\u4e09\u7ef4\u5750\u6807\u6570\u636e\u5e76\u4f7f\u7528<code>matplotlib<\/code>\u76843D\u7ed8\u56fe\u529f\u80fd\u6765\u7ed8\u5236\u692d\u7403\uff0c\u9996\u5148\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u5b9a\u4e49\u692d\u7403\u7684\u53c2\u6570\u3001\u751f\u6210\u5750\u6807\u6570\u636e\uff0c\u6700\u540e\u4f7f\u7528<code>plot_surface<\/code>\u7ed8\u5236\u692d\u7403<\/strong>\u3002\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86<code>matplotlib<\/code>\u548c<code>numpy<\/code>\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\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>\u7136\u540e\uff0c\u5bfc\u5165\u8fd9\u4e9b\u5e93\uff1a<\/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<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b9a\u4e49\u692d\u7403\u7684\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u692d\u7403\u7684\u6807\u51c6\u65b9\u7a0b\u662f\uff1a<br \/>[ \\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>\u3001<code>c<\/code>\u5206\u522b\u662f\u692d\u7403\u5728x\u3001y\u3001z\u8f74\u4e0a\u7684\u534a\u8f74\u957f\u5ea6\u3002\u4e3a\u4e86\u751f\u6210\u4e09\u7ef4\u6570\u636e\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u7403\u5750\u6807\u7cfb\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u76f4\u89d2\u5750\u6807\u7cfb\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u751f\u6210\u692d\u7403\u7684\u5750\u6807\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u7403\u5750\u6807\u7cfb\u751f\u6210\u70b9\uff0c\u5e76\u8f6c\u6362\u4e3a\u76f4\u89d2\u5750\u6807\u7cfb\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u692d\u7403\u7684\u53c2\u6570<\/p>\n<p>a = 1.0  # x\u8f74\u534a\u8f74\u957f\u5ea6<\/p>\n<p>b = 0.5  # y\u8f74\u534a\u8f74\u957f\u5ea6<\/p>\n<p>c = 0.75 # z\u8f74\u534a\u8f74\u957f\u5ea6<\/p>\n<h2><strong>\u751f\u6210\u7403\u5750\u6807<\/strong><\/h2>\n<p>u = np.linspace(0, 2 * np.pi, 100)<\/p>\n<p>v = np.linspace(0, np.pi, 100)<\/p>\n<p>u, v = np.meshgrid(u, v)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3a\u76f4\u89d2\u5750\u6807<\/strong><\/h2>\n<p>x = a * np.cos(u) * np.sin(v)<\/p>\n<p>y = b * np.sin(u) * np.sin(v)<\/p>\n<p>z = c * 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>\u7684<code>plot_surface<\/code>\u65b9\u6cd5\u7ed8\u5236\u692d\u7403\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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\u56fe\u5f62\u663e\u793a\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_xlim([-a, a])<\/p>\n<p>ax.set_ylim([-b, b])<\/p>\n<p>ax.set_zlim([-c, c])<\/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\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8be6\u7ec6\u89e3\u91ca\u548c\u6269\u5c55<\/h3>\n<\/p>\n<ol>\n<li><strong>\u751f\u6210\u7403\u5750\u6807<\/strong>\uff1a\u6211\u4eec\u4f7f\u7528<code>numpy<\/code>\u7684<code>linspace<\/code>\u51fd\u6570\u751f\u6210\u5747\u5300\u5206\u5e03\u7684\u89d2\u5ea6\u6570\u636e<code>u<\/code>\u548c<code>v<\/code>\u3002<code>u<\/code>\u4ece0\u52302\u03c0\uff0c\u4ee3\u8868\u7ecf\u5ea6\u89d2\uff1b<code>v<\/code>\u4ece0\u5230\u03c0\uff0c\u4ee3\u8868\u7eac\u5ea6\u89d2\u3002\u901a\u8fc7<code>meshgrid<\/code>\u51fd\u6570\u5c06\u8fd9\u4e9b\u89d2\u5ea6\u7ec4\u5408\u6210\u7f51\u683c\u6570\u636e\u3002<\/li>\n<li><strong>\u8f6c\u6362\u4e3a\u76f4\u89d2\u5750\u6807<\/strong>\uff1a\u901a\u8fc7\u7403\u5750\u6807\u5230\u76f4\u89d2\u5750\u6807\u7684\u8f6c\u6362\u516c\u5f0f\uff0c\u5c06<code>u<\/code>\u548c<code>v<\/code>\u8f6c\u5316\u4e3ax\u3001y\u3001z\u5750\u6807\u3002\u8fd9\u91cc\u4f7f\u7528\u4e86\u692d\u7403\u7684\u53c2\u6570<code>a<\/code>\u3001<code>b<\/code>\u3001<code>c<\/code>\u6765\u8c03\u6574x\u3001y\u3001z\u5750\u6807\u7684\u6bd4\u4f8b\u3002<\/li>\n<li><strong>\u7ed8\u5236\u692d\u7403<\/strong>\uff1a\u4f7f\u7528<code>plot_surface<\/code>\u51fd\u6570\u7ed8\u5236\u692d\u7403\uff0c\u8bbe\u7f6e\u989c\u8272\u548c\u900f\u660e\u5ea6\u3002<code>alpha<\/code>\u53c2\u6570\u7528\u4e8e\u8bbe\u7f6e\u900f\u660e\u5ea6\uff0c\u4f7f\u56fe\u5f62\u66f4\u5bb9\u6613\u89c2\u5bdf\u3002<\/li>\n<li><strong>\u8bbe\u7f6e\u56fe\u5f62\u663e\u793a\u8303\u56f4<\/strong>\uff1a\u901a\u8fc7<code>set_xlim<\/code>\u3001<code>set_ylim<\/code>\u548c<code>set_zlim<\/code>\u8bbe\u7f6e\u56fe\u5f62\u7684\u663e\u793a\u8303\u56f4\uff0c\u786e\u4fdd\u692d\u7403\u5b8c\u5168\u663e\u793a\u5728\u89c6\u56fe\u4e2d\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u5b9a\u5236<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6dfb\u52a0\u7f51\u683c\u548c\u989c\u8272\u6620\u5c04<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.plot_surface(x, y, z, cmap=&#39;viridis&#39;, edgecolor=&#39;k&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u4f7f\u7528<code>cmap<\/code>\u53c2\u6570\u6dfb\u52a0\u989c\u8272\u6620\u5c04\uff0c<code>edgecolor<\/code>\u53c2\u6570\u6dfb\u52a0\u7f51\u683c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4ea4\u4e92\u5f0f\u7ed8\u56fe<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mpl_toolkits.mplot3d import Axes3D<\/p>\n<p>fig = plt.figure()<\/p>\n<p>ax = fig.add_subplot(111, projection=&#39;3d&#39;)<\/p>\n<p>ax.plot_surface(x, y, z, color=&#39;r&#39;, alpha=0.7)<\/p>\n<p>plt.ion()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>plt.ion()<\/code>\u5f00\u542f\u4ea4\u4e92\u6a21\u5f0f\uff0c\u53ef\u4ee5\u65cb\u8f6c\u548c\u7f29\u653e\u4e09\u7ef4\u56fe\u5f62\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig.savefig(&#39;ellipsoid.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0\u66f4\u591a\u4e09\u7ef4\u5bf9\u8c61<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ed8\u5236\u4e00\u4e2a\u4e09\u7ef4\u7403\u4f53<\/p>\n<p>ax.plot_surface(x, y, z, color=&#39;b&#39;, alpha=0.5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u62d3\u5c55\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Python\u8f7b\u677e\u5730\u7ed8\u5236\u4e09\u7ef4\u692d\u7403\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u5b9a\u5236\u548c\u4f18\u5316\u3002<\/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\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u7ed8\u5236\u4e09\u7ef4\u692d\u7403\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684mplot3d\u6a21\u5757\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5Matplotlib\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<code>pip install matplotlib<\/code>\u3002\u63a5\u7740\uff0c\u4f7f\u7528numpy\u751f\u6210\u692d\u7403\u7684\u5750\u6807\u6570\u636e\uff0c\u6700\u540e\u5229\u7528Matplotlib\u8fdb\u884c\u53ef\u89c6\u5316\u3002<\/p>\n<p><strong>\u7ed8\u5236\u4e09\u7ef4\u692d\u7403\u65f6\u9700\u8981\u54ea\u4e9b\u53c2\u6570\uff1f<\/strong><br \/>\u7ed8\u5236\u4e09\u7ef4\u692d\u7403\u65f6\uff0c\u60a8\u901a\u5e38\u9700\u8981\u5b9a\u4e49\u4e09\u4e2a\u53c2\u6570\uff1a\u692d\u7403\u7684\u957f\u8f74\u3001\u77ed\u8f74\u548c\u5bbd\u8f74\u7684\u957f\u5ea6\u3002\u8fd9\u4e09\u4e2a\u53c2\u6570\u786e\u5b9a\u4e86\u692d\u7403\u7684\u5f62\u72b6\u548c\u5927\u5c0f\u3002\u60a8\u8fd8\u53ef\u4ee5\u9009\u62e9\u692d\u7403\u7684\u4e2d\u5fc3\u4f4d\u7f6e\uff0c\u4ee5\u4fbf\u63a7\u5236\u5b83\u5728\u4e09\u7ef4\u7a7a\u95f4\u4e2d\u7684\u5750\u6807\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7ed8\u5236\u692d\u7403\u7684\u793a\u4f8b\u4ee3\u7801\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u4e09\u7ef4\u692d\u7403\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\n\n# \u5b9a\u4e49\u692d\u7403\u53c2\u6570\na, b, c = 5, 3, 2  # \u957f\u8f74\u3001\u77ed\u8f74\u548c\u5bbd\u8f74\nu = np.linspace(0, 2 * np.pi, 100)\nv = np.linspace(0, np.pi, 100)\n\n# \u8ba1\u7b97\u692d\u7403\u7684\u5750\u6807\nx = a * np.outer(np.cos(u), np.sin(v))\ny = b * np.outer(np.sin(u), np.sin(v))\nz = c * np.outer(np.ones(np.size(u)), np.cos(v))\n\n# \u7ed8\u5236\u692d\u7403\nfig = plt.figure()\nax = fig.add_subplot(111, projection=&#39;3d&#39;)\nax.plot_surface(x, y, z, color=&#39;b&#39;, alpha=0.6)\n\nax.set_xlabel(&#39;X axis&#39;)\nax.set_ylabel(&#39;Y axis&#39;)\nax.set_zlabel(&#39;Z axis&#39;)\nplt.title(&#39;3D Ellipsoid&#39;)\nplt.show()\n<\/code><\/pre>\n<p><strong>\u7ed8\u5236\u692d\u7403\u65f6\uff0c\u5982\u4f55\u8c03\u6574\u989c\u8272\u548c\u900f\u660e\u5ea6\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>color<\/code>\u53c2\u6570\u6765\u8c03\u6574\u692d\u7403\u7684\u989c\u8272\uff0c\u4f7f\u7528<code>alpha<\/code>\u53c2\u6570\u6765\u63a7\u5236\u900f\u660e\u5ea6\u3002\u53ef\u4ee5\u6307\u5b9a\u989c\u8272\u4e3a\u5b57\u7b26\u4e32\uff08\u5982<code>&#39;b&#39;<\/code>\u8868\u793a\u84dd\u8272\uff09\u6216RGB\u503c\u3002\u900f\u660e\u5ea6\u7684\u8303\u56f4\u662f\u4ece0\u52301\uff0c0\u8868\u793a\u5b8c\u5168\u900f\u660e\uff0c1\u8868\u793a\u5b8c\u5168\u4e0d\u900f\u660e\u3002\u901a\u8fc7\u8c03\u6574\u8fd9\u4e9b\u53c2\u6570\uff0c\u53ef\u4ee5\u4f7f\u692d\u7403\u7684\u5916\u89c2\u66f4\u52a0\u5438\u5f15\u4eba\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u7ed8\u5236\u4e09\u7ef4\u692d\u7403\u901a\u5e38\u4f7f\u7528matplotlib\u548cnumpy\u5e93\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u751f\u6210\u4e09\u7ef4\u5750\u6807\u6570\u636e\u5e76\u4f7f\u7528ma [&hellip;]","protected":false},"author":3,"featured_media":1046296,"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\/1046290"}],"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=1046290"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1046290\/revisions"}],"predecessor-version":[{"id":1046299,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1046290\/revisions\/1046299"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1046296"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1046290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1046290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1046290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}