{"id":1146825,"date":"2025-01-08T23:20:09","date_gmt":"2025-01-08T15:20:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1146825.html"},"modified":"2025-01-08T23:20:12","modified_gmt":"2025-01-08T15:20:12","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%bb%e4%b8%a4%e7%82%b9%e9%97%b4%e7%9a%84%e5%9e%82%e7%9b%b4%e5%b9%b3%e5%88%86%e7%ba%bf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1146825.html","title":{"rendered":"\u5982\u4f55\u7528python\u753b\u4e24\u70b9\u95f4\u7684\u5782\u76f4\u5e73\u5206\u7ebf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182709\/b8e3414d-5312-4f97-9aae-589a206c55b8.webp\" alt=\"\u5982\u4f55\u7528python\u753b\u4e24\u70b9\u95f4\u7684\u5782\u76f4\u5e73\u5206\u7ebf\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u753b\u4e24\u70b9\u95f4\u7684\u5782\u76f4\u5e73\u5206\u7ebf<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u7528Python\u7ed8\u5236\u4e24\u70b9\u95f4\u7684\u5782\u76f4\u5e73\u5206\u7ebf\uff0c<strong>\u9996\u5148\u9700\u8981\u8ba1\u7b97\u4e2d\u70b9\u3001\u8ba1\u7b97\u5782\u76f4\u5e73\u5206\u7ebf\u7684\u659c\u7387\u3001\u5229\u7528\u4e2d\u70b9\u548c\u5782\u76f4\u5e73\u5206\u7ebf\u7684\u659c\u7387\u6c42\u5f97\u5782\u76f4\u5e73\u5206\u7ebf\u7684\u65b9\u7a0b<\/strong>\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u8fdb\u884c\u7ed8\u56fe\u3002<strong>\u8ba1\u7b97\u5782\u76f4\u5e73\u5206\u7ebf\u7684\u659c\u7387<\/strong>\u662f\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u7684\u5173\u952e\u6b65\u9aa4\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u5c06\u539f\u672c\u7684\u7ebf\u6bb5\u659c\u7387\u8fdb\u884c\u8d1f\u5012\u6570\u8f6c\u6362\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u8ba1\u7b97\u5782\u76f4\u5e73\u5206\u7ebf\u7684\u659c\u7387\u3002<\/p>\n<\/p>\n<p><p><strong>\u8ba1\u7b97\u5782\u76f4\u5e73\u5206\u7ebf\u7684\u659c\u7387<\/strong><\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u70b9 (A(x1, y1)) \u548c (B(x2, y2))\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u8ba1\u7b97\u8fd9\u6761\u7ebf\u6bb5\u7684\u659c\u7387 (m)\uff0c\u516c\u5f0f\u4e3a\uff1a<\/p>\n<\/p>\n<p><p>[ m = \\frac{y2 &#8211; y1}{x2 &#8211; x1} ]<\/p>\n<\/p>\n<p><p>\u63a5\u7740\uff0c\u5782\u76f4\u5e73\u5206\u7ebf\u7684\u659c\u7387 (m&#39;) \u662f (m) \u7684\u8d1f\u5012\u6570\uff1a<\/p>\n<\/p>\n<p><p>[ m&#39; = -\\frac{1}{m} ]<\/p>\n<\/p>\n<p><p>\u5982\u679c\u7ebf\u6bb5\u7684\u659c\u7387 (m) \u4e3a\u96f6\uff08\u5373\u7ebf\u6bb5\u662f\u6c34\u5e73\u7684\uff09\uff0c\u90a3\u4e48\u5782\u76f4\u5e73\u5206\u7ebf\u5c06\u4f1a\u662f\u5782\u76f4\u7684\uff0c\u5176\u659c\u7387\u5c06\u4f1a\u662f\u65e0\u7a77\u5927\u3002\u8fd9\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u7ed8\u5236\u4e00\u6761\u5782\u76f4\u7ebf\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u8ba9\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u8fd9\u4e00\u8fc7\u7a0b\u7684\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u4e0e\u5bfc\u5165\u5fc5\u8981\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\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>\u7136\u540e\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\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<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8ba1\u7b97\u4e2d\u70b9\u548c\u5782\u76f4\u5e73\u5206\u7ebf\u65b9\u7a0b<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u9700\u8981\u8ba1\u7b97\u7ebf\u6bb5\u7684\u4e2d\u70b9\u548c\u5782\u76f4\u5e73\u5206\u7ebf\u7684\u65b9\u7a0b\u3002\u4e2d\u70b9\u7684\u5750\u6807 (M) \u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u516c\u5f0f\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><p>[ M_x = \\frac{x1 + x2}{2} ]<\/p>\n<p>[ M_y = \\frac{y1 + y2}{2} ]<\/p>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u6211\u4eec\u8ba1\u7b97\u5782\u76f4\u5e73\u5206\u7ebf\u7684\u659c\u7387\uff0c\u5e76\u5229\u7528\u70b9\u659c\u5f0f\u65b9\u7a0b\u5f97\u5230\u5782\u76f4\u5e73\u5206\u7ebf\u7684\u65b9\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed8\u5236\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u8fdb\u884c\u7ed8\u56fe\u3002\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u5b9e\u73b0\u4ee3\u7801\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<p>def plot_perpendicular_bisector(x1, y1, x2, y2):<\/p>\n<p>    # \u8ba1\u7b97\u4e2d\u70b9<\/p>\n<p>    mid_x = (x1 + x2) \/ 2<\/p>\n<p>    mid_y = (y1 + y2) \/ 2<\/p>\n<p>    # \u8ba1\u7b97\u7ebf\u6bb5\u7684\u659c\u7387<\/p>\n<p>    if x1 == x2:<\/p>\n<p>        # \u7ebf\u6bb5\u4e3a\u5782\u76f4\u7ebf\uff0c\u5782\u76f4\u5e73\u5206\u7ebf\u4e3a\u6c34\u5e73\u7ebf<\/p>\n<p>        perp_slope = 0<\/p>\n<p>    elif y1 == y2:<\/p>\n<p>        # \u7ebf\u6bb5\u4e3a\u6c34\u5e73\u7ebf\uff0c\u5782\u76f4\u5e73\u5206\u7ebf\u4e3a\u5782\u76f4\u7ebf<\/p>\n<p>        perp_slope = np.inf<\/p>\n<p>    else:<\/p>\n<p>        segment_slope = (y2 - y1) \/ (x2 - x1)<\/p>\n<p>        perp_slope = -1 \/ segment_slope<\/p>\n<p>    # \u521b\u5efa\u4e00\u4e2a\u56fe\u5f62<\/p>\n<p>    plt.figure()<\/p>\n<p>    # \u7ed8\u5236\u539f\u59cb\u7ebf\u6bb5<\/p>\n<p>    plt.plot([x1, x2], [y1, y2], &#39;ro-&#39;)<\/p>\n<p>    # \u7ed8\u5236\u5782\u76f4\u5e73\u5206\u7ebf<\/p>\n<p>    if perp_slope == np.inf:<\/p>\n<p>        # \u5782\u76f4\u7ebf<\/p>\n<p>        plt.axvline(x=mid_x, color=&#39;b&#39;, linestyle=&#39;--&#39;)<\/p>\n<p>    else:<\/p>\n<p>        x_range = np.linspace(min(x1, x2) - 1, max(x1, x2) + 1, 400)<\/p>\n<p>        y_range = perp_slope * (x_range - mid_x) + mid_y<\/p>\n<p>        plt.plot(x_range, y_range, &#39;b--&#39;)<\/p>\n<p>    # \u8bbe\u7f6e\u56fe\u5f62\u7684\u5c5e\u6027<\/p>\n<p>    plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>    plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>    plt.title(&#39;Perpendicular Bisector&#39;)<\/p>\n<p>    plt.grid(True)<\/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.show()<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>plot_perpendicular_bisector(2, 3, 8, 7)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8be6\u7ec6\u8bf4\u660e\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<\/p>\n<p><h4>1\u3001\u8ba1\u7b97\u4e2d\u70b9<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u8ba1\u7b97\u4e2d\u70b9\u7684\u5750\u6807\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">mid_x = (x1 + x2) \/ 2<\/p>\n<p>mid_y = (y1 + y2) \/ 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8ba1\u7b97\u5782\u76f4\u5e73\u5206\u7ebf\u7684\u659c\u7387<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8ba1\u7b97\u7ebf\u6bb5\u7684\u659c\u7387\uff0c\u5e76\u8fdb\u800c\u8ba1\u7b97\u5782\u76f4\u5e73\u5206\u7ebf\u7684\u659c\u7387\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">if x1 == x2:<\/p>\n<p>    perp_slope = 0<\/p>\n<p>elif y1 == y2:<\/p>\n<p>    perp_slope = np.inf<\/p>\n<p>else:<\/p>\n<p>    segment_slope = (y2 - y1) \/ (x1 - x2)<\/p>\n<p>    perp_slope = -1 \/ segment_slope<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u7ed8\u5236\u539f\u59cb\u7ebf\u6bb5\u548c\u5782\u76f4\u5e73\u5206\u7ebf<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u4f7f\u7528Matplotlib\u7ed8\u5236\u539f\u59cb\u7ebf\u6bb5\u548c\u5782\u76f4\u5e73\u5206\u7ebf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ed8\u5236\u539f\u59cb\u7ebf\u6bb5<\/p>\n<p>plt.plot([x1, x2], [y1, y2], &#39;ro-&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u5782\u76f4\u5e73\u5206\u7ebf<\/strong><\/h2>\n<p>if perp_slope == np.inf:<\/p>\n<p>    plt.axvline(x=mid_x, color=&#39;b&#39;, linestyle=&#39;--&#39;)<\/p>\n<p>else:<\/p>\n<p>    x_range = np.linspace(min(x1, x2) - 1, max(x1, x2) + 1, 400)<\/p>\n<p>    y_range = perp_slope * (x_range - mid_x) + mid_y<\/p>\n<p>    plt.plot(x_range, y_range, &#39;b--&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u8bbe\u7f6e\u56fe\u5f62\u5c5e\u6027<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u8bbe\u7f6e\u56fe\u5f62\u7684\u5c5e\u6027\u5e76\u5c55\u793a\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.title(&#39;Perpendicular Bisector&#39;)<\/p>\n<p>plt.grid(True)<\/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.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u4e0e\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u7b80\u5355\u7684\u4e24\u70b9\u95f4\u5782\u76f4\u5e73\u5206\u7ebf\u7684\u7ed8\u5236\u3002\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u6269\u5c55\u5230\u66f4\u590d\u6742\u7684\u51e0\u4f55\u8ba1\u7b97\u548c\u7ed8\u56fe\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6dfb\u52a0\u66f4\u591a\u7684\u51e0\u4f55\u5f62\u72b6\uff0c\u8ba1\u7b97\u66f4\u591a\u7684\u51e0\u4f55\u6027\u8d28\uff0c\u751a\u81f3\u53ef\u4ee5\u5c06\u5176\u4e0e\u5176\u4ed6\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5de5\u5177\u7ed3\u5408\u8d77\u6765\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u8ba1\u7b97\u5e76\u7ed8\u5236\u4e24\u70b9\u95f4\u7684\u5782\u76f4\u5e73\u5206\u7ebf\u3002\u901a\u8fc7\u8ba1\u7b97\u4e2d\u70b9\u548c\u5782\u76f4\u5e73\u5206\u7ebf\u7684\u659c\u7387\uff0c\u6211\u4eec\u80fd\u591f\u51c6\u786e\u5730\u7ed8\u5236\u51fa\u5782\u76f4\u5e73\u5206\u7ebf\u3002\u672c\u6587\u8fd8\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Matplotlib\u8fdb\u884c\u7ed8\u56fe\uff0c\u5e0c\u671b\u80fd\u4e3a\u6709\u7c7b\u4f3c\u9700\u6c42\u7684\u8bfb\u8005\u63d0\u4f9b\u5e2e\u52a9\u548c\u53c2\u8003\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u5b9e\u73b0\u4e24\u70b9\u95f4\u7684\u5782\u76f4\u5e73\u5206\u7ebf\u7ed8\u5236\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684matplotlib\u5e93\u6765\u7ed8\u5236\u4e24\u70b9\u95f4\u7684\u5782\u76f4\u5e73\u5206\u7ebf\u3002\u9996\u5148\uff0c\u786e\u5b9a\u4e24\u70b9\u7684\u5750\u6807\uff0c\u7136\u540e\u8ba1\u7b97\u5b83\u4eec\u7684\u4e2d\u70b9\u548c\u659c\u7387\uff0c\u4ece\u800c\u627e\u5230\u5782\u76f4\u5e73\u5206\u7ebf\u7684\u65b9\u7a0b\u3002\u63a5\u7740\uff0c\u5229\u7528matplotlib\u7ed8\u5236\u8fd9\u6761\u7ebf\u3002\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\u53ef\u4ee5\u53c2\u8003\u76f8\u5173\u7684\u6559\u7a0b\u6216\u6587\u6863\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u7ed8\u5236\u5782\u76f4\u5e73\u5206\u7ebf\u9700\u8981\u54ea\u4e9b\u5e93\uff1f<\/strong><br \/>\u8981\u7ed8\u5236\u5782\u76f4\u5e73\u5206\u7ebf\uff0c\u63a8\u8350\u4f7f\u7528matplotlib\u5e93\u8fdb\u884c\u53ef\u89c6\u5316\uff0c\u540c\u65f6numpy\u5e93\u53ef\u4ee5\u5e2e\u52a9\u8fdb\u884c\u6570\u5b66\u8ba1\u7b97\u3002\u786e\u4fdd\u5728\u73af\u5883\u4e2d\u5b89\u88c5\u8fd9\u4e24\u4e2a\u5e93\uff0c\u4ee5\u4fbf\u987a\u5229\u8fd0\u884c\u4ee3\u7801\u3002<\/p>\n<p><strong>\u5982\u4f55\u8ba1\u7b97\u4e24\u70b9\u7684\u4e2d\u70b9\u548c\u659c\u7387\uff1f<\/strong><br \/>\u4e2d\u70b9\u7684\u8ba1\u7b97\u65b9\u6cd5\u662f\u5c06\u4e24\u4e2a\u70b9\u7684x\u5750\u6807\u548cy\u5750\u6807\u5206\u522b\u76f8\u52a0\u5e76\u9664\u4ee52\u3002\u659c\u7387\u5219\u53ef\u4ee5\u901a\u8fc7\u4e24\u4e2a\u70b9\u7684y\u5750\u6807\u5dee\u9664\u4ee5x\u5750\u6807\u5dee\u6765\u83b7\u5f97\u3002\u5bf9\u4e8e\u5782\u76f4\u5e73\u5206\u7ebf\u800c\u8a00\uff0c\u659c\u7387\u7684\u8d1f\u5012\u6570\u5c06\u7528\u4e8e\u8ba1\u7b97\u8be5\u7ebf\u7684\u65b9\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u753b\u4e24\u70b9\u95f4\u7684\u5782\u76f4\u5e73\u5206\u7ebf \u8981\u7528Python\u7ed8\u5236\u4e24\u70b9\u95f4\u7684\u5782\u76f4\u5e73\u5206\u7ebf\uff0c\u9996\u5148\u9700\u8981\u8ba1\u7b97\u4e2d\u70b9\u3001\u8ba1\u7b97\u5782\u76f4\u5e73\u5206 [&hellip;]","protected":false},"author":3,"featured_media":1146832,"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\/1146825"}],"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=1146825"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146825\/revisions"}],"predecessor-version":[{"id":1146833,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146825\/revisions\/1146833"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1146832"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1146825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1146825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1146825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}