{"id":956044,"date":"2024-12-27T02:27:41","date_gmt":"2024-12-26T18:27:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/956044.html"},"modified":"2024-12-27T02:27:44","modified_gmt":"2024-12-26T18:27:44","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e7%b9%81%e8%8a%b1%e6%9b%b2%e7%ba%bf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/956044.html","title":{"rendered":"python\u5982\u4f55\u753b\u7e41\u82b1\u66f2\u7ebf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095802\/3f652497-3f57-4075-8ef9-5e36c255cde4.webp\" alt=\"python\u5982\u4f55\u753b\u7e41\u82b1\u66f2\u7ebf\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u7ed8\u5236\u7e41\u82b1\u66f2\u7ebf\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528Matplotlib\u5e93\u3001\u53c2\u6570\u5316\u65b9\u7a0b\u4ee5\u53ca\u8ba1\u7b97\u673a\u7ed8\u56fe\u7b97\u6cd5\u7b49\u5de5\u5177\u4e0e\u65b9\u6cd5\u3002Matplotlib\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7ed8\u56fe\u529f\u80fd\u3001\u901a\u8fc7\u53c2\u6570\u5316\u65b9\u7a0b\u5b9a\u4e49\u66f2\u7ebf\u3001\u5229\u7528\u8ba1\u7b97\u673a\u7ed8\u56fe\u7b97\u6cd5\u4f18\u5316\u56fe\u5f62\u6e32\u67d3\u6548\u7387\u3002<\/strong> \u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u7e41\u82b1\u66f2\u7ebf\uff0c\u4ee5\u53ca\u4e0d\u540c\u65b9\u6cd5\u7684\u5b9e\u73b0\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001MATPLOTLIB\u5e93\u4e0e\u57fa\u7840\u6982\u5ff5<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u7ed8\u56fe\u5e93\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u5404\u79cd\u56fe\u8868\uff0c\u5305\u62ec\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u6563\u70b9\u56fe\u7b49\u3002\u7531\u4e8e\u5b83\u7684\u7075\u6d3b\u6027\u548c\u5f3a\u5927\u7684\u529f\u80fd\uff0cMatplotlib\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u590d\u6742\u7684\u6570\u5b66\u66f2\u7ebf\uff0c\u6bd4\u5982\u7e41\u82b1\u66f2\u7ebf\u3002<\/p>\n<\/p>\n<ol>\n<li>Matplotlib\u5e93\u7684\u5b89\u88c5\u4e0e\u57fa\u672c\u4f7f\u7528<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\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><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165Matplotlib\u5e93\u6765\u5f00\u59cb\u7ed8\u5236\u56fe\u5f62\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u7ed8\u5236\u4e00\u6761\u7b80\u5355\u7684\u66f2\u7ebf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u7e41\u82b1\u66f2\u7ebf\u7684\u57fa\u672c\u5b9a\u4e49\u4e0e\u53c2\u6570\u5316\u65b9\u7a0b<\/li>\n<\/ol>\n<p><p>\u7e41\u82b1\u66f2\u7ebf\u662f\u4e00\u79cd\u590d\u6742\u7684\u6570\u5b66\u66f2\u7ebf\uff0c\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u5316\u65b9\u7a0b\u6765\u5b9a\u4e49\u3002\u5e38\u89c1\u7684\u7e41\u82b1\u66f2\u7ebf\u65b9\u7a0b\u6d89\u53ca\u5230\u4e09\u89d2\u51fd\u6570\u548c\u591a\u9879\u5f0f\u51fd\u6570\u7684\u7ec4\u5408\u3002\u4e00\u4e2a\u7b80\u5355\u7684\u7e41\u82b1\u66f2\u7ebf\u65b9\u7a0b\u53ef\u4ee5\u8868\u793a\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-plaintext\">x = a * cos(n * t) * cos(t)<\/p>\n<p>y = a * cos(n * t) * sin(t)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>a<\/code>\u548c<code>n<\/code>\u662f\u5e38\u6570\uff0c<code>t<\/code>\u662f\u53c2\u6570\uff0c\u901a\u5e38\u57280\u52302\u03c0\u4e4b\u95f4\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528MATPLOTLIB\u7ed8\u5236\u7e41\u82b1\u66f2\u7ebf<\/p>\n<\/p>\n<ol>\n<li>\u5b9a\u4e49\u53c2\u6570\u5316\u65b9\u7a0b<\/li>\n<\/ol>\n<p><p>\u4e3a\u4e86\u7ed8\u5236\u7e41\u82b1\u66f2\u7ebf\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u5176\u53c2\u6570\u5316\u65b9\u7a0b\u3002\u53ef\u4ee5\u901a\u8fc7NumPy\u5e93\u751f\u6210\u53c2\u6570<code>t<\/code>\u7684\u503c\uff0c\u5e76\u8ba1\u7b97\u5bf9\u5e94\u7684<code>x<\/code>\u548c<code>y<\/code>\u5750\u6807\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\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<h2><strong>\u5b9a\u4e49\u5e38\u6570<\/strong><\/h2>\n<p>a = 1<\/p>\n<p>n = 5<\/p>\n<h2><strong>\u751f\u6210\u53c2\u6570t<\/strong><\/h2>\n<p>t = np.linspace(0, 2 * np.pi, 1000)<\/p>\n<h2><strong>\u8ba1\u7b97x\u548cy\u5750\u6807<\/strong><\/h2>\n<p>x = a * np.cos(n * t) * np.cos(t)<\/p>\n<p>y = a * np.cos(n * t) * np.sin(t)<\/p>\n<h2><strong>\u7ed8\u5236\u7e41\u82b1\u66f2\u7ebf<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<p>plt.title(&#39;\u7e41\u82b1\u66f2\u7ebf&#39;)<\/p>\n<p>plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86\u5e38\u6570<code>a<\/code>\u548c<code>n<\/code>\uff0c\u5e76\u751f\u6210\u4ece0\u52302\u03c0\u7684\u53c2\u6570<code>t<\/code>\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u53c2\u6570\u5316\u65b9\u7a0b\u8ba1\u7b97\u51fa<code>x<\/code>\u548c<code>y<\/code>\u7684\u5750\u6807\uff0c\u5e76\u4f7f\u7528<code>plt.plot()<\/code>\u51fd\u6570\u7ed8\u5236\u51fa\u7e41\u82b1\u66f2\u7ebf\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u8c03\u6574\u66f2\u7ebf\u7684\u5f62\u72b6\u4e0e\u6837\u5f0f<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8c03\u6574\u53c2\u6570<code>a<\/code>\u548c<code>n<\/code>\u7684\u503c\uff0c\u53ef\u4ee5\u6539\u53d8\u7e41\u82b1\u66f2\u7ebf\u7684\u5f62\u72b6\u3002\u4f8b\u5982\uff0c\u6539\u53d8<code>n<\/code>\u7684\u503c\u53ef\u4ee5\u8c03\u6574\u66f2\u7ebf\u7684\u82b1\u74e3\u6570\u91cf\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5c1d\u8bd5\u4e0d\u540c\u7684\u53c2\u6570\u7ec4\u5408\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4e0d\u540c\u7684n\u503c<\/p>\n<p>n_values = [3, 5, 7, 9]<\/p>\n<p>plt.figure(figsize=(10, 10))<\/p>\n<p>for i, n in enumerate(n_values):<\/p>\n<p>    x = a * np.cos(n * t) * np.cos(t)<\/p>\n<p>    y = a * np.cos(n * t) * np.sin(t)<\/p>\n<p>    plt.subplot(2, 2, i+1)<\/p>\n<p>    plt.plot(x, y)<\/p>\n<p>    plt.title(f&#39;n = {n}&#39;)<\/p>\n<p>    plt.axis(&#39;equal&#39;)<\/p>\n<p>plt.tight_layout()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>subplot<\/code>\u51fd\u6570\u5c06\u591a\u4e2a\u66f2\u7ebf\u7ed8\u5236\u5728\u540c\u4e00\u5f20\u56fe\u4e2d\uff0c\u4ee5\u4fbf\u89c2\u5bdf\u4e0d\u540c\u53c2\u6570\u7ec4\u5408\u5bf9\u66f2\u7ebf\u5f62\u72b6\u7684\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5229\u7528\u8ba1\u7b97\u673a\u7ed8\u56fe\u7b97\u6cd5\u4f18\u5316\u56fe\u5f62\u6e32\u67d3<\/p>\n<\/p>\n<p><p>\u5c3d\u7ba1Matplotlib\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u4f46\u5bf9\u4e8e\u590d\u6742\u7684\u66f2\u7ebf\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u4e00\u6b65\u4f18\u5316\u56fe\u5f62\u6e32\u67d3\u6548\u7387\u3002\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u6765\u63d0\u9ad8\u7ed8\u56fe\u6027\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528NumPy\u5411\u91cf\u5316\u64cd\u4f5c<\/li>\n<\/ol>\n<p><p>NumPy\u7684\u5411\u91cf\u5316\u64cd\u4f5c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u3002\u901a\u8fc7\u5c06\u5faa\u73af\u64cd\u4f5c\u66ff\u6362\u4e3a\u5411\u91cf\u5316\u64cd\u4f5c\uff0c\u53ef\u4ee5\u52a0\u5feb\u5750\u6807\u8ba1\u7b97\u7684\u901f\u5ea6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5411\u91cf\u5316\u64cd\u4f5c<\/p>\n<p>x = a * np.cos(n * t) * np.cos(t)<\/p>\n<p>y = a * np.cos(n * t) * np.sin(t)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u6e32\u67d3\u4f18\u5316\u4e0e\u591a\u7ebf\u7a0b\u5904\u7406<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u7ed8\u56fe\u4efb\u52a1\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u591a\u7ebf\u7a0b\u6216\u5176\u4ed6\u5e76\u884c\u8ba1\u7b97\u6280\u672f\u6765\u63d0\u9ad8\u6e32\u67d3\u6548\u7387\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>concurrent.futures<\/code>\u6a21\u5757\u6765\u5e76\u884c\u5316\u8ba1\u7b97\u8fc7\u7a0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import concurrent.futures<\/p>\n<p>def compute_coordinates(t_values):<\/p>\n<p>    x_values = a * np.cos(n * t_values) * np.cos(t_values)<\/p>\n<p>    y_values = a * np.cos(n * t_values) * np.sin(t_values)<\/p>\n<p>    return x_values, y_values<\/p>\n<h2><strong>\u4f7f\u7528\u591a\u7ebf\u7a0b\u8ba1\u7b97<\/strong><\/h2>\n<p>with concurrent.futures.ThreadPoolExecutor() as executor:<\/p>\n<p>    results = executor.map(compute_coordinates, np.array_split(t, 4))<\/p>\n<h2><strong>\u5408\u5e76\u7ed3\u679c<\/strong><\/h2>\n<p>x, y = zip(*results)<\/p>\n<p>x = np.concatenate(x)<\/p>\n<p>y = np.concatenate(y)<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u4f18\u5316\u65b9\u6cd5\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7e41\u82b1\u66f2\u7ebf\u7684\u7ed8\u5236\u6548\u7387\uff0c\u5e76\u5728\u8f83\u77ed\u7684\u65f6\u95f4\u5185\u751f\u6210\u9ad8\u8d28\u91cf\u7684\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5176\u4ed6\u7ed8\u56fe\u5e93\u4e0e\u5de5\u5177\u7684\u9009\u62e9<\/p>\n<\/p>\n<p><p>\u9664\u4e86Matplotlib\uff0cPython\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u7ed8\u56fe\u5e93\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u590d\u6742\u66f2\u7ebf\uff0c\u5982Plotly\u3001Bokeh\u3001Pygal\u7b49\u3002\u8fd9\u4e9b\u5e93\u5404\u6709\u7279\u8272\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<ol>\n<li>Plotly\u5e93<\/li>\n<\/ol>\n<p><p>Plotly\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u751f\u6210\u4ea4\u4e92\u5f0f\u56fe\u5f62\u548c\u4eea\u8868\u76d8\u3002\u5b83\u5177\u6709\u826f\u597d\u7684\u53ef\u89c6\u5316\u6548\u679c\uff0c\u9002\u5408\u7528\u4e8e\u7f51\u7edc\u5e94\u7528\u548c\u5c55\u793a\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Plotly\u7ed8\u5236\u7e41\u82b1\u66f2\u7ebf\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.graph_objects as go<\/p>\n<p>x = a * np.cos(n * t) * np.cos(t)<\/p>\n<p>y = a * np.cos(n * t) * np.sin(t)<\/p>\n<p>fig = go.Figure(data=go.Scatter(x=x, y=y, mode=&#39;lines&#39;))<\/p>\n<p>fig.update_layout(title=&#39;\u7e41\u82b1\u66f2\u7ebf&#39;, xaxis=dict(scaleanchor=&#39;y&#39;, scaleratio=1))<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>Bokeh\u5e93<\/li>\n<\/ol>\n<p><p>Bokeh\u662f\u4e00\u4e2a\u9002\u7528\u4e8eWeb\u7684\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u5e93\uff0c\u80fd\u591f\u751f\u6210\u52a8\u6001\u7684\u4ea4\u4e92\u5f0f\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Bokeh\u7ed8\u5236\u7e41\u82b1\u66f2\u7ebf\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bokeh.plotting import figure, show, output_notebook<\/p>\n<p>output_notebook()<\/p>\n<p>x = a * np.cos(n * t) * np.cos(t)<\/p>\n<p>y = a * np.cos(n * t) * np.sin(t)<\/p>\n<p>p = figure(title=&quot;\u7e41\u82b1\u66f2\u7ebf&quot;, match_aspect=True)<\/p>\n<p>p.line(x, y)<\/p>\n<p>show(p)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u603b\u7ed3\u4e0e\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u7e41\u82b1\u66f2\u7ebf\u4f5c\u4e3a\u4e00\u79cd\u590d\u6742\u7684\u6570\u5b66\u66f2\u7ebf\uff0c\u5177\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\uff0c\u5305\u62ec\u8ba1\u7b97\u673a\u56fe\u5f62\u5b66\u3001\u827a\u672f\u8bbe\u8ba1\u3001\u6570\u5b66\u7814\u7a76\u7b49\u3002\u901a\u8fc7\u672c\u6587\u4ecb\u7ecd\u7684\u4f7f\u7528Python\u7ed8\u5236\u7e41\u82b1\u66f2\u7ebf\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u548c\u7814\u7a76\u4eba\u5458\u9ad8\u6548\u5730\u751f\u6210\u548c\u5206\u6790\u8fd9\u4e9b\u66f2\u7ebf\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5e94\u7528\u573a\u666f<\/li>\n<\/ol>\n<p><p>\u7e41\u82b1\u66f2\u7ebf\u5728\u8ba1\u7b97\u673a\u56fe\u5f62\u5b66\u4e2d\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u590d\u6742\u7684\u56fe\u5f62\u548c\u56fe\u6848\uff0c\u7279\u522b\u662f\u5728\u827a\u672f\u8bbe\u8ba1\u548c\u6570\u636e\u53ef\u89c6\u5316\u9886\u57df\u3002\u6b64\u5916\uff0c\u5728\u6570\u5b66\u7814\u7a76\u4e2d\uff0c\u7e41\u82b1\u66f2\u7ebf\u4e5f\u53ef\u4ee5\u7528\u4e8e\u7814\u7a76\u51fd\u6570\u7684\u6027\u8d28\u548c\u884c\u4e3a\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u672a\u6765\u53d1\u5c55<\/li>\n<\/ol>\n<p><p>\u968f\u7740\u8ba1\u7b97\u673a\u786c\u4ef6\u548c\u8f6f\u4ef6\u6280\u672f\u7684\u4e0d\u65ad\u8fdb\u6b65\uff0c\u590d\u6742\u66f2\u7ebf\u7684\u7ed8\u5236\u5c06\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\u548c\u76f4\u89c2\u3002\u672a\u6765\u7684\u7814\u7a76\u548c\u5f00\u53d1\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63a2\u7d22\u7e41\u82b1\u66f2\u7ebf\u5728\u4e0d\u540c\u9886\u57df\u7684\u5e94\u7528\uff0c\u5f00\u53d1\u66f4\u5148\u8fdb\u7684\u7ed8\u56fe\u5de5\u5177\u548c\u7b97\u6cd5\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u65ad\u589e\u957f\u7684\u8ba1\u7b97\u548c\u53ef\u89c6\u5316\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u7e41\u82b1\u66f2\u7ebf\uff1f<\/strong><br \/>\u8981\u7ed8\u5236\u7e41\u82b1\u66f2\u7ebf\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684<code>matplotlib<\/code>\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86<code>matplotlib<\/code>\u548c<code>numpy<\/code>\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u4f7f\u7528\u6781\u5750\u6807\u7cfb\u7edf\u6765\u7ed8\u5236\u7e41\u82b1\u66f2\u7ebf\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nimport matplotlib.pyplot as plt\n\n# \u8bbe\u7f6e\u53c2\u6570\nk = 5  # \u82b1\u74e3\u6570\u91cf\ntheta = np.linspace(0, 2 * np.pi, 1000)\nr = np.sin(k * theta)  # \u7e41\u82b1\u66f2\u7ebf\u65b9\u7a0b\n\n# \u7ed8\u5236\u66f2\u7ebf\nplt.figure(figsize=(8, 8))\nplt.polar(theta, r)\nplt.title(&#39;\u7e41\u82b1\u66f2\u7ebf&#39;)\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u751f\u6210\u4e00\u4e2a\u5305\u542b5\u4e2a\u82b1\u74e3\u7684\u7e41\u82b1\u66f2\u7ebf\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u53c2\u6570\u3002<\/p>\n<p><strong>\u7ed8\u5236\u7e41\u82b1\u66f2\u7ebf\u65f6\uff0c\u5982\u4f55\u8c03\u6574\u82b1\u74e3\u7684\u6570\u91cf\u548c\u5f62\u72b6\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u7e41\u82b1\u66f2\u7ebf\u65f6\uff0c\u82b1\u74e3\u7684\u6570\u91cf\u548c\u5f62\u72b6\u4e3b\u8981\u7531\u53c2\u6570<code>k<\/code>\u51b3\u5b9a\u3002\u5728\u66f2\u7ebf\u65b9\u7a0b<code>r = sin(k * theta)<\/code>\u4e2d\uff0c<code>k<\/code>\u7684\u503c\u8d8a\u5927\uff0c\u82b1\u74e3\u6570\u91cf\u8d8a\u591a\u3002\u60a8\u53ef\u4ee5\u5c1d\u8bd5\u4e0d\u540c\u7684<code>k<\/code>\u503c\uff0c\u4f8b\u5982\u8bbe\u7f6e\u4e3a3\u30014\u30016\u7b49\uff0c\u89c2\u5bdf\u66f2\u7ebf\u7684\u53d8\u5316\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>cos<\/code>\u51fd\u6570\u66ff\u4ee3<code>sin<\/code>\uff0c\u6216\u8005\u8c03\u6574\u65b9\u7a0b\u4e2d\u7684\u5176\u4ed6\u53c2\u6570\uff0c\u4ee5\u521b\u9020\u4e0d\u540c\u7684\u89c6\u89c9\u6548\u679c\u3002<\/p>\n<p><strong>\u9664\u4e86matplotlib\uff0c\u8fd8\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u7ed8\u5236\u7e41\u82b1\u66f2\u7ebf\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u9664\u4e86<code>matplotlib<\/code>\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>seaborn<\/code>\u3001<code>plotly<\/code>\u7b49\u5176\u4ed6\u5e93\u6765\u7ed8\u5236\u7e41\u82b1\u66f2\u7ebf\u3002<code>plotly<\/code>\u7279\u522b\u9002\u5408\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u5f62\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u6ed1\u52a8\u6761\u7b49\u65b9\u5f0f\u52a8\u6001\u8c03\u6574\u53c2\u6570\uff0c\u5b9e\u65f6\u67e5\u770b\u66f2\u7ebf\u7684\u53d8\u5316\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>plotly<\/code>\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nimport plotly.graph_objects as go\n\nk = 5\ntheta = np.linspace(0, 2 * np.pi, 1000)\nr = np.sin(k * theta)\n\nfig = go.Figure(data=go.Scatterpolar(r=r, theta=theta, mode=&#39;lines&#39;))\nfig.update_layout(title=&#39;\u7e41\u82b1\u66f2\u7ebf&#39;)\nfig.show()\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u5728Web\u6d4f\u89c8\u5668\u4e2d\u67e5\u770b\u4e92\u52a8\u6548\u679c\uff0c\u589e\u5f3a\u53ef\u89c6\u5316\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u7ed8\u5236\u7e41\u82b1\u66f2\u7ebf\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff0c\u5305\u62ec\u4f7f\u7528Matplotlib\u5e93\u3001\u53c2\u6570\u5316\u65b9\u7a0b\u4ee5\u53ca\u8ba1\u7b97\u673a\u7ed8\u56fe\u7b97\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":956052,"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\/956044"}],"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=956044"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/956044\/revisions"}],"predecessor-version":[{"id":956055,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/956044\/revisions\/956055"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/956052"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=956044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=956044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=956044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}