{"id":946525,"date":"2024-12-26T23:43:12","date_gmt":"2024-12-26T15:43:12","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/946525.html"},"modified":"2024-12-26T23:43:14","modified_gmt":"2024-12-26T15:43:14","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e7%90%83%e4%bd%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/946525.html","title":{"rendered":"python\u5982\u4f55\u753b\u7403\u4f53"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082650\/6eaf90c6-bd2f-4336-ba39-57a006e8e9e5.webp\" alt=\"python\u5982\u4f55\u753b\u7403\u4f53\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u753b\u4e00\u4e2a\u7403\u4f53\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528matplotlib\u5e93\u7684mplot3d\u6a21\u5757\u3001\u4f7f\u7528mayavi\u5e93\u3001\u4f7f\u7528vpython\u5e93\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff1amatplotlib\u4fbf\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3001mayavi\u63d0\u4f9b\u66f4\u9ad8\u8d28\u91cf\u76843D\u6e32\u67d3\u3001vpython\u5219\u4e13\u6ce8\u4e8e\u7269\u7406\u6a21\u62df\u548c\u4ea4\u4e92\u3002<\/strong> \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u7ed8\u5236\u4e00\u4e2a\u7403\u4f53\uff0c\u5e76\u8ba8\u8bba\u6bcf\u79cd\u65b9\u6cd5\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001MATPLOTLIB\u7ed8\u5236\u7403\u4f53<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u5176mplot3d\u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u7b80\u5355\u76843D\u7ed8\u56fe\u3002\u867d\u7136\u5b83\u76843D\u6e32\u67d3\u80fd\u529b\u4e0d\u5982\u5176\u4ed6\u4e13\u4e1a\u76843D\u5e93\uff0c\u4f46\u5bf9\u4e8e\u7b80\u5355\u7684\u53ef\u89c6\u5316\u4efb\u52a1\u5df2\u7ecf\u8db3\u591f\u3002<\/p>\n<\/p>\n<ol>\n<li>\u751f\u6210\u7403\u4f53\u7684\u7f51\u683c<\/li>\n<\/ol>\n<p><p>\u8981\u7ed8\u5236\u4e00\u4e2a\u7403\u4f53\uff0c\u9996\u5148\u9700\u8981\u751f\u6210\u7403\u4f53\u7684\u7f51\u683c\u5750\u6807\u3002\u7403\u4f53\u7684\u8868\u9762\u53ef\u4ee5\u7528\u7403\u5750\u6807\u7cfb\u6765\u63cf\u8ff0\uff0c\u5c06\u7403\u5750\u6807\u8f6c\u6362\u4e3a\u7b1b\u5361\u5c14\u5750\u6807\u5373\u53ef\u5f97\u5230\u7403\u4f53\u7684\u7f51\u683c\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>\u751f\u6210\u7403\u5750\u6807<\/strong><\/h2>\n<p>phi = np.linspace(0, np.pi, 100)<\/p>\n<p>theta = np.linspace(0, 2 * np.pi, 100)<\/p>\n<p>phi, theta = np.meshgrid(phi, theta)<\/p>\n<h2><strong>\u7403\u5750\u6807\u8f6c\u6362\u4e3a\u7b1b\u5361\u5c14\u5750\u6807<\/strong><\/h2>\n<p>x = np.sin(phi) * np.cos(theta)<\/p>\n<p>y = np.sin(phi) * np.sin(theta)<\/p>\n<p>z = np.cos(phi)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u7ed8\u5236\u7403\u4f53<\/li>\n<\/ol>\n<p><p>\u751f\u6210\u7f51\u683c\u5750\u6807\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plot_surface<\/code>\u51fd\u6570\u7ed8\u5236\u7403\u4f53\u3002<\/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<p>ax.plot_surface(x, y, z, color=&#39;b&#39;, alpha=0.6)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u5feb\u901f\u8fdb\u884c3D\u6570\u636e\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001MAYAVI\u7ed8\u5236\u7403\u4f53<\/p>\n<\/p>\n<p><p>Mayavi\u662f\u4e00\u4e2a\u5f3a\u5927\u76843D\u53ef\u89c6\u5316\u5e93\uff0c\u80fd\u591f\u63d0\u4f9b\u9ad8\u8d28\u91cf\u76843D\u6e32\u67d3\u6548\u679c\u3002\u76f8\u6bd4\u4e8ematplotlib\uff0cmayavi\u66f4\u9002\u5408\u590d\u6742\u76843D\u7ed8\u56fe\u548c\u52a8\u753b\u5236\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Mayavi<\/li>\n<\/ol>\n<p><p>\u9996\u5148\u9700\u8981\u5b89\u88c5Mayavi\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install mayavi<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Mayavi\u7ed8\u5236\u7403\u4f53<\/li>\n<\/ol>\n<p><p>Mayavi\u63d0\u4f9b\u4e86\u4e13\u95e8\u7684\u51fd\u6570\u6765\u751f\u6210\u7403\u4f53\uff0c\u56e0\u6b64\u7ed8\u5236\u7403\u4f53\u76f8\u5bf9\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mayavi import mlab<\/p>\n<h2><strong>\u521b\u5efa\u7403\u4f53<\/strong><\/h2>\n<p>sphere = mlab.points3d(0, 0, 0, scale_factor=1, resolution=50, color=(0, 0, 1), opacity=0.5, mode=&#39;sphere&#39;)<\/p>\n<h2><strong>\u663e\u793a\u7403\u4f53<\/strong><\/h2>\n<p>mlab.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Mayavi\u63d0\u4f9b\u7684\u6e32\u67d3\u6548\u679c\u6bd4matplotlib\u66f4\u903c\u771f\uff0c\u53ef\u4ee5\u8c03\u6574\u5206\u8fa8\u7387\u548c\u989c\u8272\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u89c6\u89c9\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001VPYTHON\u7ed8\u5236\u7403\u4f53<\/p>\n<\/p>\n<p><p>VPython\u662f\u4e00\u4e2a\u4e13\u4e3a\u7269\u7406\u6a21\u62df\u548c\u4ea4\u4e92\u5f0f3D\u56fe\u5f62\u8bbe\u8ba1\u7684\u5e93\u3002\u5b83\u4f7f\u5f973D\u7f16\u7a0b\u53d8\u5f97\u7b80\u5355\u76f4\u89c2\uff0c\u975e\u5e38\u9002\u5408\u6559\u5b66\u548c\u5feb\u901f\u5f00\u53d1\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5VPython<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5VPython\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install vpython<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528VPython\u7ed8\u5236\u7403\u4f53<\/li>\n<\/ol>\n<p><p>VPython\u7684\u8bed\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u51e0\u884c\u4ee3\u7801\u5373\u53ef\u7ed8\u5236\u4e00\u4e2a\u7403\u4f53\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from vpython import sphere, vector<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7403\u4f53<\/strong><\/h2>\n<p>ball = sphere(pos=vector(0, 0, 0), radius=1, color=vector(0, 0, 1))<\/p>\n<h2><strong>\u8fd0\u884c\u52a8\u753b<\/strong><\/h2>\n<p>while True:<\/p>\n<p>    rate(30)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>VPython\u4e0d\u4ec5\u53ef\u4ee5\u7ed8\u5236\u7403\u4f53\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7269\u7406\u6a21\u62df\u529f\u80fd\uff0c\u5982\u8fd0\u52a8\u3001\u78b0\u649e\u7b49\uff0c\u975e\u5e38\u9002\u5408\u7269\u7406\u6559\u5b66\u548c\u6f14\u793a\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3\u4e0e\u6bd4\u8f83<\/p>\n<\/p>\n<ol>\n<li>\n<p>Matplotlib\uff1a\u9002\u5408\u7b80\u5355\u76843D\u53ef\u89c6\u5316\uff0c\u6613\u4e8e\u4f7f\u7528\u548c\u96c6\u6210\uff0c\u4f46\u6e32\u67d3\u6548\u679c\u4e00\u822c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>Mayavi\uff1a\u63d0\u4f9b\u9ad8\u8d28\u91cf\u76843D\u6e32\u67d3\uff0c\u9002\u5408\u590d\u6742\u76843D\u7ed8\u56fe\u548c\u52a8\u753b\uff0c\u4f46\u5b89\u88c5\u548c\u4f7f\u7528\u8f83\u590d\u6742\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>VPython\uff1a\u4e13\u6ce8\u4e8e\u7269\u7406\u6a21\u62df\u548c\u4ea4\u4e92\uff0c\u7b80\u5355\u76f4\u89c2\uff0c\u9002\u5408\u6559\u5b66\u548c\u5feb\u901f\u5f00\u53d1\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p><strong>\u5728\u9009\u62e9\u7ed8\u56fe\u5de5\u5177\u65f6\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u3002<\/strong> \u5982\u679c\u53ea\u662f\u9700\u8981\u5feb\u901f\u751f\u6210\u4e00\u4e2a\u7403\u4f53\u7684\u56fe\u50cf\uff0cmatplotlib\u8db3\u591f\uff1b\u5982\u679c\u9700\u8981\u9ad8\u8d28\u91cf\u76843D\u6548\u679c\uff0cmayavi\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u5982\u679c\u9700\u8981\u8fdb\u884c\u4ea4\u4e92\u548c\u7269\u7406\u6a21\u62df\uff0cVPython\u662f\u7406\u60f3\u7684\u5de5\u5177\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u81ea\u5df1\u7684\u9002\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u4e00\u4e2a\u7b80\u5355\u76843D\u7403\u4f53\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u7684mplot3d\u5de5\u5177\u7ed8\u52363D\u7403\u4f53\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Matplotlib\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install matplotlib<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u7136\u540e\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u521b\u5efa\u4e00\u4e2a3D\u7403\u4f53\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\n\nu = np.linspace(0, 2 * np.pi, 100)\nv = np.linspace(0, np.pi, 100)\nx = 10 * np.outer(np.cos(u), np.sin(v))\ny = 10 * np.outer(np.sin(u), np.sin(v))\nz = 10 * np.outer(np.ones(np.size(u)), np.cos(v))\n\nfig = plt.figure()\nax = fig.add_subplot(111, projection=&#39;3d&#39;)\nax.plot_surface(x, y, z, color=&#39;b&#39;, alpha=0.5)\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u751f\u6210\u4e00\u4e2a\u84dd\u8272\u900f\u660e\u7684\u7403\u4f53\u3002<\/p>\n<p><strong>\u9664\u4e86Matplotlib\uff0c\u8fd8\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u7403\u4f53\uff1f<\/strong><br \/>\u9664\u4e86Matplotlib\uff0c\u5176\u4ed6\u4e00\u4e9b\u6d41\u884c\u7684Python\u5e93\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u7403\u4f53\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Mayavi\u6216Plotly\u7b49\u5e93\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u66f4\u4e30\u5bcc\u76843D\u53ef\u89c6\u5316\u529f\u80fd\u3002Mayavi\u9002\u5408\u5904\u7406\u590d\u6742\u7684\u79d1\u5b66\u53ef\u89c6\u5316\uff0c\u800cPlotly\u5219\u5141\u8bb8\u7528\u6237\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u5f62\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53d6\u51b3\u4e8e\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u548c\u4e2a\u4eba\u504f\u597d\u3002<\/p>\n<p><strong>\u7ed8\u5236\u7403\u4f53\u65f6\u5982\u4f55\u8c03\u6574\u5176\u989c\u8272\u548c\u900f\u660e\u5ea6\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u7ed8\u5236\u7403\u4f53\u65f6\u53ef\u4ee5\u901a\u8fc7<code>plot_surface<\/code>\u65b9\u6cd5\u7684\u53c2\u6570\u6765\u8c03\u6574\u989c\u8272\u548c\u900f\u660e\u5ea6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>color<\/code>\u53c2\u6570\u8bbe\u7f6e\u7403\u4f53\u989c\u8272\uff0c\u4f7f\u7528<code>alpha<\/code>\u53c2\u6570\u8bbe\u7f6e\u900f\u660e\u5ea6\u3002\u5c06<code>alpha<\/code>\u8bbe\u7f6e\u4e3a0.5\u53ef\u4ee5\u8ba9\u7403\u4f53\u534a\u900f\u660e\uff0c\u800c\u8bbe\u7f6e\u4e3a1\u5219\u4e3a\u5b8c\u5168\u4e0d\u900f\u660e\u3002\u901a\u8fc7\u8fd9\u4e9b\u53c2\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u7531\u8c03\u6574\u7403\u4f53\u7684\u5916\u89c2\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u753b\u4e00\u4e2a\u7403\u4f53\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528matplotlib\u5e93\u7684mplot3d\u6a21\u5757\u3001\u4f7f\u7528m [&hellip;]","protected":false},"author":3,"featured_media":946533,"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\/946525"}],"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=946525"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/946525\/revisions"}],"predecessor-version":[{"id":946536,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/946525\/revisions\/946536"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/946533"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=946525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=946525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=946525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}