{"id":1061143,"date":"2024-12-31T15:40:17","date_gmt":"2024-12-31T07:40:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1061143.html"},"modified":"2024-12-31T15:40:19","modified_gmt":"2024-12-31T07:40:19","slug":"python%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e4%b8%89%e7%bb%b4%e7%81%b0%e5%ba%a6%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1061143.html","title":{"rendered":"python\u5982\u4f55\u663e\u793a\u4e09\u7ef4\u7070\u5ea6\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/31c7ed92-98cf-4c92-bd52-db521f416b21.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u663e\u793a\u4e09\u7ef4\u7070\u5ea6\u56fe\" \/><\/p>\n<p><p> <strong>Python\u663e\u793a\u4e09\u7ef4\u7070\u5ea6\u56fe\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528matplotlib\u3001scipy\u548cmayavi\u5de5\u5177\u5e93\u8fdb\u884c\u7ed8\u56fe<\/strong>\uff0c\u5176\u4e2d\uff0cmatplotlib\u662f\u6700\u5e38\u7528\u7684\u5de5\u5177\u4e4b\u4e00\u3002\u901a\u8fc7\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u4e2a\u5de5\u5177\u7684\u4f7f\u7528\u65b9\u6cd5\u6765\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Matplotlib\u663e\u793a\u4e09\u7ef4\u7070\u5ea6\u56fe<\/h3>\n<\/p>\n<p><p><strong>Matplotlib<\/strong>\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u7528\u6765\u7ed8\u5236\u5404\u79cd\u56fe\u8868\uff0c\u5305\u62ec\u4e09\u7ef4\u7070\u5ea6\u56fe\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff0c\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528Matplotlib\u6765\u663e\u793a\u4e09\u7ef4\u7070\u5ea6\u56fe\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86matplotlib\u548cnumpy\u5e93\u3002\u5982\u679c\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\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><h4>2\u3001\u5bfc\u5165\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u5fc5\u8981\u7684\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><h4>3\u3001\u521b\u5efa\u4e09\u7ef4\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u6570\u636e\u96c6\uff0c\u4f8b\u5982\u4f7f\u7528numpy\u751f\u6210\u968f\u673a\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u7684\u7070\u5ea6\u56fe\u6570\u636e<\/p>\n<p>data = np.random.rand(100, 100)<\/p>\n<h2><strong>\u521b\u5efa\u7f51\u683c<\/strong><\/h2>\n<p>x = np.linspace(0, 1, data.shape[0])<\/p>\n<p>y = np.linspace(0, 1, data.shape[1])<\/p>\n<p>X, Y = np.meshgrid(x, y)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u7ed8\u5236\u4e09\u7ef4\u7070\u5ea6\u56fe<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u7684<code>plot_surface<\/code>\u51fd\u6570\u7ed8\u5236\u4e09\u7ef4\u7070\u5ea6\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>\u7ed8\u5236\u4e09\u7ef4\u7070\u5ea6\u56fe<\/strong><\/h2>\n<p>ax.plot_surface(X, Y, data, cmap=&#39;gray&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>ax.set_title(&quot;3D Grayscale Image&quot;)<\/p>\n<p>ax.set_xlabel(&quot;X axis&quot;)<\/p>\n<p>ax.set_ylabel(&quot;Y axis&quot;)<\/p>\n<p>ax.set_zlabel(&quot;Intensity&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Scipy\u663e\u793a\u4e09\u7ef4\u7070\u5ea6\u56fe<\/h3>\n<\/p>\n<p><p><strong>Scipy<\/strong>\u5e93\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u53ef\u4ee5\u7ed3\u5408matplotlib\u6765\u663e\u793a\u4e09\u7ef4\u7070\u5ea6\u56fe\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86scipy\u5e93\uff0c\u5982\u679c\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scipy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5bfc\u5165\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u5fc5\u8981\u7684\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>from scipy import ndimage<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u521b\u5efa\u4e09\u7ef4\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u6570\u636e\u96c6\uff0c\u4f8b\u5982\u4f7f\u7528numpy\u751f\u6210\u968f\u673a\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u7684\u7070\u5ea6\u56fe\u6570\u636e<\/p>\n<p>data = np.random.rand(30, 30, 30)<\/p>\n<h2><strong>\u521b\u5efa\u7f51\u683c<\/strong><\/h2>\n<p>x = np.linspace(0, 1, data.shape[0])<\/p>\n<p>y = np.linspace(0, 1, data.shape[1])<\/p>\n<p>z = np.linspace(0, 1, data.shape[2])<\/p>\n<p>X, Y, Z = np.meshgrid(x, y, z)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u5904\u7406\u6570\u636e\u5e76\u7ed8\u5236\u4e09\u7ef4\u7070\u5ea6\u56fe<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Scipy\u7684ndimage\u6a21\u5757\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\uff0c\u5e76\u4f7f\u7528Matplotlib\u7ed8\u5236\u4e09\u7ef4\u7070\u5ea6\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>\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406<\/strong><\/h2>\n<p>data_smoothed = ndimage.gaussian_filter(data, sigma=1)<\/p>\n<h2><strong>\u7ed8\u5236\u4e09\u7ef4\u7070\u5ea6\u56fe<\/strong><\/h2>\n<p>ax.plot_surface(X[:, :, 15], Y[:, :, 15], data_smoothed[:, :, 15], cmap=&#39;gray&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>ax.set_title(&quot;3D Grayscale Image (Scipy)&quot;)<\/p>\n<p>ax.set_xlabel(&quot;X axis&quot;)<\/p>\n<p>ax.set_ylabel(&quot;Y axis&quot;)<\/p>\n<p>ax.set_zlabel(&quot;Intensity&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Mayavi\u663e\u793a\u4e09\u7ef4\u7070\u5ea6\u56fe<\/h3>\n<\/p>\n<p><p><strong>Mayavi<\/strong>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u6570\u636e\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u663e\u793a\u590d\u6742\u7684\u4e09\u7ef4\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86mayavi\u5e93\uff0c\u5982\u679c\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\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<p><h4>2\u3001\u5bfc\u5165\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from mayavi import mlab<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u521b\u5efa\u4e09\u7ef4\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u6570\u636e\u96c6\uff0c\u4f8b\u5982\u4f7f\u7528numpy\u751f\u6210\u968f\u673a\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u7684\u7070\u5ea6\u56fe\u6570\u636e<\/p>\n<p>data = np.random.rand(30, 30, 30)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u7ed8\u5236\u4e09\u7ef4\u7070\u5ea6\u56fe<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Mayavi\u7684<code>contour3d<\/code>\u51fd\u6570\u7ed8\u5236\u4e09\u7ef4\u7070\u5ea6\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ed8\u5236\u4e09\u7ef4\u7070\u5ea6\u56fe<\/p>\n<p>mlab.contour3d(data, contours=10, opacity=0.5)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898<\/strong><\/h2>\n<p>mlab.title(&quot;3D Grayscale Image (Mayavi)&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>mlab.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ecb\u7ecd\uff0c\u53ef\u4ee5\u770b\u5230\u5728Python\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684\u5de5\u5177\u5e93\u663e\u793a\u4e09\u7ef4\u7070\u5ea6\u56fe\u7684\u65b9\u6cd5\u3002\u5176\u4e2d\uff0c<strong>Matplotlib<\/strong>\u662f\u6700\u5e38\u7528\u4e14\u7b80\u5355\u6613\u4e0a\u624b\u7684\u5de5\u5177\uff0c<strong>Scipy<\/strong>\u7ed3\u5408Matplotlib\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u5904\u7406\u5e76\u663e\u793a\uff0c<strong>Mayavi<\/strong>\u5219\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u4e09\u7ef4\u53ef\u89c6\u5316\u529f\u80fd\u3002\u8fd9\u4e09\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u5177\u4f53\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u548c\u719f\u6089\u7a0b\u5ea6\u6765\u51b3\u5b9a\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u4e4b\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u6765\u663e\u793a\u4e09\u7ef4\u7070\u5ea6\u56fe\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u66f4\u597d\u5730\u7406\u89e3\u548c\u5c55\u793a\u6570\u636e\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u52a0\u8f7d\u548c\u663e\u793a\u4e09\u7ef4\u7070\u5ea6\u56fe\u50cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\u6765\u52a0\u8f7d\u4e09\u7ef4\u7070\u5ea6\u56fe\u50cf\u6570\u636e\uff0c\u7ed3\u5408<code>matplotlib<\/code>\u5e93\u6765\u8fdb\u884c\u663e\u793a\u3002\u9996\u5148\uff0c\u901a\u8fc7<code>numpy<\/code>\u8bfb\u53d6\u56fe\u50cf\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528<code>plt.imshow()<\/code>\u65b9\u6cd5\u5728\u4e0d\u540c\u7684\u5207\u7247\u4e2d\u5c55\u793a\u56fe\u50cf\uff0c\u6216\u4f7f\u7528<code>mpl_toolkits.mplot3d<\/code>\u8fdb\u884c\u4e09\u7ef4\u53ef\u89c6\u5316\u3002<\/p>\n<p><strong>\u4f7f\u7528\u54ea\u4e9b\u5e93\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u4e09\u7ef4\u7070\u5ea6\u56fe\uff1f<\/strong><br \/>\u5904\u7406\u4e09\u7ef4\u7070\u5ea6\u56fe\u50cf\u65f6\uff0c<code>numpy<\/code>\u3001<code>scikit-image<\/code>\u548c<code>matplotlib<\/code>\u662f\u5e38\u7528\u7684\u5e93\u3002<code>numpy<\/code>\u7528\u4e8e\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\uff0c<code>scikit-image<\/code>\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u56fe\u50cf\u5904\u7406\u529f\u80fd\uff0c\u800c<code>matplotlib<\/code>\u5219\u7528\u4e8e\u53ef\u89c6\u5316\u56fe\u50cf\u3002\u7ed3\u5408\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u5b9e\u73b0\u52a0\u8f7d\u3001\u5904\u7406\u548c\u5c55\u793a\u4e09\u7ef4\u7070\u5ea6\u56fe\u7684\u5b8c\u6574\u6d41\u7a0b\u3002<\/p>\n<p><strong>\u80fd\u5426\u5728Python\u4e2d\u5bf9\u4e09\u7ef4\u7070\u5ea6\u56fe\u8fdb\u884c\u5904\u7406\u548c\u5206\u6790\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\u3002\u4f7f\u7528<code>scikit-image<\/code>\u5e93\uff0c\u60a8\u53ef\u4ee5\u5bf9\u4e09\u7ef4\u7070\u5ea6\u56fe\u8fdb\u884c\u591a\u79cd\u5904\u7406\uff0c\u6bd4\u5982\u6ee4\u6ce2\u3001\u8fb9\u7f18\u68c0\u6d4b\u3001\u9608\u503c\u5206\u5272\u7b49\u3002\u6b64\u5916\uff0c<code>numpy<\/code>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u5bf9\u56fe\u50cf\u6570\u636e\u8fdb\u884c\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\u548c\u5206\u6790\uff0c\u5e2e\u52a9\u63d0\u53d6\u6709\u7528\u7684\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u663e\u793a\u4e09\u7ef4\u7070\u5ea6\u56fe\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528matplotlib\u3001scipy\u548cmayavi\u5de5\u5177\u5e93\u8fdb\u884c\u7ed8\u56fe\uff0c\u5176\u4e2d\uff0cm [&hellip;]","protected":false},"author":3,"featured_media":1061154,"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\/1061143"}],"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=1061143"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061143\/revisions"}],"predecessor-version":[{"id":1061156,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1061143\/revisions\/1061156"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1061154"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1061143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1061143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1061143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}