{"id":1078200,"date":"2025-01-08T12:09:21","date_gmt":"2025-01-08T04:09:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1078200.html"},"modified":"2025-01-08T12:09:24","modified_gmt":"2025-01-08T04:09:24","slug":"python%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e4%b8%89%e7%bb%b4%e5%9b%be%e7%89%87-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1078200.html","title":{"rendered":"Python\u5982\u4f55\u663e\u793a\u4e09\u7ef4\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181858\/1ec14849-c0cb-4e30-8a10-bd2c9bb473c9.webp\" alt=\"Python\u5982\u4f55\u663e\u793a\u4e09\u7ef4\u56fe\u7247\" \/><\/p>\n<p><p> <strong>Python\u663e\u793a\u4e09\u7ef4\u56fe\u7247\u7684\u65b9\u5f0f\u6709\uff1a\u4f7f\u7528Matplotlib\u5e93\u3001\u4f7f\u7528Mayavi\u5e93\u3001\u4f7f\u7528Plotly\u5e93\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528Matplotlib\u5e93<\/strong>\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002Matplotlib\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u76842D\u548c3D\u7ed8\u56fe\u5e93\uff0c\u80fd\u591f\u521b\u5efa\u9759\u6001\u3001\u52a8\u753b\u548c\u4ea4\u4e92\u5f0f\u7684\u56fe\u8868\u3002\u5b83\u76843D\u7ed8\u56fe\u529f\u80fd\u7531mpl_toolkits.mplot3d\u6a21\u5757\u63d0\u4f9b\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u7ed8\u5236\u5404\u79cd\u4e09\u7ef4\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Matplotlib\u5e93\u6765\u663e\u793a\u4e09\u7ef4\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u6240\u9700\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Matplotlib\u5e93\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u76f8\u5173\u7684Python\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 numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u4e09\u7ef4\u56fe\u5f62\u4e4b\u524d\uff0c\u6211\u4eec\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>\u4e09\u3001\u521b\u5efa\u4e09\u7ef4\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u4e09\u7ef4\u56fe\u5f62\u3002\u4f8b\u5982\uff0c\u7ed8\u5236\u4e00\u4e2a\u4e09\u7ef4\u6b63\u5f26\u66f2\u9762\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6570\u636e<\/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<h2><strong>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>fig = plt.figure()<\/p>\n<p>ax = fig.add_subplot(111, projection=&#39;3d&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u4e09\u7ef4\u66f2\u9762\u56fe<\/strong><\/h2>\n<p>ax.plot_surface(x, y, z, cmap=&#39;viridis&#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>\u56db\u3001\u8be6\u7ec6\u4ecb\u7ecdMatplotlib\u76843D\u7ed8\u56fe\u529f\u80fd<\/h3>\n<\/p>\n<p><h4>1\u3001\u521b\u5efa\u4e09\u7ef4\u5750\u6807\u7cfb<\/h4>\n<\/p>\n<p><p>\u5728Matplotlib\u4e2d\uff0c\u521b\u5efa\u4e09\u7ef4\u5750\u6807\u7cfb\u9700\u8981\u4f7f\u7528<code>Axes3D<\/code>\u7c7b\u3002\u53ef\u4ee5\u901a\u8fc7<code>fig.add_subplot<\/code>\u65b9\u6cd5\u6765\u6dfb\u52a0\u4e09\u7ef4\u5750\u6807\u7cfb\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><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7ed8\u5236\u4e09\u7ef4\u66f2\u9762\u56fe<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>ax.plot_surface<\/code>\u65b9\u6cd5\u53ef\u4ee5\u7ed8\u5236\u4e09\u7ef4\u66f2\u9762\u56fe\u3002\u8be5\u65b9\u6cd5\u7684\u53c2\u6570\u5305\u62ecx\u3001y\u3001z\u6570\u636e\u4ee5\u53ca\u989c\u8272\u6620\u5c04\u8868\uff08cmap\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.plot_surface(x, y, z, cmap=&#39;viridis&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7ed8\u5236\u4e09\u7ef4\u6563\u70b9\u56fe<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u4e09\u7ef4\u66f2\u9762\u56fe\uff0cMatplotlib\u8fd8\u53ef\u4ee5\u7ed8\u5236\u4e09\u7ef4\u6563\u70b9\u56fe\u3002\u53ef\u4ee5\u4f7f\u7528<code>ax.scatter<\/code>\u65b9\u6cd5\u3002<\/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<h2><strong>\u7ed8\u5236\u4e09\u7ef4\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>ax.scatter(x, y, z, c=&#39;r&#39;, marker=&#39;o&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u7ed8\u5236\u4e09\u7ef4\u7ebf\u56fe<\/h4>\n<\/p>\n<p><p>\u4e09\u7ef4\u7ebf\u56fe\u53ef\u4ee5\u4f7f\u7528<code>ax.plot<\/code>\u65b9\u6cd5\u6765\u7ed8\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6570\u636e<\/p>\n<p>z = np.linspace(0, 1, 100)<\/p>\n<p>x = z * np.sin(25 * z)<\/p>\n<p>y = z * np.cos(25 * z)<\/p>\n<h2><strong>\u7ed8\u5236\u4e09\u7ef4\u7ebf\u56fe<\/strong><\/h2>\n<p>ax.plot(x, y, z, label=&#39;parametric curve&#39;)<\/p>\n<p>ax.legend()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Mayavi\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Matplotlib\uff0cMayavi\u5e93\u4e5f\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u4e09\u7ef4\u6570\u636e\u53ef\u89c6\u5316\u5de5\u5177\u3002Mayavi\u662f\u57fa\u4e8eVTK\uff08Visualization Toolkit\uff09\u6784\u5efa\u7684\uff0c\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u4e09\u7ef4\u56fe\u5f62\u548c\u52a8\u753b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Mayavi<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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><h4>2\u3001\u5bfc\u5165Mayavi\u5e93<\/h4>\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><h4>3\u3001\u7ed8\u5236\u4e09\u7ef4\u56fe\u5f62<\/h4>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u7ed8\u5236\u4e00\u4e2a\u4e09\u7ef4\u6b63\u5f26\u66f2\u9762\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6570\u636e<\/p>\n<p>x, y = np.mgrid[-5:5:100j, -5:5:100j]<\/p>\n<p>z = np.sin(np.sqrt(x&lt;strong&gt;2 + y&lt;\/strong&gt;2))<\/p>\n<h2><strong>\u7ed8\u5236\u4e09\u7ef4\u66f2\u9762\u56fe<\/strong><\/h2>\n<p>mlab.surf(x, y, z, colormap=&#39;viridis&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>mlab.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Plotly\u5e93<\/h3>\n<\/p>\n<p><p>Plotly\u662f\u4e00\u4e2a\u57fa\u4e8eJavaScript\u7684\u7ed8\u56fe\u5e93\uff0c\u652f\u6301Python\u63a5\u53e3\uff0c\u80fd\u591f\u521b\u5efa\u4ea4\u4e92\u5f0f\u7684\u4e09\u7ef4\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Plotly<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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><h4>2\u3001\u5bfc\u5165Plotly\u5e93<\/h4>\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><h4>3\u3001\u7ed8\u5236\u4e09\u7ef4\u56fe\u5f62<\/h4>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u7ed8\u5236\u4e00\u4e2a\u4e09\u7ef4\u6b63\u5f26\u66f2\u9762\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u6570\u636e<\/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<h2><strong>\u521b\u5efa\u4e09\u7ef4\u66f2\u9762\u56fe<\/strong><\/h2>\n<p>fig = go.Figure(data=[go.Surface(z=z, x=x, y=y)])<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u663e\u793a\u4e09\u7ef4\u56fe\u7247\uff0c\u5305\u62ecMatplotlib\u3001Mayavi\u548cPlotly\u7b49\u3002<strong>Matplotlib<\/strong>\u9002\u7528\u4e8e\u7b80\u5355\u7684\u4e09\u7ef4\u56fe\u5f62\u7ed8\u5236\uff0c<strong>Mayavi<\/strong>\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u4e09\u7ef4\u6570\u636e\u53ef\u89c6\u5316\uff0c\u800c<strong>Plotly<\/strong>\u9002\u7528\u4e8e\u521b\u5efa\u4ea4\u4e92\u5f0f\u7684\u4e09\u7ef4\u56fe\u5f62\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5b8c\u6210\u4e09\u7ef4\u56fe\u5f62\u7684\u7ed8\u5236\u548c\u5c55\u793a\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u548c\u5904\u7406\u4e09\u7ef4\u56fe\u50cf\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u6765\u8bfb\u53d6\u548c\u5904\u7406\u4e09\u7ef4\u56fe\u50cf\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Numpy\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u52a0\u8f7d\u4e09\u7ef4\u6570\u7ec4\u6570\u636e\uff0cMatplotlib\u5e93\u4e2d\u7684<code>mplot3d<\/code>\u6a21\u5757\u80fd\u591f\u8fdb\u884c\u4e09\u7ef4\u53ef\u89c6\u5316\u3002\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u5982Pydicom\u6765\u5904\u7406\u533b\u5b66\u6210\u50cf\u6570\u636e\uff0c\u6216\u4f7f\u7528SimpleITK\u6765\u5904\u7406\u533b\u7597\u56fe\u50cf\u3002\u786e\u4fdd\u60a8\u638c\u63e1\u4e86\u76f8\u5e94\u5e93\u7684\u57fa\u672c\u7528\u6cd5\uff0c\u4ee5\u4fbf\u6709\u6548\u5730\u8fdb\u884c\u6570\u636e\u5904\u7406\u3002<\/p>\n<p><strong>Python\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u6765\u521b\u5efa\u4e09\u7ef4\u56fe\u5f62\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5e93\u6765\u521b\u5efa\u4e09\u7ef4\u56fe\u5f62\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fMatplotlib\u3001Mayavi\u548cPlotly\u3002Matplotlib\u9002\u5408\u4e8e\u57fa\u672c\u7684\u4e09\u7ef4\u7ed8\u56fe\uff0c\u800cMayavi\u5219\u9002\u5408\u4e8e\u66f4\u590d\u6742\u7684\u4e09\u7ef4\u53ef\u89c6\u5316\u3002Plotly\u5219\u63d0\u4f9b\u4e86\u4ea4\u4e92\u5f0f\u7684\u4e09\u7ef4\u56fe\u5f62\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u6ed1\u52a8\u548c\u7f29\u653e\u8fdb\u884c\u6df1\u5165\u5206\u6790\u3002\u6839\u636e\u9879\u76ee\u7684\u9700\u6c42\u548c\u590d\u6742\u7a0b\u5ea6\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u9ad8Python\u4e2d\u4e09\u7ef4\u56fe\u7247\u7684\u6e32\u67d3\u6548\u679c\uff1f<\/strong><br \/>\u63d0\u5347\u4e09\u7ef4\u56fe\u7247\u6e32\u67d3\u6548\u679c\u7684\u65b9\u5f0f\u6709\u5f88\u591a\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8c03\u6574\u5149\u7167\u548c\u9634\u5f71\u7684\u8bbe\u7f6e\uff0c\u4ee5\u589e\u52a0\u56fe\u50cf\u7684\u6df1\u5ea6\u611f\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u9ad8\u8d28\u91cf\u7684\u7eb9\u7406\u548c\u6750\u8d28\u53ef\u4ee5\u8ba9\u56fe\u50cf\u66f4\u52a0\u771f\u5b9e\u3002\u8c03\u6574\u89c6\u89d2\u548c\u76f8\u673a\u4f4d\u7f6e\u4e5f\u80fd\u663e\u8457\u5f71\u54cd\u89c6\u89c9\u6548\u679c\u3002\u5c1d\u8bd5\u4e0d\u540c\u7684\u6e32\u67d3\u53c2\u6570\uff0c\u5e76\u4f7f\u7528\u5408\u9002\u7684\u5e93\u529f\u80fd\u6765\u5b9e\u73b0\u66f4\u597d\u7684\u56fe\u50cf\u6548\u679c\uff0c\u80fd\u591f\u5e2e\u52a9\u60a8\u521b\u9020\u51fa\u66f4\u5177\u5438\u5f15\u529b\u7684\u4e09\u7ef4\u53ef\u89c6\u5316\u4f5c\u54c1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u663e\u793a\u4e09\u7ef4\u56fe\u7247\u7684\u65b9\u5f0f\u6709\uff1a\u4f7f\u7528Matplotlib\u5e93\u3001\u4f7f\u7528Mayavi\u5e93\u3001\u4f7f\u7528Plotly\u5e93\u3002 \u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1078214,"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\/1078200"}],"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=1078200"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078200\/revisions"}],"predecessor-version":[{"id":1078217,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1078200\/revisions\/1078217"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1078214"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1078200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1078200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1078200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}