{"id":1044214,"date":"2024-12-31T13:12:40","date_gmt":"2024-12-31T05:12:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1044214.html"},"modified":"2024-12-31T13:12:42","modified_gmt":"2024-12-31T05:12:42","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%bb%e6%b0%b8%e6%81%92%e5%86%99%e8%bd%ae%e7%9c%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1044214.html","title":{"rendered":"\u5982\u4f55\u7528python\u753b\u6c38\u6052\u5199\u8f6e\u773c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/75b59862-9080-4313-9077-867fceb0e7c9.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u753b\u6c38\u6052\u5199\u8f6e\u773c\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528python\u753b\u6c38\u6052\u5199\u8f6e\u773c<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Python\u753b\u6c38\u6052\u5199\u8f6e\u773c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\u5b9e\u73b0\uff1a\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u8bbe\u7f6e\u753b\u5e03\u3001\u7ed8\u5236\u57fa\u7840\u5706\u5f62\u7ed3\u6784\u3001\u6dfb\u52a0\u7ec6\u8282\u5143\u7d20\u3002<\/strong>\u5176\u4e2d\uff0c\u7ed8\u5236\u57fa\u7840\u5706\u5f62\u7ed3\u6784\u662f\u6700\u4e3a\u91cd\u8981\u7684\u4e00\u6b65\uff0c\u56e0\u4e3a\u6c38\u6052\u5199\u8f6e\u773c\u7684\u6838\u5fc3\u56fe\u6848\u90fd\u662f\u57fa\u4e8e\u5706\u5f62\u7ed3\u6784\u5c55\u5f00\u7684\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u7ed8\u5236\u4e00\u4e2a\u6c38\u6052\u5199\u8f6e\u773c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684\u7ed8\u56fe\u5e93\u5982Matplotlib\u548cNumpy\u6765\u5b9e\u73b0\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u521b\u5efa\u590d\u6742\u7684\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3002Matplotlib\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u521b\u5efa2D\u56fe\u5f62\uff0c\u800cNumpy\u5219\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5904\u7406\u6570\u7ec4\u548c\u6570\u5b66\u51fd\u6570\u3002<\/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\u8bbe\u7f6e\u753b\u5e03<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7ed8\u5236\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u7f6e\u753b\u5e03\u7684\u5927\u5c0f\u548c\u80cc\u666f\u989c\u8272\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684<code>figure<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u753b\u5e03\uff0c\u5e76\u4f7f\u7528<code>axis<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u8f74\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, ax = plt.subplots(figsize=(6, 6))<\/p>\n<p>ax.set_xlim(-1.5, 1.5)<\/p>\n<p>ax.set_ylim(-1.5, 1.5)<\/p>\n<p>ax.set_aspect(&#39;equal&#39;)<\/p>\n<p>ax.axis(&#39;off&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed8\u5236\u57fa\u7840\u5706\u5f62\u7ed3\u6784<\/h3>\n<\/p>\n<p><p>\u6c38\u6052\u5199\u8f6e\u773c\u7684\u6838\u5fc3\u56fe\u6848\u662f\u7531\u591a\u4e2a\u540c\u5fc3\u5706\u548c\u6247\u5f62\u7ec4\u6210\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684<code>Circle<\/code>\u7c7b\u6765\u7ed8\u5236\u5706\u5f62\uff0c\u5e76\u4f7f\u7528<code>Arc<\/code>\u7c7b\u6765\u7ed8\u5236\u6247\u5f62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ed8\u5236\u57fa\u7840\u5706\u5f62<\/p>\n<p>circle1 = plt.Circle((0, 0), 1, color=&#39;black&#39;, fill=False, linewidth=2)<\/p>\n<p>circle2 = plt.Circle((0, 0), 0.8, color=&#39;black&#39;, fill=False, linewidth=2)<\/p>\n<p>circle3 = plt.Circle((0, 0), 0.6, color=&#39;black&#39;, fill=False, linewidth=2)<\/p>\n<p>circle4 = plt.Circle((0, 0), 0.4, color=&#39;black&#39;, fill=True)<\/p>\n<p>ax.add_artist(circle1)<\/p>\n<p>ax.add_artist(circle2)<\/p>\n<p>ax.add_artist(circle3)<\/p>\n<p>ax.add_artist(circle4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6dfb\u52a0\u7ec6\u8282\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u6c38\u6052\u5199\u8f6e\u773c\u7684\u7ec6\u8282\u5143\u7d20\u5305\u62ec\u6247\u5f62\u548c\u70b9\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684<code>Arc<\/code>\u7c7b\u6765\u7ed8\u5236\u6247\u5f62\uff0c\u5e76\u4f7f\u7528<code>scatter<\/code>\u51fd\u6570\u6765\u7ed8\u5236\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ed8\u5236\u6247\u5f62<\/p>\n<p>angles = np.linspace(0, 2 * np.pi, 6, endpoint=False)<\/p>\n<p>for angle in angles:<\/p>\n<p>    arc = plt.Arc((0, 0), 1.2, 1.2, angle=np.degrees(angle), theta1=30, theta2=150, color=&#39;black&#39;, linewidth=2)<\/p>\n<p>    ax.add_artist(arc)<\/p>\n<h2><strong>\u7ed8\u5236\u70b9<\/strong><\/h2>\n<p>points = [(0.6 * np.cos(angle), 0.6 * np.sin(angle)) for angle in angles]<\/p>\n<p>points = np.array(points)<\/p>\n<p>ax.scatter(points[:, 0], points[:, 1], color=&#39;black&#39;, s=100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u663e\u793a\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>show<\/code>\u51fd\u6570\u6765\u663e\u793a\u7ed8\u5236\u7684\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\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>fig, ax = plt.subplots(figsize=(6, 6))<\/p>\n<p>ax.set_xlim(-1.5, 1.5)<\/p>\n<p>ax.set_ylim(-1.5, 1.5)<\/p>\n<p>ax.set_aspect(&#39;equal&#39;)<\/p>\n<p>ax.axis(&#39;off&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u57fa\u7840\u5706\u5f62<\/strong><\/h2>\n<p>circle1 = plt.Circle((0, 0), 1, color=&#39;black&#39;, fill=False, linewidth=2)<\/p>\n<p>circle2 = plt.Circle((0, 0), 0.8, color=&#39;black&#39;, fill=False, linewidth=2)<\/p>\n<p>circle3 = plt.Circle((0, 0), 0.6, color=&#39;black&#39;, fill=False, linewidth=2)<\/p>\n<p>circle4 = plt.Circle((0, 0), 0.4, color=&#39;black&#39;, fill=True)<\/p>\n<p>ax.add_artist(circle1)<\/p>\n<p>ax.add_artist(circle2)<\/p>\n<p>ax.add_artist(circle3)<\/p>\n<p>ax.add_artist(circle4)<\/p>\n<h2><strong>\u7ed8\u5236\u6247\u5f62<\/strong><\/h2>\n<p>angles = np.linspace(0, 2 * np.pi, 6, endpoint=False)<\/p>\n<p>for angle in angles:<\/p>\n<p>    arc = plt.Arc((0, 0), 1.2, 1.2, angle=np.degrees(angle), theta1=30, theta2=150, color=&#39;black&#39;, linewidth=2)<\/p>\n<p>    ax.add_artist(arc)<\/p>\n<h2><strong>\u7ed8\u5236\u70b9<\/strong><\/h2>\n<p>points = [(0.6 * np.cos(angle), 0.6 * np.sin(angle)) for angle in angles]<\/p>\n<p>points = np.array(points)<\/p>\n<p>ax.scatter(points[:, 0], points[:, 1], color=&#39;black&#39;, s=100)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7ed8\u5236\u4e00\u4e2a\u6c38\u6052\u5199\u8f6e\u773c\u3002\u901a\u8fc7\u8c03\u6574\u53c2\u6570\u548c\u6dfb\u52a0\u66f4\u591a\u7ec6\u8282\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u7f8e\u5316\u56fe\u5f62\u3002\u5e0c\u671b\u8fd9\u4e9b\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236\u6c38\u6052\u5199\u8f6e\u773c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u6c38\u6052\u5199\u8f6e\u773c\u7684\u57fa\u672c\u5f62\u72b6\uff1f<\/strong><br \/>\u5728Python\u4e2d\u7ed8\u5236\u6c38\u6052\u5199\u8f6e\u773c\uff0c\u53ef\u4ee5\u4f7f\u7528\u56fe\u5f62\u5e93\u5982Matplotlib\u6216Turtle\u3002\u9996\u5148\uff0c\u786e\u5b9a\u773c\u775b\u7684\u57fa\u672c\u5f62\u72b6\u548c\u989c\u8272\u3002\u4f7f\u7528Turtle\u5e93\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u7ed8\u5236\u5706\u5f62\u548c\u692d\u5706\u5f62\u6765\u5f62\u6210\u773c\u775b\u7684\u5916\u8f6e\u5ed3\uff0c\u7136\u540e\u6dfb\u52a0\u5185\u90e8\u7ec6\u8282\uff0c\u5982\u9ed1\u8272\u7684\u77b3\u5b54\u548c\u7ea2\u8272\u7684\u8f6e\u5ed3\u3002Matplotlib\u5219\u53ef\u4ee5\u901a\u8fc7\u7ed8\u5236\u591a\u4e2a\u5706\u5f62\u548c\u8c03\u6574\u989c\u8272\u6765\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\u3002<\/p>\n<p><strong>2. \u9700\u8981\u54ea\u4e9bPython\u5e93\u6765\u7ed8\u5236\u6c38\u6052\u5199\u8f6e\u773c\uff1f<\/strong><br \/>\u7ed8\u5236\u6c38\u6052\u5199\u8f6e\u773c\u7684\u5e38\u7528\u5e93\u5305\u62ecTurtle\u548cMatplotlib\u3002Turtle\u9002\u5408\u521d\u5b66\u8005\uff0c\u901a\u8fc7\u7b80\u5355\u7684\u547d\u4ee4\u53ef\u4ee5\u8f7b\u677e\u5730\u7ed8\u5236\u56fe\u5f62\u3002\u800cMatplotlib\u66f4\u9002\u5408\u9700\u8981\u9ad8\u8d28\u91cf\u56fe\u5f62\u548c\u590d\u6742\u7ed8\u5236\u7684\u7528\u6237\u3002\u6b64\u5916\uff0c\u5982\u679c\u60f3\u8981\u66f4\u9ad8\u7ea7\u7684\u56fe\u5f62\u6548\u679c\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Pygame\u6216Pillow\u5e93\u6765\u5904\u7406\u56fe\u50cf\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u4e3a\u7ed8\u5236\u7684\u6c38\u6052\u5199\u8f6e\u773c\u6dfb\u52a0\u52a8\u753b\u6548\u679c\uff1f<\/strong><br \/>\u8981\u4e3a\u6c38\u6052\u5199\u8f6e\u773c\u6dfb\u52a0\u52a8\u753b\u6548\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528Turtle\u5e93\u7684<code>ontimer()<\/code>\u51fd\u6570\u6216\u8005Matplotlib\u7684\u52a8\u753b\u6a21\u5757\u3002\u901a\u8fc7\u8bbe\u7f6e\u5b9a\u65f6\u5668\uff0c\u53ef\u4ee5\u4f7f\u773c\u775b\u7684\u67d0\u4e9b\u90e8\u5206\uff08\u5982\u77b3\u5b54\uff09\u9010\u6e10\u53d8\u5316\uff0c\u6a21\u62df\u773c\u775b\u7684\u95ea\u70c1\u6216\u79fb\u52a8\u6548\u679c\u3002\u786e\u4fdd\u5728\u7ed8\u5236\u8fc7\u7a0b\u4e2d\u66f4\u65b0\u56fe\u5f62\u5e76\u91cd\u7ed8\uff0c\u4ee5\u5b9e\u73b0\u6d41\u7545\u7684\u52a8\u753b\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528python\u753b\u6c38\u6052\u5199\u8f6e\u773c \u4f7f\u7528Python\u753b\u6c38\u6052\u5199\u8f6e\u773c\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\u5b9e\u73b0\uff1a\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u8bbe\u7f6e\u753b\u5e03 [&hellip;]","protected":false},"author":3,"featured_media":1044224,"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\/1044214"}],"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=1044214"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1044214\/revisions"}],"predecessor-version":[{"id":1044226,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1044214\/revisions\/1044226"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1044224"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1044214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1044214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1044214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}