{"id":1108101,"date":"2025-01-08T16:56:43","date_gmt":"2025-01-08T08:56:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1108101.html"},"modified":"2025-01-08T16:56:51","modified_gmt":"2025-01-08T08:56:51","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e5%87%ba%e6%bc%82%e4%ba%ae%e7%9a%843d%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1108101.html","title":{"rendered":"python\u5982\u4f55\u753b\u51fa\u6f02\u4eae\u76843D\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071857\/42ee00a9-2670-4c6a-bc7b-7a621c241bc0.webp\" alt=\"python\u5982\u4f55\u753b\u51fa\u6f02\u4eae\u76843D\u56fe\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d:<\/p>\n<p><strong>Python\u753b\u51fa\u6f02\u4eae\u76843D\u56fe\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528Matplotlib\u3001Mayavi\u3001Plotly\u3001PyOpenGL\u7b49\u5de5\u5177\u3002<\/strong> \u63a8\u8350\u4f7f\u7528Matplotlib\u548cPlotly\uff0c\u56e0\u4e3a\u5b83\u4eec\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u7ed8\u5236\u7f8e\u89c2\u76843D\u56fe\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u9645\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001MATPLOTLIB\u7ed8\u52363D\u56fe<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\u4e4b\u4e00\u3002\u5b83\u5305\u542b\u4e86\u5927\u91cf\u7684\u7ed8\u56fe\u5de5\u5177\uff0c\u53ef\u4ee5\u7ed8\u5236\u5404\u79cd2D\u548c3D\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5Matplotlib<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Matplotlib\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u6765\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>2. \u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u52363D\u56fe\u4e4b\u524d\uff0c\u9700\u8981\u5bfc\u5165Matplotlib\u548cNumpy\u5e93\uff1a<\/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<p><h3>3. \u521b\u5efa3D\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u76843D\u56fe\u5f62\uff0c\u4f8b\u5982\u4e00\u4e2a3D\u6563\u70b9\u56fe\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>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = np.random.rand(100)<\/p>\n<p>y = np.random.rand(100)<\/p>\n<p>z = np.random.rand(100)<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>ax.scatter(x, y, z)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>ax.set_title(&#39;3D Scatter Plot&#39;)<\/p>\n<p>ax.set_xlabel(&#39;X axis&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Y axis&#39;)<\/p>\n<p>ax.set_zlabel(&#39;Z axis&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u5c06\u521b\u5efa\u4e00\u4e2a3D\u6563\u70b9\u56fe\uff0c\u5e76\u4e3a\u5750\u6807\u8f74\u6dfb\u52a0\u6807\u7b7e\u548c\u6807\u9898\u3002\u901a\u8fc7\u8c03\u6574\u6570\u636e\u548c\u6837\u5f0f\uff0c\u53ef\u4ee5\u521b\u5efa\u66f4\u590d\u6742\u548c\u6f02\u4eae\u76843D\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h3>4. \u7ed8\u52363D\u66f2\u9762\u56fe<\/h3>\n<\/p>\n<p><p>3D\u66f2\u9762\u56fe\u662f\u53e6\u4e00\u79cd\u5e38\u7528\u76843D\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = np.linspace(-5, 5, 100)<\/p>\n<p>y = np.linspace(-5, 5, 100)<\/p>\n<p>x, y = np.meshgrid(x, y)<\/p>\n<p>z = np.sin(np.sqrt(x&lt;strong&gt;2 + y&lt;\/strong&gt;2))<\/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, cmap=&#39;viridis&#39;)<\/p>\n<p>ax.set_title(&#39;3D Surface Plot&#39;)<\/p>\n<p>ax.set_xlabel(&#39;X axis&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Y axis&#39;)<\/p>\n<p>ax.set_zlabel(&#39;Z axis&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>plot_surface<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a3D\u66f2\u9762\u56fe\uff0c\u5e76\u4f7f\u7528<code>viridis<\/code>\u8272\u5f69\u6620\u5c04\u6765\u589e\u5f3a\u56fe\u5f62\u7684\u7f8e\u89c2\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>5. \u7f8e\u53163D\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>Matplotlib\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u81ea\u5b9a\u4e49\u9009\u9879\uff0c\u53ef\u4ee5\u7528\u6765\u7f8e\u53163D\u56fe\u5f62\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8c03\u6574\u989c\u8272\u6620\u5c04\u3001\u6dfb\u52a0\u989c\u8272\u6761\u3001\u8bbe\u7f6e\u900f\u660e\u5ea6\u7b49\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<p>surf = ax.plot_surface(x, y, z, cmap=&#39;coolwarm&#39;, edgecolor=&#39;none&#39;, alpha=0.8)<\/p>\n<h2><strong>\u6dfb\u52a0\u989c\u8272\u6761<\/strong><\/h2>\n<p>fig.colorbar(surf, ax=ax, shrink=0.5, aspect=5)<\/p>\n<p>ax.set_title(&#39;Beautified 3D Surface Plot&#39;)<\/p>\n<p>ax.set_xlabel(&#39;X axis&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Y axis&#39;)<\/p>\n<p>ax.set_zlabel(&#39;Z axis&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u8c03\u6574\uff0c\u53ef\u4ee5\u8ba93D\u56fe\u5f62\u53d8\u5f97\u66f4\u52a0\u7f8e\u89c2\u548c\u4fe1\u606f\u4e30\u5bcc\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001PLOTLY\u7ed8\u52363D\u56fe<\/p>\n<\/p>\n<p><p>Plotly\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u5e93\uff0c\u652f\u6301\u9ad8\u8d28\u91cf\u7684\u56fe\u5f62\u6e32\u67d3\u548c\u4e30\u5bcc\u7684\u4ea4\u4e92\u529f\u80fd\u3002\u5b83\u5728\u7ed8\u52363D\u56fe\u5f62\u65b9\u9762\u4e5f\u975e\u5e38\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5Plotly<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Plotly\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install plotly<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u52363D\u56fe\u4e4b\u524d\uff0c\u9700\u8981\u5bfc\u5165Plotly\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.graph_objects as go<\/p>\n<p>import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u521b\u5efa3D\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u521b\u5efa3D\u6563\u70b9\u56fe\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6570\u636e<\/p>\n<p>x = np.random.rand(100)<\/p>\n<p>y = np.random.rand(100)<\/p>\n<p>z = np.random.rand(100)<\/p>\n<p>fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z, mode=&#39;markers&#39;)])<\/p>\n<p>fig.update_layout(title=&#39;3D Scatter Plot&#39;,<\/p>\n<p>                  scene=dict(xaxis_title=&#39;X axis&#39;,<\/p>\n<p>                             yaxis_title=&#39;Y axis&#39;,<\/p>\n<p>                             zaxis_title=&#39;Z axis&#39;))<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u5c06\u521b\u5efa\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u76843D\u6563\u70b9\u56fe\uff0c\u5e76\u4e3a\u5750\u6807\u8f74\u6dfb\u52a0\u6807\u7b7e\u548c\u6807\u9898\u3002<\/p>\n<\/p>\n<p><h3>4. \u7ed8\u52363D\u66f2\u9762\u56fe<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u521b\u5efa3D\u66f2\u9762\u56fe\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = np.linspace(-5, 5, 100)<\/p>\n<p>y = np.linspace(-5, 5, 100)<\/p>\n<p>x, y = np.meshgrid(x, y)<\/p>\n<p>z = np.sin(np.sqrt(x&lt;strong&gt;2 + y&lt;\/strong&gt;2))<\/p>\n<p>fig = go.Figure(data=[go.Surface(z=z, x=x, y=y)])<\/p>\n<p>fig.update_layout(title=&#39;3D Surface Plot&#39;,<\/p>\n<p>                  scene=dict(xaxis_title=&#39;X axis&#39;,<\/p>\n<p>                             yaxis_title=&#39;Y axis&#39;,<\/p>\n<p>                             zaxis_title=&#39;Z axis&#39;))<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Plotly\u521b\u5efa\u4e00\u4e2a3D\u66f2\u9762\u56fe\uff0c\u5e76\u4e3a\u56fe\u5f62\u6dfb\u52a0\u6807\u9898\u548c\u5750\u6807\u8f74\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h3>5. \u7f8e\u53163D\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>Plotly\u4e5f\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u81ea\u5b9a\u4e49\u9009\u9879\uff0c\u53ef\u4ee5\u7528\u6765\u7f8e\u53163D\u56fe\u5f62\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8c03\u6574\u989c\u8272\u6620\u5c04\u3001\u6dfb\u52a0\u989c\u8272\u6761\u3001\u8bbe\u7f6e\u900f\u660e\u5ea6\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig = go.Figure(data=[go.Surface(z=z, x=x, y=y, colorscale=&#39;Viridis&#39;, opacity=0.8)])<\/p>\n<p>fig.update_layout(title=&#39;Beautified 3D Surface Plot&#39;,<\/p>\n<p>                  scene=dict(xaxis_title=&#39;X axis&#39;,<\/p>\n<p>                             yaxis_title=&#39;Y axis&#39;,<\/p>\n<p>                             zaxis_title=&#39;Z axis&#39;))<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u8c03\u6574\uff0c\u53ef\u4ee5\u8ba93D\u56fe\u5f62\u53d8\u5f97\u66f4\u52a0\u7f8e\u89c2\u548c\u4fe1\u606f\u4e30\u5bcc\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001MAYAVI\u7ed8\u52363D\u56fe<\/p>\n<\/p>\n<p><p>Mayavi\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u76843D\u79d1\u5b66\u6570\u636e\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u9002\u5408\u7528\u4e8e\u590d\u6742\u76843D\u7ed8\u56fe\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5Mayavi<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5Mayavi\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install mayavi<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u52363D\u56fe\u4e4b\u524d\uff0c\u9700\u8981\u5bfc\u5165Mayavi\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mayavi import mlab<\/p>\n<p>import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u521b\u5efa3D\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u521b\u5efa3D\u6563\u70b9\u56fe\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6570\u636e<\/p>\n<p>x = np.random.rand(100)<\/p>\n<p>y = np.random.rand(100)<\/p>\n<p>z = np.random.rand(100)<\/p>\n<p>mlab.points3d(x, y, z, mode=&#39;sphere&#39;, scale_factor=0.1)<\/p>\n<p>mlab.title(&#39;3D Scatter Plot&#39;)<\/p>\n<p>mlab.xlabel(&#39;X axis&#39;)<\/p>\n<p>mlab.ylabel(&#39;Y axis&#39;)<\/p>\n<p>mlab.zlabel(&#39;Z axis&#39;)<\/p>\n<p>mlab.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u5c06\u521b\u5efa\u4e00\u4e2a3D\u6563\u70b9\u56fe\uff0c\u5e76\u4e3a\u5750\u6807\u8f74\u6dfb\u52a0\u6807\u7b7e\u548c\u6807\u9898\u3002<\/p>\n<\/p>\n<p><h3>4. \u7ed8\u52363D\u66f2\u9762\u56fe<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u521b\u5efa3D\u66f2\u9762\u56fe\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = np.linspace(-5, 5, 100)<\/p>\n<p>y = np.linspace(-5, 5, 100)<\/p>\n<p>x, y = np.meshgrid(x, y)<\/p>\n<p>z = np.sin(np.sqrt(x&lt;strong&gt;2 + y&lt;\/strong&gt;2))<\/p>\n<p>mlab.surf(x, y, z, colormap=&#39;cool&#39;)<\/p>\n<p>mlab.title(&#39;3D Surface Plot&#39;)<\/p>\n<p>mlab.xlabel(&#39;X axis&#39;)<\/p>\n<p>mlab.ylabel(&#39;Y axis&#39;)<\/p>\n<p>mlab.zlabel(&#39;Z axis&#39;)<\/p>\n<p>mlab.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Mayavi\u521b\u5efa\u4e00\u4e2a3D\u66f2\u9762\u56fe\uff0c\u5e76\u4e3a\u56fe\u5f62\u6dfb\u52a0\u6807\u9898\u548c\u5750\u6807\u8f74\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h3>5. \u7f8e\u53163D\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>Mayavi\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u81ea\u5b9a\u4e49\u9009\u9879\uff0c\u53ef\u4ee5\u7528\u6765\u7f8e\u53163D\u56fe\u5f62\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8c03\u6574\u989c\u8272\u6620\u5c04\u3001\u6dfb\u52a0\u989c\u8272\u6761\u3001\u8bbe\u7f6e\u900f\u660e\u5ea6\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mlab.surf(x, y, z, colormap=&#39;cool&#39;, opacity=0.8)<\/p>\n<p>mlab.title(&#39;Beautified 3D Surface Plot&#39;)<\/p>\n<p>mlab.xlabel(&#39;X axis&#39;)<\/p>\n<p>mlab.ylabel(&#39;Y axis&#39;)<\/p>\n<p>mlab.zlabel(&#39;Z axis&#39;)<\/p>\n<p>mlab.colorbar(title=&#39;Color Scale&#39;, orientation=&#39;vertical&#39;)<\/p>\n<p>mlab.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u8c03\u6574\uff0c\u53ef\u4ee5\u8ba93D\u56fe\u5f62\u53d8\u5f97\u66f4\u52a0\u7f8e\u89c2\u548c\u4fe1\u606f\u4e30\u5bcc\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001PYOPENGL\u7ed8\u52363D\u56fe<\/p>\n<\/p>\n<p><p>PyOpenGL\u662f\u4e00\u4e2aPython\u7ed1\u5b9a\u7684OpenGL\u5e93\uff0c\u9002\u5408\u7528\u4e8e\u521b\u5efa\u590d\u6742\u548c\u9ad8\u6027\u80fd\u76843D\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5PyOpenGL<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5b89\u88c5PyOpenGL\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyOpenGL PyOpenGL_accelerate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u52363D\u56fe\u4e4b\u524d\uff0c\u9700\u8981\u5bfc\u5165PyOpenGL\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from OpenGL.GL import *<\/p>\n<p>from OpenGL.GLUT import *<\/p>\n<p>from OpenGL.GLU import *<\/p>\n<p>import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u521b\u5efa3D\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u521b\u5efa3D\u6563\u70b9\u56fe\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def draw_points():<\/p>\n<p>    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)<\/p>\n<p>    glLoadIdentity()<\/p>\n<p>    gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0)<\/p>\n<p>    glBegin(GL_POINTS)<\/p>\n<p>    for _ in range(100):<\/p>\n<p>        glVertex3f(np.random.rand(), np.random.rand(), np.random.rand())<\/p>\n<p>    glEnd()<\/p>\n<p>    glutSwapBuffers()<\/p>\n<p>glutInit()<\/p>\n<p>glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)<\/p>\n<p>glutInitWindowSize(800, 600)<\/p>\n<p>glutCreateWindow(b&#39;3D Scatter Plot&#39;)<\/p>\n<p>glutDisplayFunc(draw_points)<\/p>\n<p>glutM<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\u5c06\u521b\u5efa\u4e00\u4e2a3D\u6563\u70b9\u56fe\uff0c\u5e76\u8bbe\u7f6eOpenGL\u7a97\u53e3\u7684\u57fa\u672c\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h3>4. \u7ed8\u52363D\u66f2\u9762\u56fe<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u521b\u5efa3D\u66f2\u9762\u56fe\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def draw_surface():<\/p>\n<p>    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)<\/p>\n<p>    glLoadIdentity()<\/p>\n<p>    gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0)<\/p>\n<p>    x = np.linspace(-5, 5, 100)<\/p>\n<p>    y = np.linspace(-5, 5, 100)<\/p>\n<p>    x, y = np.meshgrid(x, y)<\/p>\n<p>    z = np.sin(np.sqrt(x&lt;strong&gt;2 + y&lt;\/strong&gt;2))<\/p>\n<p>    glBegin(GL_QUADS)<\/p>\n<p>    for i in range(len(x)-1):<\/p>\n<p>        for j in range(len(y)-1):<\/p>\n<p>            glVertex3f(x[i, j], y[i, j], z[i, j])<\/p>\n<p>            glVertex3f(x[i+1, j], y[i+1, j], z[i+1, j])<\/p>\n<p>            glVertex3f(x[i+1, j+1], y[i+1, j+1], z[i+1, j+1])<\/p>\n<p>            glVertex3f(x[i, j+1], y[i, j+1], z[i, j+1])<\/p>\n<p>    glEnd()<\/p>\n<p>    glutSwapBuffers()<\/p>\n<p>glutInit()<\/p>\n<p>glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)<\/p>\n<p>glutInitWindowSize(800, 600)<\/p>\n<p>glutCreateWindow(b&#39;3D Surface Plot&#39;)<\/p>\n<p>glutDisplayFunc(draw_surface)<\/p>\n<p>glutMainLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528PyOpenGL\u521b\u5efa\u4e00\u4e2a3D\u66f2\u9762\u56fe\uff0c\u5e76\u8bbe\u7f6eOpenGL\u7a97\u53e3\u7684\u57fa\u672c\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h3>5. \u7f8e\u53163D\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>PyOpenGL\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u81ea\u5b9a\u4e49\u9009\u9879\uff0c\u53ef\u4ee5\u7528\u6765\u7f8e\u53163D\u56fe\u5f62\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8c03\u6574\u989c\u8272\u6620\u5c04\u3001\u6dfb\u52a0\u989c\u8272\u6761\u3001\u8bbe\u7f6e\u900f\u660e\u5ea6\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def draw_surface():<\/p>\n<p>    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)<\/p>\n<p>    glLoadIdentity()<\/p>\n<p>    gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0)<\/p>\n<p>    x = np.linspace(-5, 5, 100)<\/p>\n<p>    y = np.linspace(-5, 5, 100)<\/p>\n<p>    x, y = np.meshgrid(x, y)<\/p>\n<p>    z = np.sin(np.sqrt(x&lt;strong&gt;2 + y&lt;\/strong&gt;2))<\/p>\n<p>    glBegin(GL_QUADS)<\/p>\n<p>    for i in range(len(x)-1):<\/p>\n<p>        for j in range(len(y)-1):<\/p>\n<p>            glColor3f(abs(z[i, j]), abs(z[i+1, j]), abs(z[i+1, j+1]))<\/p>\n<p>            glVertex3f(x[i, j], y[i, j], z[i, j])<\/p>\n<p>            glVertex3f(x[i+1, j], y[i+1, j], z[i+1, j])<\/p>\n<p>            glVertex3f(x[i+1, j+1], y[i+1, j+1], z[i+1, j+1])<\/p>\n<p>            glVertex3f(x[i, j+1], y[i, j+1], z[i, j+1])<\/p>\n<p>    glEnd()<\/p>\n<p>    glutSwapBuffers()<\/p>\n<p>glutInit()<\/p>\n<p>glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)<\/p>\n<p>glutInitWindowSize(800, 600)<\/p>\n<p>glutCreateWindow(b&#39;Beautified 3D Surface Plot&#39;)<\/p>\n<p>glutDisplayFunc(draw_surface)<\/p>\n<p>glutMainLoop()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u8c03\u6574\uff0c\u53ef\u4ee5\u8ba93D\u56fe\u5f62\u53d8\u5f97\u66f4\u52a0\u7f8e\u89c2\u548c\u4fe1\u606f\u4e30\u5bcc\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<p>\u901a\u8fc7\u4f7f\u7528Matplotlib\u3001Plotly\u3001Mayavi\u548cPyOpenGL\u7b49\u5de5\u5177\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u7ed8\u5236\u51fa\u6f02\u4eae\u76843D\u56fe\u5f62\u3002\u6bcf\u4e2a\u5de5\u5177\u90fd\u6709\u5176\u72ec\u7279\u7684\u529f\u80fd\u548c\u4f18\u52bf\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u5c55\u793a\u6570\u636e\u548c\u7ed3\u679c\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u521b\u5efa\u7f8e\u89c2\u76843D\u56fe\u5f62\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u7ed8\u52363D\u56fe\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u4e8e\u7ed8\u52363D\u56fe\u5f62\u3002\u5176\u4e2d\uff0cMatplotlib\u662f\u6700\u5e38\u7528\u7684\u5e93\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u63a5\u53e3\u6765\u521b\u5efa3D\u56fe\u3002\u9664\u4e86Matplotlib\uff0cPlotly\u548cMayavi\u4e5f\u662f\u975e\u5e38\u5f3a\u5927\u7684\u9009\u62e9\uff0c\u5c24\u5176\u9002\u5408\u9700\u8981\u4ea4\u4e92\u6027\u548c\u590d\u6742\u53ef\u89c6\u5316\u7684\u5e94\u7528\u3002\u6839\u636e\u4f60\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u5e93\u6765\u5b9e\u73b0\u4e0d\u540c\u7684\u6548\u679c\u3002<\/p>\n<p><strong>\u7ed8\u52363D\u56fe\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u53c2\u6570\u548c\u8bbe\u7f6e\uff1f<\/strong><br \/>\u5728\u7ed8\u52363D\u56fe\u65f6\uff0c\u91cd\u8981\u7684\u53c2\u6570\u5305\u62ec\u89c6\u89d2\u3001\u989c\u8272\u3001\u4ee5\u53ca\u6570\u636e\u7684\u8303\u56f4\u3002\u901a\u8fc7\u8c03\u6574\u89c6\u89d2\uff0c\u53ef\u4ee5\u83b7\u5f97\u4e0d\u540c\u7684\u89c2\u5bdf\u6548\u679c\u3002\u989c\u8272\u548c\u6837\u5f0f\u7684\u9009\u62e9\u80fd\u591f\u4f7f\u56fe\u5f62\u66f4\u52a0\u7f8e\u89c2\uff0c\u5e2e\u52a9\u6570\u636e\u66f4\u76f4\u89c2\u5730\u8868\u8fbe\u3002\u8fd8\u53ef\u4ee5\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6807\u7b7e\u548c\u6807\u9898\uff0c\u4ee5\u4fbf\u4e8e\u89c2\u4f17\u7406\u89e3\u56fe\u5f62\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f18\u53163D\u56fe\u7684\u6e32\u67d3\u901f\u5ea6\uff1f<\/strong><br \/>3D\u56fe\u7684\u6e32\u67d3\u901f\u5ea6\u53ef\u80fd\u4f1a\u53d7\u5230\u6570\u636e\u91cf\u548c\u56fe\u5f62\u590d\u6742\u5ea6\u7684\u5f71\u54cd\u3002\u4e3a\u4e86\u63d0\u9ad8\u6e32\u67d3\u901f\u5ea6\uff0c\u53ef\u4ee5\u8003\u8651\u51cf\u5c11\u7ed8\u5236\u7684\u70b9\u6570\u6216\u4f7f\u7528\u7b80\u5316\u7684\u51e0\u4f55\u56fe\u5f62\u3002\u6b64\u5916\uff0c\u9009\u62e9\u9002\u5408\u7684\u6e32\u67d3\u5f15\u64ce\uff0c\u6bd4\u5982\u4f7f\u7528Plotly\u7684WebGL\u652f\u6301\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u56fe\u5f62\u7684\u6e32\u67d3\u6027\u80fd\u3002\u5982\u679c\u4f7f\u7528Matplotlib\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u8c03\u6574\u56fe\u5f62\u7684\u5206\u8fa8\u7387\u548c\u4f7f\u7528\u201cblit\u201d\u6a21\u5f0f\u6765\u4f18\u5316\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d: Python\u753b\u51fa\u6f02\u4eae\u76843D\u56fe\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528Matplotlib\u3001Mayavi\u3001Plotl [&hellip;]","protected":false},"author":3,"featured_media":1108115,"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\/1108101"}],"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=1108101"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108101\/revisions"}],"predecessor-version":[{"id":1108122,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1108101\/revisions\/1108122"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1108115"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1108101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1108101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1108101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}