{"id":1093312,"date":"2025-01-08T14:29:49","date_gmt":"2025-01-08T06:29:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1093312.html"},"modified":"2025-01-08T14:29:52","modified_gmt":"2025-01-08T06:29:52","slug":"%e5%a6%82%e4%bd%95%e5%86%99python%e7%9a%84%e6%95%b0%e5%ad%a6%e5%85%ac%e5%bc%8f-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1093312.html","title":{"rendered":"\u5982\u4f55\u5199python\u7684\u6570\u5b66\u516c\u5f0f"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205942\/7813adc5-b0d9-4537-a798-53d8f2f1cda3.webp\" alt=\"\u5982\u4f55\u5199python\u7684\u6570\u5b66\u516c\u5f0f\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u5199Python\u7684\u6570\u5b66\u516c\u5f0f<\/strong><br \/><strong>\u4f7f\u7528\u5e93\u5982SymPy\u548cNumPy\u8fdb\u884c\u7b26\u53f7\u8ba1\u7b97\u3001\u4f7f\u7528Matplotlib\u548cSeaborn\u8fdb\u884c\u53ef\u89c6\u5316\u3001\u4f7f\u7528LaTeX\u683c\u5f0f\u8868\u793a\u6570\u5b66\u516c\u5f0f<\/strong>\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u7f16\u5199\u6570\u5b66\u516c\u5f0f\uff0c\u5e76\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u516c\u5f0f\u8fdb\u884c\u8ba1\u7b97\u548c\u53ef\u89c6\u5316\u3002\u7279\u522b\u5730\uff0c\u6211\u4eec\u5c06\u91cd\u70b9\u4ecb\u7ecdSymPy\u548cNumPy\u5e93\uff0c\u5b83\u4eec\u5728\u7b26\u53f7\u8ba1\u7b97\u548c\u6570\u503c\u8ba1\u7b97\u65b9\u9762\u975e\u5e38\u5f3a\u5927\u3002 <\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528SymPy\u8fdb\u884c\u7b26\u53f7\u8ba1\u7b97<br \/>SymPy\u662f\u4e00\u4e2a\u7528\u4e8e\u7b26\u53f7\u8ba1\u7b97\u7684Python\u5e93\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u8fdb\u884c\u4ee3\u6570\u8ba1\u7b97\u3001\u6c42\u89e3\u65b9\u7a0b\u3001\u79ef\u5206\u548c\u5fae\u5206\u7b49\u3002\u901a\u8fc7\u4f7f\u7528SymPy\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u5730\u8868\u793a\u548c\u64cd\u4f5c\u6570\u5b66\u516c\u5f0f\u3002 <\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5SymPy<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528SymPy\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install sympy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u7b26\u53f7\u8ba1\u7b97\u57fa\u7840<\/h3>\n<\/p>\n<p><p>SymPy\u5141\u8bb8\u7528\u6237\u5b9a\u4e49\u7b26\u53f7\uff0c\u5e76\u5bf9\u8fd9\u4e9b\u7b26\u53f7\u8fdb\u884c\u5404\u79cd\u6570\u5b66\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sympy as sp<\/p>\n<h2><strong>\u5b9a\u4e49\u7b26\u53f7<\/strong><\/h2>\n<p>x, y = sp.symbols(&#39;x y&#39;)<\/p>\n<h2><strong>\u8868\u793a\u4e00\u4e2a\u516c\u5f0f<\/strong><\/h2>\n<p>formula = x2 + 2*x + 1<\/p>\n<h2><strong>\u5c55\u5f00\u516c\u5f0f<\/strong><\/h2>\n<p>expanded_formula = sp.expand(formula)<\/p>\n<h2><strong>\u56e0\u5f0f\u5206\u89e3<\/strong><\/h2>\n<p>factored_formula = sp.factor(formula)<\/p>\n<p>print(&quot;Original formula:&quot;, formula)<\/p>\n<p>print(&quot;Expanded formula:&quot;, expanded_formula)<\/p>\n<p>print(&quot;Factored formula:&quot;, factored_formula)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e24\u4e2a\u7b26\u53f7x\u548cy\uff0c\u5e76\u8868\u793a\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u4e8c\u6b21\u516c\u5f0f\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u5c55\u5f00\u548c\u56e0\u5f0f\u5206\u89e3\u8fd9\u4e2a\u516c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u6c42\u89e3\u65b9\u7a0b<\/h3>\n<\/p>\n<p><p>SymPy\u8fd8\u53ef\u4ee5\u7528\u6765\u6c42\u89e3\u65b9\u7a0b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u6c42\u89e3\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u65b9\u7a0b<\/p>\n<p>equation = sp.Eq(x2 + 2*x + 1, 0)<\/p>\n<h2><strong>\u6c42\u89e3\u65b9\u7a0b<\/strong><\/h2>\n<p>solutions = sp.solve(equation, x)<\/p>\n<p>print(&quot;Solutions:&quot;, solutions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e00\u5143\u4e8c\u6b21\u65b9\u7a0b\uff0c\u5e76\u4f7f\u7528SymPy\u7684solve\u51fd\u6570\u6765\u6c42\u89e3\u65b9\u7a0b\u7684\u6839\u3002<\/p>\n<\/p>\n<p><h3>4\u3001\u79ef\u5206\u548c\u5fae\u5206<\/h3>\n<\/p>\n<p><p>SymPy\u8fd8\u53ef\u4ee5\u8fdb\u884c\u79ef\u5206\u548c\u5fae\u5206\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570<\/p>\n<p>f = sp.sin(x)<\/p>\n<h2><strong>\u6c42\u5bfc\u6570<\/strong><\/h2>\n<p>derivative = sp.diff(f, x)<\/p>\n<h2><strong>\u6c42\u79ef\u5206<\/strong><\/h2>\n<p>integral = sp.integrate(f, x)<\/p>\n<p>print(&quot;Derivative:&quot;, derivative)<\/p>\n<p>print(&quot;Integral:&quot;, integral)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6b63\u5f26\u51fd\u6570\uff0c\u5e76\u8ba1\u7b97\u4e86\u5b83\u7684\u5bfc\u6570\u548c\u4e0d\u5b9a\u79ef\u5206\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528NumPy\u8fdb\u884c\u6570\u503c\u8ba1\u7b97<br \/>NumPy\u662f\u4e00\u4e2a\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\u7684Python\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u652f\u6301\u9ad8\u6027\u80fd\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u8ba1\u7b97\u7684\u529f\u80fd\u3002\u5c3d\u7ba1NumPy\u4e0d\u652f\u6301\u7b26\u53f7\u8ba1\u7b97\uff0c\u4f46\u5b83\u5728\u6570\u503c\u8ba1\u7b97\u65b9\u9762\u975e\u5e38\u5f3a\u5927\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5NumPy<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528NumPy\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u521b\u5efa\u6570\u7ec4\u548c\u77e9\u9635<\/h3>\n<\/p>\n<p><p>NumPy\u7684\u6838\u5fc3\u662fndarray\u5bf9\u8c61\uff0c\u5b83\u662f\u4e00\u4e2a\u591a\u7ef4\u6570\u7ec4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u521b\u5efa\u6570\u7ec4\u548c\u77e9\u9635\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_1d = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4\uff08\u77e9\u9635\uff09<\/strong><\/h2>\n<p>matrix_2d = np.array([[1, 2], [3, 4], [5, 6]])<\/p>\n<p>print(&quot;1D Array:&quot;, array_1d)<\/p>\n<p>print(&quot;2D Matrix:&quot;, matrix_2d)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u6570\u7ec4\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u5982\u5143\u7d20\u8bbf\u95ee\u3001\u5207\u7247\u3001\u5f62\u72b6\u53d8\u6362\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbf\u95ee\u6570\u7ec4\u5143\u7d20<\/p>\n<p>element = array_1d[2]<\/p>\n<h2><strong>\u6570\u7ec4\u5207\u7247<\/strong><\/h2>\n<p>sub_array = array_1d[1:4]<\/p>\n<h2><strong>\u5f62\u72b6\u53d8\u6362<\/strong><\/h2>\n<p>reshaped_matrix = matrix_2d.reshape(2, 3)<\/p>\n<p>print(&quot;Element at index 2:&quot;, element)<\/p>\n<p>print(&quot;Sub-array:&quot;, sub_array)<\/p>\n<p>print(&quot;Reshaped matrix:&quot;, reshaped_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u6570\u5b66\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>NumPy\u652f\u6301\u591a\u79cd\u6570\u5b66\u8fd0\u7b97\uff0c\u5982\u52a0\u51cf\u4e58\u9664\u3001\u77e9\u9635\u4e58\u6cd5\u3001\u6c42\u548c\u3001\u5e73\u5747\u503c\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u7ec4\u52a0\u6cd5<\/p>\n<p>sum_array = array_1d + 2<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>matrix_product = np.dot(matrix_2d, matrix_2d.T)<\/p>\n<h2><strong>\u6c42\u548c<\/strong><\/h2>\n<p>array_sum = np.sum(array_1d)<\/p>\n<h2><strong>\u5e73\u5747\u503c<\/strong><\/h2>\n<p>array_mean = np.mean(array_1d)<\/p>\n<p>print(&quot;Sum array:&quot;, sum_array)<\/p>\n<p>print(&quot;Matrix product:&quot;, matrix_product)<\/p>\n<p>print(&quot;Array sum:&quot;, array_sum)<\/p>\n<p>print(&quot;Array mean:&quot;, array_mean)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e9b\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528NumPy\u8fdb\u884c\u57fa\u672c\u7684\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528Matplotlib\u8fdb\u884c\u516c\u5f0f\u53ef\u89c6\u5316<br \/>Matplotlib\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u9759\u6001\u3001\u52a8\u753b\u548c\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u7684Python\u5e93\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u6765\u7ed8\u5236\u6570\u5b66\u516c\u5f0f\u7684\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5Matplotlib<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Matplotlib\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528pip\u6765\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><h3>2\u3001\u7ed8\u5236\u7b80\u5355\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7ed8\u5236\u7b80\u5355\u51fd\u6570\u56fe\u5f62\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>def f(x):<\/p>\n<p>    return x2 + 2*x + 1<\/p>\n<h2><strong>\u751f\u6210x\u503c<\/strong><\/h2>\n<p>x = np.linspace(-10, 10, 100)<\/p>\n<h2><strong>\u8ba1\u7b97y\u503c<\/strong><\/h2>\n<p>y = f(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<p>plt.xlabel(&#39;x&#39;)<\/p>\n<p>plt.ylabel(&#39;f(x)&#39;)<\/p>\n<p>plt.title(&#39;Plot of f(x) = x^2 + 2x + 1&#39;)<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e8c\u6b21\u51fd\u6570\uff0c\u5e76\u4f7f\u7528Matplotlib\u7ed8\u5236\u4e86\u5b83\u7684\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u7ed8\u5236\u591a\u4e2a\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u540c\u4e00\u56fe\u8868\u4e2d\u7ed8\u5236\u591a\u4e2a\u51fd\u6570\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u51fd\u6570<\/p>\n<p>def g(x):<\/p>\n<p>    return np.sin(x)<\/p>\n<h2><strong>\u8ba1\u7b97y\u503c<\/strong><\/h2>\n<p>y1 = f(x)<\/p>\n<p>y2 = g(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x, y1, label=&#39;f(x) = x^2 + 2x + 1&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;g(x) = sin(x)&#39;)<\/p>\n<p>plt.xlabel(&#39;x&#39;)<\/p>\n<p>plt.ylabel(&#39;y&#39;)<\/p>\n<p>plt.title(&#39;Multiple Plots&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728\u540c\u4e00\u56fe\u8868\u4e2d\u7ed8\u5236\u4e86\u4e24\u4e2a\u51fd\u6570\u7684\u56fe\u5f62\uff0c\u5e76\u4f7f\u7528\u56fe\u4f8b\u533a\u5206\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528LaTeX\u683c\u5f0f\u8868\u793a\u6570\u5b66\u516c\u5f0f<br \/>LaTeX\u662f\u4e00\u79cd\u7528\u4e8e\u6392\u7248\u6280\u672f\u6587\u6863\u7684\u8bed\u8a00\uff0c\u7279\u522b\u9002\u5408\u8868\u793a\u6570\u5b66\u516c\u5f0f\u3002\u6211\u4eec\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u4f7f\u7528LaTeX\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u6765\u663e\u793a\u6570\u5b66\u516c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5728Matplotlib\u4e2d\u4f7f\u7528LaTeX<\/h3>\n<\/p>\n<p><p>Matplotlib\u652f\u6301\u5728\u56fe\u8868\u4e2d\u4f7f\u7528LaTeX\u683c\u5f0f\u7684\u6570\u5b66\u516c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ed8\u5236\u56fe\u5f62<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.xlabel(&#39;x&#39;)<\/p>\n<p>plt.ylabel(r&#39;$f(x) = x^2 + 2x + 1$&#39;)<\/p>\n<p>plt.title(r&#39;Plot of $f(x) = x^2 + 2x + 1$&#39;)<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5728y\u8f74\u6807\u7b7e\u548c\u56fe\u8868\u6807\u9898\u4e2d\u4f7f\u7528\u4e86LaTeX\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u5728Jupyter Notebook\u4e2d\u4f7f\u7528LaTeX<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u5728Jupyter Notebook\u4e2d\u5de5\u4f5c\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528LaTeX\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u6765\u663e\u793a\u6570\u5b66\u516c\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from IPython.display import display, Math<\/p>\n<h2><strong>\u663e\u793a\u6570\u5b66\u516c\u5f0f<\/strong><\/h2>\n<p>display(Math(r&#39;f(x) = x^2 + 2x + 1&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528IPython.display\u6a21\u5757\u4e2d\u7684Math\u7c7b\u6765\u663e\u793aLaTeX\u683c\u5f0f\u7684\u6570\u5b66\u516c\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u7efc\u5408\u793a\u4f8b<br \/>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u7f16\u5199\u548c\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\uff0c\u6211\u4eec\u5c06\u7ed3\u5408SymPy\u3001NumPy\u548cMatplotlib\u8fdb\u884c\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\u3002\u6211\u4eec\u5c06\u5b9a\u4e49\u4e00\u4e2a\u4e8c\u6b21\u51fd\u6570\uff0c\u4f7f\u7528SymPy\u8fdb\u884c\u7b26\u53f7\u8ba1\u7b97\uff0c\u4f7f\u7528NumPy\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\uff0c\u5e76\u4f7f\u7528Matplotlib\u8fdb\u884c\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b9a\u4e49\u51fd\u6570\u548c\u6c42\u89e3\u5bfc\u6570<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u4e8c\u6b21\u51fd\u6570\uff0c\u5e76\u4f7f\u7528SymPy\u6c42\u89e3\u5b83\u7684\u5bfc\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sympy as sp<\/p>\n<h2><strong>\u5b9a\u4e49\u7b26\u53f7<\/strong><\/h2>\n<p>x = sp.symbols(&#39;x&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>f = x2 + 2*x + 1<\/p>\n<h2><strong>\u6c42\u5bfc\u6570<\/strong><\/h2>\n<p>f_prime = sp.diff(f, x)<\/p>\n<p>print(&quot;Function:&quot;, f)<\/p>\n<p>print(&quot;Derivative:&quot;, f_prime)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u6570\u503c\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528NumPy\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5c06SymPy\u516c\u5f0f\u8f6c\u6362\u4e3aNumPy\u51fd\u6570<\/strong><\/h2>\n<p>f_np = sp.lambdify(x, f, &#39;numpy&#39;)<\/p>\n<p>f_prime_np = sp.lambdify(x, f_prime, &#39;numpy&#39;)<\/p>\n<h2><strong>\u751f\u6210x\u503c<\/strong><\/h2>\n<p>x_values = np.linspace(-10, 10, 100)<\/p>\n<h2><strong>\u8ba1\u7b97y\u503c<\/strong><\/h2>\n<p>y_values = f_np(x_values)<\/p>\n<p>y_prime_values = f_prime_np(x_values)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u7ed8\u5236\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528Matplotlib\u7ed8\u5236\u51fd\u6570\u53ca\u5176\u5bfc\u6570\u7684\u56fe\u5f62\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u51fd\u6570\u56fe\u5f62<\/strong><\/h2>\n<p>plt.plot(x_values, y_values, label=r&#39;$f(x) = x^2 + 2x + 1$&#39;)<\/p>\n<p>plt.plot(x_values, y_prime_values, label=r&quot;$f&#39;(x) = 2x + 2$&quot;)<\/p>\n<p>plt.xlabel(&#39;x&#39;)<\/p>\n<p>plt.ylabel(&#39;y&#39;)<\/p>\n<p>plt.title(r&#39;Plot of $f(x) = x^2 + 2x + 1$ and its derivative&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u7efc\u5408\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u7ed3\u5408\u4f7f\u7528SymPy\u3001NumPy\u548cMatplotlib\u6765\u5b9a\u4e49\u3001\u8ba1\u7b97\u548c\u53ef\u89c6\u5316\u6570\u5b66\u516c\u5f0f\u3002\u901a\u8fc7\u8fd9\u4e9b\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u5730\u5904\u7406\u5404\u79cd\u6570\u5b66\u95ee\u9898\uff0c\u5e76\u751f\u6210\u9ad8\u8d28\u91cf\u7684\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\u8d77\u6765\uff0cPython\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5e93\u548c\u5de5\u5177\u6765\u7f16\u5199\u548c\u4f7f\u7528\u6570\u5b66\u516c\u5f0f\u3002<strong>SymPy\u9002\u7528\u4e8e\u7b26\u53f7\u8ba1\u7b97\u3001NumPy\u9002\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\u3001Matplotlib\u9002\u7528\u4e8e\u53ef\u89c6\u5316\u3001LaTeX\u683c\u5f0f\u7528\u4e8e\u8868\u793a\u6570\u5b66\u516c\u5f0f<\/strong>\u3002\u901a\u8fc7\u7efc\u5408\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u9ad8\u6548\u5730\u5904\u7406\u5404\u79cd\u6570\u5b66\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u590d\u6742\u7684\u6570\u5b66\u516c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u6765\u5b9e\u73b0\u590d\u6742\u7684\u6570\u5b66\u516c\u5f0f\u3002\u4f8b\u5982\uff0cNumPy\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u548c\u6570\u5b66\u51fd\u6570\uff0c\u800cSymPy\u5219\u662f\u7528\u4e8e\u7b26\u53f7\u8ba1\u7b97\u7684\u5e93\uff0c\u53ef\u4ee5\u5904\u7406\u4ee3\u6570\u8868\u8fbe\u5f0f\u3001\u5fae\u79ef\u5206\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u5e93\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5730\u7f16\u5199\u548c\u8ba1\u7b97\u590d\u6742\u7684\u6570\u5b66\u516c\u5f0f\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u6765\u5904\u7406\u6570\u5b66\u516c\u5f0f\uff1f<\/strong><br \/>Python\u6709\u8bb8\u591a\u5e93\u4e13\u95e8\u7528\u4e8e\u6570\u5b66\u8ba1\u7b97\u3002NumPy\u548cSciPy\u662f\u6700\u5e38\u7528\u7684\uff0c\u524d\u8005\u9002\u5408\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\uff0c\u540e\u8005\u5219\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u79d1\u5b66\u8ba1\u7b97\u529f\u80fd\u3002\u5bf9\u4e8e\u9700\u8981\u7b26\u53f7\u8ba1\u7b97\u7684\u7528\u6237\uff0cSymPy\u662f\u4e00\u4e2a\u7406\u60f3\u7684\u9009\u62e9\u3002\u8fd8\u6709Matplotlib\u53ef\u4ee5\u7528\u4e8e\u53ef\u89c6\u5316\u6570\u5b66\u516c\u5f0f\u7684\u56fe\u5f62\u8868\u793a\uff0c\u5e2e\u52a9\u7528\u6237\u66f4\u597d\u5730\u7406\u89e3\u516c\u5f0f\u7684\u542b\u4e49\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u6570\u5b66\u516c\u5f0f\u7684\u56fe\u5f62\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u7ed8\u5236\u6570\u5b66\u516c\u5f0f\u7684\u56fe\u5f62\u3002\u901a\u8fc7\u5b9a\u4e49\u51fd\u6570\u5e76\u4f7f\u7528Matplotlib\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u53ef\u4ee5\u5c06\u6570\u5b66\u516c\u5f0f\u53ef\u89c6\u5316\u3002\u4f8b\u5982\uff0c\u7528\u6237\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u8868\u793a\u67d0\u4e2a\u516c\u5f0f\u7684\u7ed3\u679c\uff0c\u5e76\u4f7f\u7528<code>plt.plot()<\/code>\u6765\u7ed8\u5236\u5176\u56fe\u50cf\uff0c\u8fd9\u6837\u53ef\u4ee5\u76f4\u89c2\u5730\u770b\u5230\u516c\u5f0f\u7684\u53d8\u5316\u8d8b\u52bf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u5199Python\u7684\u6570\u5b66\u516c\u5f0f\u4f7f\u7528\u5e93\u5982SymPy\u548cNumPy\u8fdb\u884c\u7b26\u53f7\u8ba1\u7b97\u3001\u4f7f\u7528Matplotlib\u548cSeabor [&hellip;]","protected":false},"author":3,"featured_media":1093318,"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\/1093312"}],"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=1093312"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1093312\/revisions"}],"predecessor-version":[{"id":1093321,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1093312\/revisions\/1093321"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1093318"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1093312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1093312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1093312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}