{"id":934265,"date":"2024-12-26T18:32:28","date_gmt":"2024-12-26T10:32:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/934265.html"},"modified":"2024-12-26T18:32:30","modified_gmt":"2024-12-26T10:32:30","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e7%90%83","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/934265.html","title":{"rendered":"python\u5982\u4f55\u753b\u7403"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071343\/483e81b2-5a66-44ab-9aa1-53e15fdded7c.webp\" alt=\"python\u5982\u4f55\u753b\u7403\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d\uff1a<br \/>\u5728Python\u4e2d\uff0c\u7ed8\u5236\u4e00\u4e2a\u7403\u4f53\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<strong>Matplotlib\u3001Mayavi\u3001VTK<\/strong>\u7b49\u5e93\u6765\u5b9e\u73b0\u3002\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u529f\u80fd\u548c\u4f18\u52bf\u3002<strong>Matplotlib<\/strong>\u4e3b\u8981\u7528\u4e8e2D\u7ed8\u56fe\uff0c\u4f46\u4e5f\u53ef\u4ee5\u901a\u8fc7<code>mplot3d<\/code>\u6a21\u5757\u8fdb\u884c\u7b80\u5355\u76843D\u7ed8\u5236\u3002<strong>Mayavi<\/strong>\u5219\u662f\u4e00\u4e2a\u5f3a\u5927\u76843D\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u9002\u5408\u4e8e\u9700\u8981\u590d\u67423D\u56fe\u5f62\u7684\u5e94\u7528\u3002<strong>VTK<\/strong>\uff08The Visualization Toolkit\uff09\u662f\u4e00\u4e2a\u66f4\u52a0\u9ad8\u7ea7\u4e14\u590d\u6742\u7684\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u9ad8\u7ea73D\u53ef\u89c6\u5316\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u7ed8\u5236\u4e00\u4e2a\u7403\u4f53\uff0c\u5e76\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528<strong>Matplotlib<\/strong>\u7ed8\u5236\u7403\u4f53\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528Matplotlib\u7ed8\u5236\u7403\u4f53<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u867d\u7136\u4e3b\u8981\u7528\u4e8e2D\u7ed8\u56fe\uff0c\u4f46\u901a\u8fc7\u5176<code>mplot3d<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u8fdb\u884c\u7b80\u5355\u76843D\u7ed8\u5236\uff0c\u5305\u62ec\u7403\u4f53\u3002<\/p>\n<\/p>\n<ol>\n<li>\u8bbe\u7f6eMatplotlib\u73af\u5883<\/li>\n<\/ol>\n<p><p>\u5728\u5f00\u59cb\u7ed8\u5236\u7403\u4f53\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5Matplotlib\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165<code>matplotlib.pyplot<\/code>\u548c<code>mpl_toolkits.mplot3d<\/code>\u6765\u8bbe\u7f6e\u7ed8\u56fe\u73af\u5883\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>from mpl_toolkits.mplot3d import Axes3D<\/p>\n<p>import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u521b\u5efa\u7403\u4f53\u6570\u636e<\/li>\n<\/ol>\n<p><p>\u4e3a\u4e86\u7ed8\u5236\u7403\u4f53\uff0c\u9700\u8981\u751f\u6210\u7403\u4f53\u7684\u8868\u9762\u6570\u636e\u3002\u7403\u4f53\u7684\u8868\u9762\u53ef\u4ee5\u7528\u7403\u5750\u6807\u7cfb\u6765\u8868\u793a\uff0c\u901a\u8fc7<code>numpy<\/code>\u5e93\u53ef\u4ee5\u8f7b\u677e\u751f\u6210\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u7403\u4f53\u6570\u636e<\/p>\n<p>phi, theta = np.mgrid[0.0:2.0 * np.pi:100j, 0.0:np.pi:50j]<\/p>\n<p>x = np.sin(theta) * np.cos(phi)<\/p>\n<p>y = np.sin(theta) * np.sin(phi)<\/p>\n<p>z = np.cos(theta)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u7ed8\u5236\u7403\u4f53<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528Matplotlib\u7684<code>plot_surface<\/code>\u51fd\u6570\u7ed8\u5236\u7403\u4f53\u8868\u9762\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;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u7b80\u5355\u5730\u4f7f\u7528Matplotlib\u7ed8\u5236\u4e00\u4e2a\u7403\u4f53\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u5feb\u901f\u53ef\u89c6\u5316\u7684\u7b80\u53553D\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528Mayavi\u7ed8\u5236\u7403\u4f53<\/p>\n<\/p>\n<p><p>Mayavi\u662f\u4e00\u4e2a\u5f3a\u5927\u76843D\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u9002\u5408\u7528\u4e8e\u9700\u8981\u590d\u67423D\u56fe\u5f62\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Mayavi<\/li>\n<\/ol>\n<p><p>Mayavi\u4f9d\u8d56\u4e8e\u591a\u4e2a\u5e93\uff0c\u901a\u5e38\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 mayavi<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u521b\u5efa\u7403\u4f53<\/li>\n<\/ol>\n<p><p>Mayavi\u63d0\u4f9b\u4e86\u66f4\u52a0\u76f4\u89c2\u7684\u63a5\u53e3\u6765\u521b\u5efa\u548c\u64cd\u4f5c3D\u56fe\u5f62\u3002\u53ef\u4ee5\u4f7f\u7528<code>mayavi.mlab<\/code>\u6a21\u5757\u6765\u521b\u5efa\u4e00\u4e2a\u7403\u4f53\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=2, resolution=50, color=(0, 0, 1), opacity=0.5)<\/p>\n<p>mlab.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Mayavi\u7684\u4f18\u52bf\u5728\u4e8e\u5176\u5f3a\u5927\u76843D\u5904\u7406\u80fd\u529b\uff0c\u9002\u5408\u7528\u4e8e\u9700\u8981\u4ea4\u4e92\u5f0f\u64cd\u4f5c\u548c\u590d\u6742\u6e32\u67d3\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528VTK\u7ed8\u5236\u7403\u4f53<\/p>\n<\/p>\n<p><p>VTK\u662f\u4e00\u4e2a\u7528\u4e8e3D\u8ba1\u7b97\u673a\u56fe\u5f62\u3001\u56fe\u50cf\u5904\u7406\u548c\u53ef\u89c6\u5316\u7684\u5f00\u6e90\u8f6f\u4ef6\u7cfb\u7edf\uff0c\u9002\u5408\u4e8e\u9ad8\u7ea73D\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5VTK<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5VTK\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install vtk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u521b\u5efa\u7403\u4f53<\/li>\n<\/ol>\n<p><p>VTK\u63d0\u4f9b\u4e86\u4e30\u5bcc\u76843D\u56fe\u5f62\u529f\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528<code>vtkSphereSource<\/code>\u521b\u5efa\u4e00\u4e2a\u7403\u4f53\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import vtk<\/p>\n<h2><strong>\u521b\u5efa\u7403\u4f53\u6e90<\/strong><\/h2>\n<p>sphereSource = vtk.vtkSphereSource()<\/p>\n<p>sphereSource.SetRadius(1.0)<\/p>\n<p>sphereSource.SetThetaResolution(50)<\/p>\n<p>sphereSource.SetPhiResolution(50)<\/p>\n<h2><strong>\u6620\u5c04\u5668<\/strong><\/h2>\n<p>mapper = vtk.vtkPolyDataMapper()<\/p>\n<p>mapper.SetInputConnection(sphereSource.GetOutputPort())<\/p>\n<h2><strong>\u6f14\u5458<\/strong><\/h2>\n<p>actor = vtk.vtkActor()<\/p>\n<p>actor.SetMapper(mapper)<\/p>\n<h2><strong>\u6e32\u67d3\u5668<\/strong><\/h2>\n<p>renderer = vtk.vtkRenderer()<\/p>\n<p>renderer.AddActor(actor)<\/p>\n<p>renderer.SetBackground(1, 1, 1)<\/p>\n<h2><strong>\u6e32\u67d3\u7a97\u53e3<\/strong><\/h2>\n<p>renderWindow = vtk.vtkRenderWindow()<\/p>\n<p>renderWindow.AddRenderer(renderer)<\/p>\n<h2><strong>\u6e32\u67d3\u7a97\u53e3\u4ea4\u4e92<\/strong><\/h2>\n<p>renderWindowInteractor = vtk.vtkRenderWindowInteractor()<\/p>\n<p>renderWindowInteractor.SetRenderWindow(renderWindow)<\/p>\n<h2><strong>\u5f00\u59cb\u6e32\u67d3<\/strong><\/h2>\n<p>renderWindow.Render()<\/p>\n<p>renderWindowInteractor.Start()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>VTK\u9002\u5408\u7528\u4e8e\u590d\u6742\u7684\u79d1\u5b66\u8ba1\u7b97\u548c\u9700\u8981\u9ad8\u6027\u80fd\u6e32\u67d3\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177<\/p>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u7ed8\u5236\u7403\u4f53\u7684\u5de5\u5177\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\u8fdb\u884c\u9009\u62e9\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7b80\u53553D\u7ed8\u5236<\/strong>\uff1a\u5982\u679c\u60a8\u7684\u9700\u6c42\u53ea\u662f\u7b80\u5355\u76843D\u53ef\u89c6\u5316\uff0cMatplotlib\u8db3\u4ee5\u80dc\u4efb\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4ea4\u4e92\u5f0f\u548c\u590d\u67423D\u6e32\u67d3<\/strong>\uff1a\u5bf9\u4e8e\u9700\u8981\u4ea4\u4e92\u5f0f\u64cd\u4f5c\u548c\u590d\u6742\u6e32\u67d3\u7684\u5e94\u7528\uff0cMayavi\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9ad8\u7ea7\u79d1\u5b66\u8ba1\u7b97\u548c\u53ef\u89c6\u5316<\/strong>\uff1a\u5bf9\u4e8e\u9700\u8981\u9ad8\u7ea7\u79d1\u5b66\u8ba1\u7b97\u548c\u9ad8\u6027\u80fd\u6e32\u67d3\u7684\u5e94\u7528\uff0cVTK\u662f\u6700\u4f73\u9009\u62e9\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u6765\u7ed8\u5236\u7403\u4f53\uff0c\u6bcf\u79cd\u5de5\u5177\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u3002\u901a\u8fc7\u4f7f\u7528Matplotlib\u3001Mayavi\u548cVTK\uff0c\u7528\u6237\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u8fdb\u884c3D\u53ef\u89c6\u5316\u3002\u65e0\u8bba\u662f\u7b80\u5355\u76843D\u6a21\u578b\u7ed8\u5236\uff0c\u8fd8\u662f\u590d\u6742\u7684\u79d1\u5b66\u8ba1\u7b97\u548c\u53ef\u89c6\u5316\uff0cPython\u90fd\u80fd\u63d0\u4f9b\u5f3a\u5927\u7684\u652f\u6301\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u60a8\u5df2\u7ecf\u638c\u63e1\u4e86\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u4e00\u4e2a\u7403\u4f53\u7684\u57fa\u672c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa3D\u7403\u4f53\u7684\u53ef\u89c6\u5316\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u521b\u5efa3D\u7403\u4f53\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>Axes3D<\/code>\u6a21\u5757\u4ee5\u53ca\u53c2\u6570\u5316\u65b9\u7a0b\u6765\u7ed8\u5236\u7403\u4f53\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u8bbe\u7f6e\u7403\u7684\u534a\u5f84\u3001\u5b9a\u4e49\u7403\u7684\u7ecf\u7eac\u5ea6\uff0c\u5e76\u5229\u7528<code>plot_surface<\/code>\u65b9\u6cd5\u6765\u7ed8\u5236\u7403\u4f53\u7684\u8868\u9762\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u7ed8\u5236\u7403\u4f53\u65f6\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u5e93\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u6709Matplotlib\u3001Mayavi\u548cVPython\u3002Matplotlib\u9002\u5408\u57fa\u7840\u76843D\u56fe\u5f62\u7ed8\u5236\uff0cMayavi\u9002\u5408\u590d\u6742\u7684\u79d1\u5b66\u8ba1\u7b97\u548c\u53ef\u89c6\u5316\uff0c\u800cVPython\u5219\u63d0\u4f9b\u4e86\u66f4\u6613\u4e8e\u4f7f\u7528\u76843D\u52a8\u753b\u548c\u4ea4\u4e92\u5f0f\u56fe\u5f62\u754c\u9762\u3002\u6839\u636e\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u8fbe\u5230\u6700\u4f73\u7684\u6548\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u8c03\u6574Python\u7ed8\u5236\u7684\u7403\u4f53\u7684\u989c\u8272\u548c\u900f\u660e\u5ea6\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Matplotlib\u7ed8\u5236\u7403\u4f53\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>facecolors<\/code>\u548c<code>alpha<\/code>\u53c2\u6570\u6765\u8c03\u6574\u7403\u4f53\u7684\u989c\u8272\u548c\u900f\u660e\u5ea6\u3002<code>facecolors<\/code>\u53ef\u4ee5\u8bbe\u7f6e\u7403\u4f53\u7684\u586b\u5145\u989c\u8272\uff0c\u800c<code>alpha<\/code>\u5219\u53ef\u4ee5\u63a7\u5236\u900f\u660e\u5ea6\u503c\uff0c\u8303\u56f4\u4ece0\u52301\uff0c0\u8868\u793a\u5b8c\u5168\u900f\u660e\uff0c1\u8868\u793a\u4e0d\u900f\u660e\u3002\u901a\u8fc7\u8fd9\u4e9b\u53c2\u6570\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u4e30\u5bcc\u7684\u89c6\u89c9\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1a\u5728Python\u4e2d\uff0c\u7ed8\u5236\u4e00\u4e2a\u7403\u4f53\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Matplotlib\u3001Mayavi\u3001VTK\u7b49\u5e93\u6765\u5b9e\u73b0\u3002\u6bcf\u4e2a [&hellip;]","protected":false},"author":3,"featured_media":934270,"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\/934265"}],"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=934265"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/934265\/revisions"}],"predecessor-version":[{"id":934271,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/934265\/revisions\/934271"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/934270"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=934265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=934265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=934265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}