{"id":1068358,"date":"2024-12-31T16:42:48","date_gmt":"2024-12-31T08:42:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1068358.html"},"modified":"2024-12-31T16:42:51","modified_gmt":"2024-12-31T08:42:51","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%bb%e4%b8%89%e7%bb%b4%e5%87%bd%e6%95%b0%e5%9b%be%e5%83%8f","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1068358.html","title":{"rendered":"\u5982\u4f55\u7528python\u753b\u4e09\u7ef4\u51fd\u6570\u56fe\u50cf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/0d821fee-30eb-4f43-bc94-7e313e1bec56.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u753b\u4e09\u7ef4\u51fd\u6570\u56fe\u50cf\" \/><\/p>\n<p><p> <strong>\u7528Python\u7ed8\u5236\u4e09\u7ef4\u51fd\u6570\u56fe\u50cf<\/strong>\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2a\u6b65\u9aa4\u6765\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\u5b89\u88c5\u6240\u9700\u7684\u5e93\u3001\u8bbe\u7f6e\u6570\u636e\u3001\u521b\u5efa\u56fe\u50cf\u5bf9\u8c61\u3001\u7ed8\u5236\u56fe\u50cf\u5e76\u8fdb\u884c\u7f8e\u5316\u3002<strong>\u4f7f\u7528Matplotlib\u5e93\u3001\u5b9a\u4e49\u51fd\u6570\u3001\u8bbe\u7f6e\u6570\u636e\u8303\u56f4\u3001\u521b\u5efa\u4e09\u7ef4\u56fe\u50cf\u5bf9\u8c61\u3001\u7ed8\u5236\u5e76\u7f8e\u5316\u56fe\u50cf<\/strong>\u662f\u5b9e\u73b0\u6b64\u4efb\u52a1\u7684\u5173\u952e\u6b65\u9aa4\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u4ecb\u7ecd\u548c\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u5fc5\u8981\u7684Python\u5e93\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecMatplotlib\u548cNumpy\u3002Matplotlib\u7528\u4e8e\u7ed8\u5236\u56fe\u50cf\uff0c\u800cNumpy\u7528\u4e8e\u5904\u7406\u6570\u503c\u8ba1\u7b97\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\u8fd9\u4e9b\u5e93\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\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u8fd9\u4e9b\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>\u4e8c\u3001\u5b9a\u4e49\u51fd\u6570\u548c\u6570\u636e\u8303\u56f4<\/h3>\n<\/p>\n<p><p>\u5b9a\u4e49\u4f60\u8981\u7ed8\u5236\u7684\u4e09\u7ef4\u51fd\u6570\uff0c\u5e76\u8bbe\u7f6e\u6570\u636e\u8303\u56f4\u3002\u5e38\u89c1\u7684\u4e09\u7ef4\u51fd\u6570\u5305\u62ecz = f(x, y)\u7c7b\u578b\u7684\u51fd\u6570\uff0c\u6bd4\u5982z = sin(sqrt(x^2 + y^2))\u3002\u53ef\u4ee5\u4f7f\u7528Numpy\u6765\u751f\u6210x\u548cy\u7684\u503c\uff0c\u5e76\u8ba1\u7b97\u5bf9\u5e94\u7684z\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def f(x, y):<\/p>\n<p>    return np.sin(np.sqrt(x&lt;strong&gt;2 + y&lt;\/strong&gt;2))<\/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 = f(x, y)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u521b\u5efa\u4e09\u7ef4\u56fe\u50cf\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u7684<code>Axes3D<\/code>\u6a21\u5757\u521b\u5efa\u4e00\u4e2a\u4e09\u7ef4\u56fe\u50cf\u5bf9\u8c61\u3002\u53ef\u4ee5\u901a\u8fc7<code>plt.figure()<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u50cf\u7a97\u53e3\uff0c\u5e76\u901a\u8fc7<code>add_subplot<\/code>\u65b9\u6cd5\u6dfb\u52a0\u4e00\u4e2a\u4e09\u7ef4\u5b50\u56fe\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><h3>\u56db\u3001\u7ed8\u5236\u4e09\u7ef4\u51fd\u6570\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u4e09\u7ef4\u51fd\u6570\u56fe\u50cf\uff0c\u5305\u62ec\u7ebf\u6846\u56fe\uff08wireframe\uff09\u3001\u66f2\u9762\u56fe\uff08surface\uff09\u3001\u6563\u70b9\u56fe\uff08scatter\uff09\u7b49\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>plot_surface<\/code>\u65b9\u6cd5\u7ed8\u5236\u66f2\u9762\u56fe\u7684\u793a\u4f8b\uff1a<\/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><h3>\u4e94\u3001\u7f8e\u5316\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u56fe\u50cf\u66f4\u5177\u53ef\u8bfb\u6027\uff0c\u53ef\u4ee5\u6dfb\u52a0\u6807\u7b7e\u3001\u6807\u9898\u548c\u989c\u8272\u6761\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u7f8e\u5316\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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>ax.set_title(&#39;3D Surface Plot&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u989c\u8272\u6761<\/strong><\/h2>\n<p>fig.colorbar(ax.plot_surface(x, y, z, cmap=&#39;viridis&#39;), ax=ax, shrink=0.5, aspect=5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u663e\u793a\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4f7f\u7528<code>plt.show()<\/code>\u51fd\u6570\u663e\u793a\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u7528\u4e8e\u7ed8\u5236\u4e00\u4e2a\u4e09\u7ef4\u51fd\u6570\u56fe\u50cf\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>def f(x, y):<\/p>\n<p>    return np.sin(np.sqrt(x&lt;strong&gt;2 + y&lt;\/strong&gt;2))<\/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 = f(x, y)<\/p>\n<p>fig = plt.figure()<\/p>\n<p>ax = fig.add_subplot(111, projection=&#39;3d&#39;)<\/p>\n<p>surface = ax.plot_surface(x, y, z, cmap=&#39;viridis&#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>ax.set_title(&#39;3D Surface Plot&#39;)<\/p>\n<p>fig.colorbar(surface, ax=ax, shrink=0.5, aspect=5)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6df1\u5165\u7406\u89e3\u4e0e\u6269\u5c55<\/h3>\n<\/p>\n<p><h4>1\u3001\u7ed8\u5236\u4e0d\u540c\u7c7b\u578b\u7684\u4e09\u7ef4\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u66f2\u9762\u56fe\uff0c\u8fd8\u53ef\u4ee5\u7ed8\u5236\u5176\u4ed6\u7c7b\u578b\u7684\u4e09\u7ef4\u56fe\u50cf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u4e09\u7ef4\u56fe\u50cf\u7c7b\u578b\u53ca\u5176\u7ed8\u5236\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u7ebf\u6846\u56fe\uff08Wireframe\uff09<\/strong>\uff1a\u901a\u8fc7<code>plot_wireframe<\/code>\u65b9\u6cd5\u7ed8\u5236\u7ebf\u6846\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.plot_wireframe(x, y, z, color=&#39;blue&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6563\u70b9\u56fe\uff08Scatter\uff09<\/strong>\uff1a\u901a\u8fc7<code>scatter<\/code>\u65b9\u6cd5\u7ed8\u5236\u4e09\u7ef4\u6563\u70b9\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.scatter(x, y, z, color=&#39;red&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7b49\u9ad8\u7ebf\u56fe\uff08Contour\uff09<\/strong>\uff1a\u901a\u8fc7<code>contour3D<\/code>\u65b9\u6cd5\u7ed8\u5236\u4e09\u7ef4\u7b49\u9ad8\u7ebf\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.contour3D(x, y, z, 50, cmap=&#39;binary&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>2\u3001\u4ea4\u4e92\u5f0f\u4e09\u7ef4\u56fe\u50cf<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u63a2\u7d22\u4e09\u7ef4\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684\u4ea4\u4e92\u529f\u80fd\u3002\u901a\u8fc7\u542f\u7528\u4ea4\u4e92\u6a21\u5f0f\uff0c\u53ef\u4ee5\u65cb\u8f6c\u3001\u7f29\u653e\u548c\u79fb\u52a8\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mpl_toolkits.mplot3d import Axes3D<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<p>plt.ion()  # \u542f\u7528\u4ea4\u4e92\u6a21\u5f0f<\/p>\n<p>def f(x, y):<\/p>\n<p>    return np.sin(np.sqrt(x&lt;strong&gt;2 + y&lt;\/strong&gt;2))<\/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 = f(x, y)<\/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>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u548cMatplotlib\u5e93\u7ed8\u5236\u5404\u79cd\u7c7b\u578b\u7684\u4e09\u7ef4\u51fd\u6570\u56fe\u50cf\u3002<strong>\u638c\u63e1\u8fd9\u4e9b\u6280\u80fd\u4e0d\u4ec5\u80fd\u591f\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u4e09\u7ef4\u6570\u636e\uff0c\u8fd8\u80fd\u5728\u79d1\u7814\u3001\u5de5\u7a0b\u548c\u6570\u636e\u53ef\u89c6\u5316\u7b49\u9886\u57df\u4e2d\u63d0\u4f9b\u6709\u529b\u7684\u652f\u6301\u3002<\/strong><\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u7ed8\u5236\u4e09\u7ef4\u51fd\u6570\u56fe\u50cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u4e09\u7ef4\u51fd\u6570\u56fe\u50cf\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fMatplotlib\u548cMayavi\u3002Matplotlib\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u5408\u7b80\u5355\u7684\u4e09\u7ef4\u56fe\u5f62\u7ed8\u5236\uff0c\u800cMayavi\u5219\u66f4\u9002\u5408\u590d\u6742\u7684\u4e09\u7ef4\u53ef\u89c6\u5316\u3002\u5982\u679c\u4f60\u9700\u8981\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\uff0cNumPy\u548cSciPy\u4e5f\u53ef\u4ee5\u4e0e\u8fd9\u4e9b\u5e93\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u5904\u7406\u6570\u636e\u3002<\/p>\n<p><strong>\u7ed8\u5236\u4e09\u7ef4\u51fd\u6570\u56fe\u50cf\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u7ed8\u5236\u4e09\u7ef4\u51fd\u6570\u56fe\u50cf\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff08\u5982Matplotlib\uff09\u3001\u5b9a\u4e49\u4f60\u8981\u7ed8\u5236\u7684\u4e09\u7ef4\u51fd\u6570\u3001\u8bbe\u7f6e\u5750\u6807\u7f51\u683c\u3001\u8ba1\u7b97\u51fd\u6570\u503c\uff0c\u5e76\u4f7f\u7528\u7ed8\u56fe\u5e93\u7684\u7ed8\u56fe\u51fd\u6570\u8fdb\u884c\u53ef\u89c6\u5316\u3002\u5bf9\u4e8eMatplotlib\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>Axes3D<\/code>\u7c7b\u6765\u521b\u5efa\u4e09\u7ef4\u5750\u6807\u7cfb\uff0c\u5e76\u5229\u7528<code>plot_surface()<\/code>\u3001<code>scatter()<\/code>\u7b49\u65b9\u6cd5\u8fdb\u884c\u7ed8\u56fe\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u5347\u4e09\u7ef4\u56fe\u50cf\u7684\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u6027\uff1f<\/strong><br \/>\u4e3a\u4e86\u63d0\u5347\u4e09\u7ef4\u56fe\u50cf\u7684\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u6027\uff0c\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u63aa\u65bd\uff1a\u8c03\u6574\u89c6\u89d2\u4ee5\u5c55\u793a\u6700\u4f73\u6548\u679c\u3001\u9009\u62e9\u9002\u5f53\u7684\u989c\u8272\u6620\u5c04\u3001\u6dfb\u52a0\u7f51\u683c\u7ebf\u548c\u5750\u6807\u8f74\u6807\u7b7e\u3001\u4ee5\u53ca\u4f7f\u7528\u900f\u660e\u5ea6\u6765\u7a81\u51fa\u663e\u793a\u7279\u5b9a\u533a\u57df\u3002\u6b64\u5916\uff0c\u6ce8\u91ca\u548c\u56fe\u4f8b\u4e5f\u80fd\u5e2e\u52a9\u89c2\u4f17\u66f4\u597d\u5730\u7406\u89e3\u56fe\u50cf\u6240\u4f20\u8fbe\u7684\u4fe1\u606f\u3002\u4f7f\u7528\u8fd9\u4e9b\u6280\u5de7\uff0c\u53ef\u4ee5\u4f7f\u56fe\u50cf\u4e0d\u4ec5\u7f8e\u89c2\uff0c\u8fd8\u80fd\u6709\u6548\u4f20\u8fbe\u6570\u636e\u7684\u542b\u4e49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u7ed8\u5236\u4e09\u7ef4\u51fd\u6570\u56fe\u50cf\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2a\u6b65\u9aa4\u6765\u5b9e\u73b0\uff0c\u4e3b\u8981\u5305\u62ec\u5b89\u88c5\u6240\u9700\u7684\u5e93\u3001\u8bbe\u7f6e\u6570\u636e\u3001\u521b\u5efa\u56fe\u50cf\u5bf9\u8c61\u3001\u7ed8\u5236\u56fe\u50cf\u5e76 [&hellip;]","protected":false},"author":3,"featured_media":1068371,"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\/1068358"}],"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=1068358"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068358\/revisions"}],"predecessor-version":[{"id":1068374,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1068358\/revisions\/1068374"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1068371"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1068358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1068358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1068358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}