{"id":923271,"date":"2024-12-26T14:46:40","date_gmt":"2024-12-26T06:46:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/923271.html"},"modified":"2024-12-26T14:46:43","modified_gmt":"2024-12-26T06:46:43","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e5%9c%86%e9%94%a5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/923271.html","title":{"rendered":"Python\u5982\u4f55\u753b\u5706\u9525"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211416\/9b169d99-4c1e-48e6-916d-54ea642135b2.webp\" alt=\"Python\u5982\u4f55\u753b\u5706\u9525\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u753b\u5706\u9525\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u548cNumPy\u5e93\u3001\u5229\u7528\u4e09\u7ef4\u7ed8\u56fe\u529f\u80fd\u3001\u4e3a\u5706\u9525\u7684\u5e95\u9762\u548c\u4fa7\u9762\u751f\u6210\u76f8\u5e94\u7684\u5750\u6807\u6570\u636e\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u7684<code>mpl_toolkits.mplot3d<\/code>\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ed8\u5236\u4e09\u7ef4\u56fe\u5f62\u3002\u9996\u5148\uff0c\u751f\u6210\u5706\u9525\u7684\u5e95\u9762\u548c\u4fa7\u9762\u6570\u636e\uff0c\u7136\u540e\u901a\u8fc7<code>plot_surface<\/code>\u6216<code>plot_trisurf<\/code>\u7b49\u51fd\u6570\u5c06\u5176\u7ed8\u5236\u51fa\u6765\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Matplotlib\u548cNumPy\u5e93\u3002\u5982\u679c\u6ca1\u6709\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><p>\u7136\u540e\uff0c\u5728Python\u811a\u672c\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><p>\u4e8c\u3001\u751f\u6210\u5706\u9525\u7684\u5e95\u9762\u548c\u4fa7\u9762\u5750\u6807<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u751f\u6210\u5e95\u9762\u5750\u6807<\/strong><\/p>\n<\/p>\n<p><p>\u5706\u9525\u7684\u5e95\u9762\u662f\u4e00\u4e2a\u5706\uff0c\u56e0\u6b64\u53ef\u4ee5\u901a\u8fc7\u89d2\u5ea6\u53c2\u6570\u5316\u6765\u751f\u6210\u5176\u5750\u6807\u3002\u4f7f\u7528NumPy\u4e2d\u7684<code>linspace<\/code>\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u4ece0\u52302\u03c0\u7684\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528<code>cos<\/code>\u548c<code>sin<\/code>\u51fd\u6570\u8ba1\u7b97\u51fa\u5706\u7684x\u548cy\u5750\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u53c2\u6570\u8bbe\u7f6e<\/p>\n<p>radius = 1.0<\/p>\n<p>height = 2.0<\/p>\n<p>num_points = 100<\/p>\n<h2><strong>\u751f\u6210\u5e95\u9762\u5750\u6807<\/strong><\/h2>\n<p>theta = np.linspace(0, 2 * np.pi, num_points)<\/p>\n<p>x_base = radius * np.cos(theta)<\/p>\n<p>y_base = radius * np.sin(theta)<\/p>\n<p>z_base = np.zeros_like(x_base)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u751f\u6210\u4fa7\u9762\u5750\u6807<\/strong><\/p>\n<\/p>\n<p><p>\u5706\u9525\u7684\u4fa7\u9762\u53ef\u4ee5\u901a\u8fc7\u751f\u6210\u5706\u9525\u4f53\u7684\u9876\u70b9\u548c\u5e95\u9762\u4e4b\u95f4\u7684\u8fde\u63a5\u7ebf\u6765\u5b9e\u73b0\u3002\u9700\u8981\u4e3a\u6bcf\u4e2a\u5e95\u9762\u70b9\u751f\u6210\u4e00\u4e2a\u5bf9\u5e94\u7684\u9876\u70b9\u5750\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u9876\u70b9\u5750\u6807<\/p>\n<p>x_vertex = np.array([0])<\/p>\n<p>y_vertex = np.array([0])<\/p>\n<p>z_vertex = np.array([height])<\/p>\n<h2><strong>\u7ec4\u5408\u5750\u6807<\/strong><\/h2>\n<p>x_side = np.vstack((x_base, x_vertex))<\/p>\n<p>y_side = np.vstack((y_base, y_vertex))<\/p>\n<p>z_side = np.vstack((z_base, z_vertex))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u7ed8\u5236\u5706\u9525<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa3D\u7ed8\u56fe\u73af\u5883<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u7684<code>figure<\/code>\u548c<code>add_subplot<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u7ed8\u56fe\u73af\u5883\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<\/li>\n<li>\n<p><strong>\u7ed8\u5236\u5e95\u9762\u548c\u4fa7\u9762<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>plot_surface<\/code>\u6216<code>plot_trisurf<\/code>\u51fd\u6570\u5206\u522b\u7ed8\u5236\u5706\u9525\u7684\u5e95\u9762\u548c\u4fa7\u9762\u3002\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528<code>plot_trisurf<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5904\u7406\u4fa7\u9762\u5750\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ed8\u5236\u5e95\u9762<\/p>\n<p>ax.plot_trisurf(x_base, y_base, z_base, color=&#39;b&#39;, alpha=0.5, linewidth=0)<\/p>\n<h2><strong>\u7ed8\u5236\u4fa7\u9762<\/strong><\/h2>\n<p>for i in range(num_points):<\/p>\n<p>    ax.plot_trisurf(x_side[:, i], y_side[:, i], z_side[:, i], color=&#39;r&#39;, alpha=0.5, linewidth=0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>show<\/code>\u51fd\u6570\u663e\u793a\u7ed8\u5236\u7684\u5706\u9525\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f18\u5316\u548c\u8c03\u6574\u7ed8\u56fe<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8c03\u6574\u89c6\u89d2\u548c\u6bd4\u4f8b<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>view_init<\/code>\u51fd\u6570\u8c03\u6574\u89c6\u89d2\uff0c\u4ee5\u83b7\u5f97\u6700\u4f73\u7684\u89c2\u5bdf\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.view_init(elev=30, azim=30)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u6bd4\u4f8b<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>set_box_aspect<\/code>\u51fd\u6570\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6bd4\u4f8b\uff0c\u4ee5\u786e\u4fdd\u56fe\u5f62\u7684\u5404\u4e2a\u7ef4\u5ea6\u6bd4\u4f8b\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.set_box_aspect([1, 1, 1])  # \u4f7f x, y, z \u8f74\u6bd4\u4f8b\u4e00\u81f4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u603b\u7ed3\u4e0e\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684Matplotlib\u5e93\u751f\u6210\u4e00\u4e2a\u7b80\u5355\u7684\u4e09\u7ef4\u5706\u9525\u56fe\u5f62\u3002\u8fd9\u4e00\u8fc7\u7a0b\u6d89\u53ca\u5230\u7684\u6280\u672f\u5305\u62ec\u4e09\u7ef4\u5750\u6807\u751f\u6210\u3001\u53c2\u6570\u5316\u7ed8\u56fe\u548c\u4e09\u7ef4\u6e32\u67d3\u7b49\u3002\u7ed8\u5236\u5706\u9525\u7684\u6280\u80fd\u5728\u79d1\u5b66\u8ba1\u7b97\u3001\u6570\u636e\u53ef\u89c6\u5316\u548c\u8ba1\u7b97\u673a\u56fe\u5f62\u5b66\u4e2d\u5177\u6709\u91cd\u8981\u5e94\u7528\u3002\u901a\u8fc7\u8c03\u6574\u53c2\u6570\u548c\u6539\u8fdb\u7ed8\u56fe\u4ee3\u7801\uff0c\u53ef\u4ee5\u751f\u6210\u66f4\u52a0\u590d\u6742\u548c\u7cbe\u7f8e\u7684\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u4e00\u6b65\u6269\u5c55\uff0c\u6bd4\u5982\u4e3a\u5706\u9525\u6dfb\u52a0\u7eb9\u7406\u3001\u5149\u7167\u6548\u679c\uff0c\u6216\u8005\u7ed3\u5408\u5176\u4ed6\u51e0\u4f55\u4f53\u751f\u6210\u590d\u6742\u7684\u4e09\u7ef4\u6a21\u578b\u3002\u8fd9\u4e9b\u6280\u672f\u5728\u8ba1\u7b97\u673a\u56fe\u5f62\u5b66\u3001\u5de5\u7a0b\u4eff\u771f\u548c\u52a8\u753b\u5236\u4f5c\u4e2d\u5177\u6709\u5e7f\u6cdb\u5e94\u7528\u524d\u666f\u3002\u901a\u8fc7\u4e0d\u65ad\u5b9e\u8df5\u548c\u5b66\u4e60\uff0c\u53ef\u4ee5\u63d0\u5347\u5728Python\u73af\u5883\u4e0b\u8fdb\u884c\u4e09\u7ef4\u7ed8\u56fe\u7684\u80fd\u529b\uff0c\u4e3a\u5404\u79cd\u9879\u76ee\u63d0\u4f9b\u89c6\u89c9\u5316\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u52363D\u5706\u9525\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u7ed8\u52363D\u5706\u9525\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684mplot3d\u6a21\u5757\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5Matplotlib\uff0c\u5e76\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u5706\u9525\u7684\u5e95\u9762\u534a\u5f84\u548c\u9ad8\u5ea6\uff0c\u5229\u7528\u53c2\u6570\u65b9\u7a0b\u751f\u6210\u5706\u9525\u7684\u9876\u70b9\u548c\u5e95\u9762\u5750\u6807\uff0c\u6700\u540e\u4f7f\u7528plot_surface()\u51fd\u6570\u5b9e\u73b03D\u7ed8\u5236\u3002<\/p>\n<p><strong>\u7ed8\u5236\u5706\u9525\u9700\u8981\u54ea\u4e9bPython\u5e93\uff1f<\/strong><br \/>\u7ed8\u5236\u5706\u9525\u901a\u5e38\u9700\u8981Matplotlib\u5e93\uff0c\u7279\u522b\u662f\u5b83\u7684mplot3d\u5de5\u5177\u3002\u5982\u679c\u9700\u8981\u66f4\u590d\u6742\u7684\u56fe\u5f62\uff0c\u8fd8\u53ef\u4ee5\u8003\u8651\u4f7f\u7528NumPy\u6765\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\uff0c\u6216\u4f7f\u7528\u5176\u4ed63D\u7ed8\u56fe\u5e93\u5982Mayavi\u6216Plotly\uff0c\u4ee5\u83b7\u5f97\u66f4\u4e30\u5bcc\u7684\u89c6\u89c9\u6548\u679c\u548c\u4ea4\u4e92\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u81ea\u5b9a\u4e49\u5706\u9525\u7684\u989c\u8272\u548c\u6837\u5f0f\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Matplotlib\u7ed8\u5236\u5706\u9525\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eplot_surface()\u51fd\u6570\u4e2d\u7684\u53c2\u6570\u6765\u5b9a\u5236\u989c\u8272\u548c\u6837\u5f0f\u3002\u53ef\u4ee5\u6307\u5b9a\u8272\u5f69\u6620\u5c04\u3001\u900f\u660e\u5ea6\u3001\u5149\u7167\u6548\u679c\u7b49\uff0c\u751a\u81f3\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u989c\u8272\u6570\u7ec4\u6765\u4f7f\u5706\u9525\u7684\u4e0d\u540c\u90e8\u5206\u5448\u73b0\u4e0d\u540c\u7684\u989c\u8272\uff0c\u4ece\u800c\u589e\u5f3a\u89c6\u89c9\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u753b\u5706\u9525\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u548cNumPy\u5e93\u3001\u5229\u7528\u4e09\u7ef4\u7ed8\u56fe\u529f\u80fd\u3001\u4e3a\u5706\u9525\u7684\u5e95\u9762\u548c\u4fa7\u9762\u751f\u6210 [&hellip;]","protected":false},"author":3,"featured_media":923276,"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\/923271"}],"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=923271"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/923271\/revisions"}],"predecessor-version":[{"id":923280,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/923271\/revisions\/923280"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/923276"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=923271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=923271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=923271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}