{"id":1067653,"date":"2024-12-31T16:36:29","date_gmt":"2024-12-31T08:36:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1067653.html"},"modified":"2024-12-31T16:36:31","modified_gmt":"2024-12-31T08:36:31","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%bb%e7%94%b5%e8%b7%af%e7%9a%84%e7%9b%b8%e9%87%8f%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1067653.html","title":{"rendered":"\u5982\u4f55\u7528python\u753b\u7535\u8def\u7684\u76f8\u91cf\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/3f192f03-afbc-4239-a2d1-12b2a44d0768.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u753b\u7535\u8def\u7684\u76f8\u91cf\u56fe\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u7ed8\u5236\u7535\u8def\u7684\u76f8\u91cf\u56fe\u7684\u6b65\u9aa4\u662f\uff1a\u9009\u62e9\u5408\u9002\u7684\u7ed8\u56fe\u5e93\u3001\u5b9a\u4e49\u7535\u8def\u53c2\u6570\u3001\u8ba1\u7b97\u76f8\u91cf\u3001\u7ed8\u5236\u76f8\u91cf\u56fe\u3002<\/strong> \u5176\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u7ed8\u56fe\u5e93\u662f\u6700\u4e3a\u5173\u952e\u7684\u4e00\u6b65\uff0c\u5e38\u7528\u7684\u5e93\u5305\u62ecMatplotlib\u548cPlotly\u3002\u4ee5Matplotlib\u4e3a\u4f8b\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u76842D\u7ed8\u56fe\u529f\u80fd\uff0c\u80fd\u591f\u6ee1\u8db3\u5927\u591a\u6570\u76f8\u91cf\u56fe\u7684\u7ed8\u5236\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u7684Matplotlib\u5e93\u6765\u7ed8\u5236\u7535\u8def\u7684\u76f8\u91cf\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u7ed8\u56fe\u5e93<\/h3>\n<\/p>\n<p><p>\u7ed8\u5236\u76f8\u91cf\u56fe\u9700\u8981\u4e00\u4e2a\u80fd\u591f\u5904\u7406\u590d\u6742\u6570\u548c\u5411\u91cf\u7684\u7ed8\u56fe\u5e93\u3002Matplotlib\u662fPython\u4e2d\u975e\u5e38\u6d41\u884c\u7684\u7ed8\u56fe\u5e93\uff0c\u5b83\u4e0d\u4ec5\u529f\u80fd\u5f3a\u5927\uff0c\u800c\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u901a\u8fc7Matplotlib\u53ef\u4ee5\u8f7b\u677e\u7ed8\u52362D\u56fe\u5f62\uff0c\u5305\u62ec\u76f8\u91cf\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b9a\u4e49\u7535\u8def\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u76f8\u91cf\u56fe\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u7535\u8def\u7684\u53c2\u6570\u3002\u8fd9\u4e9b\u53c2\u6570\u5305\u62ec\u7535\u538b\u3001\u7535\u6d41\u3001\u963b\u6297\u7b49\u3002\u6211\u4eec\u901a\u5e38\u4f7f\u7528\u590d\u6570\u6765\u8868\u793a\u8fd9\u4e9b\u53c2\u6570\uff0c\u56e0\u4e3a\u590d\u6570\u80fd\u591f\u540c\u65f6\u8868\u793a\u5e45\u503c\u548c\u76f8\u4f4d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u7535\u538b\u548c\u7535\u6d41\u7684\u5e45\u503c\u548c\u76f8\u4f4d\uff08\u5355\u4f4d\uff1a\u4f0f\u7279\u548c\u5b89\u57f9\uff09<\/strong><\/h2>\n<p>V_magnitude = 10<\/p>\n<p>V_phase = np.deg2rad(30)  # \u5c06\u89d2\u5ea6\u8f6c\u6362\u4e3a\u5f27\u5ea6<\/p>\n<p>I_magnitude = 5<\/p>\n<p>I_phase = np.deg2rad(-45)  # \u5c06\u89d2\u5ea6\u8f6c\u6362\u4e3a\u5f27\u5ea6<\/p>\n<h2><strong>\u4f7f\u7528\u590d\u6570\u8868\u793a\u7535\u538b\u548c\u7535\u6d41<\/strong><\/h2>\n<p>V = V_magnitude * np.exp(1j * V_phase)<\/p>\n<p>I = I_magnitude * np.exp(1j * I_phase)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8ba1\u7b97\u76f8\u91cf<\/h3>\n<\/p>\n<p><p>\u6709\u4e86\u7535\u538b\u548c\u7535\u6d41\u7684\u590d\u6570\u8868\u793a\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba1\u7b97\u51fa\u5176\u4ed6\u76f8\u91cf\uff0c\u4f8b\u5982\u963b\u6297\u548c\u529f\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u963b\u6297<\/p>\n<p>Z = V \/ I<\/p>\n<h2><strong>\u8ba1\u7b97\u529f\u7387<\/strong><\/h2>\n<p>S = V * np.conj(I)  # \u590d\u5171\u8f6d<\/p>\n<p>P = np.real(S)  # \u6709\u529f\u529f\u7387<\/p>\n<p>Q = np.imag(S)  # \u65e0\u529f\u529f\u7387<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ed8\u5236\u76f8\u91cf\u56fe<\/h3>\n<\/p>\n<p><p>\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7ed8\u5236\u76f8\u91cf\u56fe\u3002\u76f8\u91cf\u56fe\u7684\u7279\u70b9\u662f\u7528\u7bad\u5934\u8868\u793a\u590d\u6570\u7684\u5e45\u503c\u548c\u76f8\u4f4d\u3002Matplotlib\u4e2d\u7684<code>quiver<\/code>\u51fd\u6570\u975e\u5e38\u9002\u5408\u7ed8\u5236\u8fd9\u79cd\u7bad\u5934\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<h2><strong>\u7ed8\u5236\u7535\u538b\u76f8\u91cf<\/strong><\/h2>\n<p>plt.quiver(0, 0, V.real, V.imag, angles=&#39;xy&#39;, scale_units=&#39;xy&#39;, scale=1, color=&#39;r&#39;, label=&#39;Voltage V&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u7535\u6d41\u76f8\u91cf<\/strong><\/h2>\n<p>plt.quiver(0, 0, I.real, I.imag, angles=&#39;xy&#39;, scale_units=&#39;xy&#39;, scale=1, color=&#39;b&#39;, label=&#39;Current I&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74<\/strong><\/h2>\n<p>plt.xlim(-15, 15)<\/p>\n<p>plt.ylim(-15, 15)<\/p>\n<p>plt.xlabel(&#39;Real&#39;)<\/p>\n<p>plt.ylabel(&#39;Imaginary&#39;)<\/p>\n<p>plt.axhline(0, color=&#39;black&#39;,linewidth=0.5)<\/p>\n<p>plt.axvline(0, color=&#39;black&#39;,linewidth=0.5)<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.title(&#39;Phasor Diagram&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u76f8\u91cf\u56fe\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u6269\u5c55\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6dfb\u52a0\u66f4\u591a\u7684\u7535\u8def\u53c2\u6570\uff0c\u7ed8\u5236\u66f4\u591a\u7684\u76f8\u91cf\uff0c\u6216\u8005\u6539\u53d8\u56fe\u5f62\u7684\u6837\u5f0f\u548c\u989c\u8272\u3002<\/p>\n<\/p>\n<p><h4>\u6dfb\u52a0\u66f4\u591a\u76f8\u91cf<\/h4>\n<\/p>\n<p><p>\u5728\u590d\u6742\u7684\u7535\u8def\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed8\u5236\u591a\u4e2a\u76f8\u91cf\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6dfb\u52a0\u53e6\u4e00\u4e2a\u7535\u538b\u6e90\u548c\u7535\u6d41\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u53e6\u4e00\u4e2a\u7535\u538b\u548c\u7535\u6d41<\/p>\n<p>V2_magnitude = 8<\/p>\n<p>V2_phase = np.deg2rad(60)<\/p>\n<p>I2_magnitude = 4<\/p>\n<p>I2_phase = np.deg2rad(-30)<\/p>\n<h2><strong>\u4f7f\u7528\u590d\u6570\u8868\u793a<\/strong><\/h2>\n<p>V2 = V2_magnitude * np.exp(1j * V2_phase)<\/p>\n<p>I2 = I2_magnitude * np.exp(1j * I2_phase)<\/p>\n<h2><strong>\u7ed8\u5236\u76f8\u91cf\u56fe<\/strong><\/h2>\n<p>plt.quiver(0, 0, V2.real, V2.imag, angles=&#39;xy&#39;, scale_units=&#39;xy&#39;, scale=1, color=&#39;g&#39;, label=&#39;Voltage V2&#39;)<\/p>\n<p>plt.quiver(0, 0, I2.real, I2.imag, angles=&#39;xy&#39;, scale_units=&#39;xy&#39;, scale=1, color=&#39;y&#39;, label=&#39;Current I2&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6539\u53d8\u56fe\u5f62\u6837\u5f0f<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7Matplotlib\u7684\u5404\u79cd\u914d\u7f6e\u9009\u9879\u6765\u6539\u53d8\u56fe\u5f62\u7684\u6837\u5f0f\u3002\u4f8b\u5982\uff0c\u6539\u53d8\u7bad\u5934\u7684\u989c\u8272\u3001\u6837\u5f0f\uff0c\u6dfb\u52a0\u7f51\u683c\u7ebf\uff0c\u8bbe\u7f6e\u6807\u9898\u548c\u6807\u7b7e\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6539\u53d8\u7bad\u5934\u6837\u5f0f<\/p>\n<p>plt.quiver(0, 0, V.real, V.imag, angles=&#39;xy&#39;, scale_units=&#39;xy&#39;, scale=1, color=&#39;r&#39;, linestyle=&#39;--&#39;, label=&#39;Voltage V&#39;)<\/p>\n<p>plt.quiver(0, 0, I.real, I.imag, angles=&#39;xy&#39;, scale_units=&#39;xy&#39;, scale=1, color=&#39;b&#39;, linestyle=&#39;-&#39;, label=&#39;Current I&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u7f51\u683c\u7ebf<\/strong><\/h2>\n<p>plt.grid(True)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;Enhanced Phasor Diagram&#39;)<\/p>\n<p>plt.xlabel(&#39;Real Part&#39;)<\/p>\n<p>plt.ylabel(&#39;Imaginary Part&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u4f8b<\/strong><\/h2>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7ed8\u5236\u7535\u8def\u7684\u76f8\u91cf\u56fe\uff0c\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\u9009\u62e9\u5408\u9002\u7684\u7ed8\u56fe\u5e93\u3001\u5b9a\u4e49\u7535\u8def\u53c2\u6570\u3001\u8ba1\u7b97\u76f8\u91cf\u548c\u7ed8\u5236\u76f8\u91cf\u56fe\u3002Matplotlib\u662f\u4e00\u4e2a\u975e\u5e38\u9002\u5408\u7684\u7ed8\u56fe\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7ed8\u56fe\u529f\u80fd\u548c\u7075\u6d3b\u7684\u914d\u7f6e\u9009\u9879\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u7ed8\u5236\u51fa\u7535\u8def\u7684\u76f8\u91cf\u56fe\uff0c\u5e76\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u6269\u5c55\u56fe\u5f62\u7684\u5185\u5bb9\u548c\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u7535\u8def\u7684\u76f8\u91cf\u56fe\u3002\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u6b22\u8fce\u968f\u65f6\u4ea4\u6d41\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728\u4f7f\u7528Python\u7ed8\u5236\u7535\u8def\u76f8\u91cf\u56fe\u65f6\uff0c\u5e94\u8be5\u4f7f\u7528\u54ea\u4e9b\u5e93\uff1f<\/strong><br \/>\u8981\u7ed8\u5236\u7535\u8def\u7684\u76f8\u91cf\u56fe\uff0c\u63a8\u8350\u4f7f\u7528Python\u4e2d\u7684Matplotlib\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ed8\u56fe\u529f\u80fd\u548c\u826f\u597d\u7684\u81ea\u5b9a\u4e49\u9009\u9879\u3002\u6b64\u5916\uff0cNumPy\u5e93\u53ef\u4ee5\u5e2e\u52a9\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\uff0c\u65b9\u4fbf\u5904\u7406\u76f8\u91cf\u7684\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u5728Python\u4e2d\u5bfc\u5165\u54ea\u4e9b\u7535\u8def\u5143\u4ef6\u7684\u6a21\u578b\u6765\u7ed8\u5236\u76f8\u91cf\u56fe\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u7c7b\u6216\u8005\u501f\u52a9\u73b0\u6709\u7684\u5e93\uff08\u5982SciPy\uff09\u6765\u8868\u793a\u7535\u8def\u5143\u4ef6\u6a21\u578b\uff0c\u4f8b\u5982\u7535\u963b\u3001\u7535\u611f\u548c\u7535\u5bb9\u3002\u8fd9\u4e9b\u5143\u4ef6\u53ef\u4ee5\u901a\u8fc7\u5176\u963b\u6297\u6765\u8ba1\u7b97\u76f8\u91cf\uff0c\u5e76\u5728\u76f8\u91cf\u56fe\u4e2d\u8868\u793a\u3002<\/p>\n<p><strong>\u7ed8\u5236\u7535\u8def\u76f8\u91cf\u56fe\u65f6\uff0c\u5982\u4f55\u5904\u7406\u4e0d\u540c\u9891\u7387\u4e0b\u7684\u76f8\u91cf\uff1f<\/strong><br \/>\u4e0d\u540c\u9891\u7387\u4e0b\u7684\u76f8\u91cf\u5bf9\u5e94\u4e0d\u540c\u7684\u963b\u6297\uff0c\u56e0\u6b64\u5728\u7ed8\u5236\u76f8\u91cf\u56fe\u65f6\uff0c\u9700\u8981\u6839\u636e\u9891\u7387\u8ba1\u7b97\u6bcf\u4e2a\u7535\u8def\u5143\u4ef6\u7684\u963b\u6297\u3002\u53ef\u4ee5\u4f7f\u7528\u590d\u6570\u5f62\u5f0f\u6765\u8868\u793a\u76f8\u91cf\uff0c\u5e76\u6839\u636e\u76f8\u4f4d\u89d2\u8fdb\u884c\u8f6c\u6362\uff0c\u786e\u4fdd\u5728\u540c\u4e00\u56fe\u4e2d\u51c6\u786e\u5c55\u793a\u5404\u4e2a\u5143\u4ef6\u7684\u76f8\u91cf\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u663e\u793a\u76f8\u91cf\u56fe\u7684\u7ed3\u679c\u5e76\u8fdb\u884c\u540e\u7eed\u5206\u6790\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>show()<\/code>\u51fd\u6570\u6765\u5c55\u793a\u7ed8\u5236\u7684\u76f8\u91cf\u56fe\u3002\u4e3a\u4e86\u8fdb\u884c\u540e\u7eed\u5206\u6790\uff0c\u53ef\u4ee5\u5c06\u7ed8\u56fe\u6570\u636e\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\uff0c\u6216\u8005\u4f7f\u7528NumPy\u6570\u7ec4\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u6570\u503c\u8ba1\u7b97\u548c\u5206\u6790\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u7814\u7a76\u548c\u9a8c\u8bc1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u7ed8\u5236\u7535\u8def\u7684\u76f8\u91cf\u56fe\u7684\u6b65\u9aa4\u662f\uff1a\u9009\u62e9\u5408\u9002\u7684\u7ed8\u56fe\u5e93\u3001\u5b9a\u4e49\u7535\u8def\u53c2\u6570\u3001\u8ba1\u7b97\u76f8\u91cf\u3001\u7ed8\u5236\u76f8\u91cf\u56fe\u3002 \u5176\u4e2d\uff0c\u9009\u62e9\u5408 [&hellip;]","protected":false},"author":3,"featured_media":1067659,"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\/1067653"}],"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=1067653"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1067653\/revisions"}],"predecessor-version":[{"id":1067661,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1067653\/revisions\/1067661"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1067659"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1067653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1067653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1067653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}