{"id":1094304,"date":"2025-01-08T14:39:44","date_gmt":"2025-01-08T06:39:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1094304.html"},"modified":"2025-01-08T14:39:47","modified_gmt":"2025-01-08T06:39:47","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%bb%99%e5%9c%86%e7%94%bb%e5%88%bb%e5%ba%a6%e7%ba%bf-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1094304.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u7ed9\u5706\u753b\u523b\u5ea6\u7ebf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210458\/2bca648a-f354-4f73-84d9-08c2faee9654.webp\" alt=\"python\u4e2d\u5982\u4f55\u7ed9\u5706\u753b\u523b\u5ea6\u7ebf\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u7ed9\u5706\u753b\u523b\u5ea6\u7ebf\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528matplotlib\u5e93\u3001\u521b\u5efa\u4e00\u4e2a\u5706\u3001\u5728\u5706\u5468\u4e0a\u7ed8\u5236\u523b\u5ea6\u7ebf\u3002<\/strong> \u5176\u4e2d\uff0c<strong>matplotlib<\/strong>\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u76842D\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u7ed8\u5236\u5404\u79cd\u56fe\u5f62\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528matplotlib\u5728Python\u4e2d\u7ed9\u5706\u753b\u523b\u5ea6\u7ebf\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528matplotlib\u5e93<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u80fd\u591f\u751f\u6210\u5404\u79cd\u56fe\u5f62\u548c\u56fe\u8868\u3002\u6211\u4eec\u5c06\u4f7f\u7528matplotlib\u5e93\u4e2d\u7684\u4e00\u4e9b\u51fd\u6570\u548c\u65b9\u6cd5\u6765\u7ed8\u5236\u5706\u548c\u523b\u5ea6\u7ebf\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5matplotlib\u5e93\uff0c\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\u6211\u4eec\u53ef\u4ee5\u5bfc\u5165matplotlib\u5e93\uff0c\u5e76\u5f00\u59cb\u7ed8\u5236\u5706\u548c\u523b\u5ea6\u7ebf\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><p>\u4e8c\u3001\u521b\u5efa\u4e00\u4e2a\u5706<\/p>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u5706\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u5706\u5fc3\u548c\u534a\u5f84\u3002\u53ef\u4ee5\u4f7f\u7528numpy\u5e93\u6765\u751f\u6210\u5706\u5468\u4e0a\u7684\u70b9\uff0c\u7136\u540e\u4f7f\u7528matplotlib\u5e93\u6765\u7ed8\u5236\u5706\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5706\u5fc3\u5750\u6807<\/p>\n<p>center_x = 0<\/p>\n<p>center_y = 0<\/p>\n<h2><strong>\u5706\u7684\u534a\u5f84<\/strong><\/h2>\n<p>radius = 5<\/p>\n<h2><strong>\u751f\u6210\u5706\u5468\u4e0a\u7684\u70b9<\/strong><\/h2>\n<p>theta = np.linspace(0, 2*np.pi, 100)<\/p>\n<p>x = center_x + radius * np.cos(theta)<\/p>\n<p>y = center_y + radius * np.sin(theta)<\/p>\n<h2><strong>\u7ed8\u5236\u5706<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5728\u5706\u5468\u4e0a\u7ed8\u5236\u523b\u5ea6\u7ebf<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728\u5706\u5468\u4e0a\u7ed8\u5236\u523b\u5ea6\u7ebf\uff0c\u6211\u4eec\u9700\u8981\u8ba1\u7b97\u6bcf\u4e2a\u523b\u5ea6\u7ebf\u7684\u8d77\u70b9\u548c\u7ec8\u70b9\u5750\u6807\u3002\u53ef\u4ee5\u4f7f\u7528numpy\u5e93\u4e2d\u7684\u4e09\u89d2\u51fd\u6570\u6765\u8ba1\u7b97\u8fd9\u4e9b\u5750\u6807\u3002\u4ee5\u4e0b\u662f\u7ed8\u5236\u523b\u5ea6\u7ebf\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u523b\u5ea6\u7ebf\u7684\u6570\u91cf<\/p>\n<p>num_ticks = 12<\/p>\n<h2><strong>\u523b\u5ea6\u7ebf\u7684\u957f\u5ea6<\/strong><\/h2>\n<p>tick_length = 0.5<\/p>\n<h2><strong>\u8ba1\u7b97\u523b\u5ea6\u7ebf\u7684\u8d77\u70b9\u548c\u7ec8\u70b9<\/strong><\/h2>\n<p>for i in range(num_ticks):<\/p>\n<p>    angle = i * 2 * np.pi \/ num_ticks<\/p>\n<p>    x_start = center_x + (radius - tick_length) * np.cos(angle)<\/p>\n<p>    y_start = center_y + (radius - tick_length) * np.sin(angle)<\/p>\n<p>    x_end = center_x + radius * np.cos(angle)<\/p>\n<p>    y_end = center_y + radius * np.sin(angle)<\/p>\n<p>    plt.plot([x_start, x_end], [y_start, y_end], &#39;k&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.gca().set_aspect(&#39;equal&#39;, adjustable=&#39;box&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u523b\u5ea6\u7ebf\u7684\u6570\u91cf\u548c\u957f\u5ea6\u3002\u7136\u540e\uff0c\u901a\u8fc7\u4e00\u4e2a\u5faa\u73af\u6765\u8ba1\u7b97\u6bcf\u4e2a\u523b\u5ea6\u7ebf\u7684\u8d77\u70b9\u548c\u7ec8\u70b9\u5750\u6807\uff0c\u5e76\u4f7f\u7528plt.plot()\u51fd\u6570\u7ed8\u5236\u523b\u5ea6\u7ebf\u3002\u6700\u540e\uff0c\u4f7f\u7528plt.show()\u51fd\u6570\u663e\u793a\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u6dfb\u52a0\u523b\u5ea6\u7ebf\u6807\u7b7e<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u523b\u5ea6\u7ebf\u66f4\u6709\u610f\u4e49\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u523b\u5ea6\u7ebf\u65c1\u8fb9\u6dfb\u52a0\u6807\u7b7e\u3002\u53ef\u4ee5\u4f7f\u7528plt.text()\u51fd\u6570\u6765\u6dfb\u52a0\u6807\u7b7e\u3002\u4ee5\u4e0b\u662f\u6dfb\u52a0\u523b\u5ea6\u7ebf\u6807\u7b7e\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u523b\u5ea6\u7ebf\u6807\u7b7e<\/p>\n<p>tick_labels = [str(i) for i in range(1, num_ticks + 1)]<\/p>\n<h2><strong>\u6dfb\u52a0\u523b\u5ea6\u7ebf\u6807\u7b7e<\/strong><\/h2>\n<p>for i in range(num_ticks):<\/p>\n<p>    angle = i * 2 * np.pi \/ num_ticks<\/p>\n<p>    x_label = center_x + (radius + tick_length) * np.cos(angle)<\/p>\n<p>    y_label = center_y + (radius + tick_length) * np.sin(angle)<\/p>\n<p>    plt.text(x_label, y_label, tick_labels[i], ha=&#39;center&#39;, va=&#39;center&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.gca().set_aspect(&#39;equal&#39;, adjustable=&#39;box&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u523b\u5ea6\u7ebf\u6807\u7b7e\uff0c\u7136\u540e\u901a\u8fc7\u4e00\u4e2a\u5faa\u73af\u6765\u8ba1\u7b97\u6bcf\u4e2a\u6807\u7b7e\u7684\u5750\u6807\uff0c\u5e76\u4f7f\u7528plt.text()\u51fd\u6570\u6dfb\u52a0\u6807\u7b7e\u3002\u6700\u540e\uff0c\u4f7f\u7528plt.show()\u51fd\u6570\u663e\u793a\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u8c03\u6574\u56fe\u5f62\u6837\u5f0f<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u56fe\u5f62\u66f4\u7f8e\u89c2\uff0c\u53ef\u4ee5\u5bf9\u56fe\u5f62\u8fdb\u884c\u4e00\u4e9b\u6837\u5f0f\u8c03\u6574\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eplt.plot()\u51fd\u6570\u548cplt.text()\u51fd\u6570\u7684\u53c2\u6570\u6765\u8c03\u6574\u56fe\u5f62\u7684\u6837\u5f0f\u3002\u4ee5\u4e0b\u662f\u8c03\u6574\u56fe\u5f62\u6837\u5f0f\u7684\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8c03\u6574\u523b\u5ea6\u7ebf\u7684\u989c\u8272\u548c\u5bbd\u5ea6<\/p>\n<p>for i in range(num_ticks):<\/p>\n<p>    angle = i * 2 * np.pi \/ num_ticks<\/p>\n<p>    x_start = center_x + (radius - tick_length) * np.cos(angle)<\/p>\n<p>    y_start = center_y + (radius - tick_length) * np.sin(angle)<\/p>\n<p>    x_end = center_x + radius * np.cos(angle)<\/p>\n<p>    y_end = center_y + radius * np.sin(angle)<\/p>\n<p>    plt.plot([x_start, x_end], [y_start, y_end], color=&#39;blue&#39;, linewidth=2)<\/p>\n<h2><strong>\u8c03\u6574\u523b\u5ea6\u7ebf\u6807\u7b7e\u7684\u989c\u8272\u548c\u5b57\u4f53\u5927\u5c0f<\/strong><\/h2>\n<p>for i in range(num_ticks):<\/p>\n<p>    angle = i * 2 * np.pi \/ num_ticks<\/p>\n<p>    x_label = center_x + (radius + tick_length) * np.cos(angle)<\/p>\n<p>    y_label = center_y + (radius + tick_length) * np.sin(angle)<\/p>\n<p>    plt.text(x_label, y_label, tick_labels[i], ha=&#39;center&#39;, va=&#39;center&#39;, color=&#39;red&#39;, fontsize=12)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.gca().set_aspect(&#39;equal&#39;, adjustable=&#39;box&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8bbe\u7f6eplt.plot()\u51fd\u6570\u7684color\u548clinewidth\u53c2\u6570\u6765\u8c03\u6574\u523b\u5ea6\u7ebf\u7684\u989c\u8272\u548c\u5bbd\u5ea6\uff0c\u901a\u8fc7\u8bbe\u7f6eplt.text()\u51fd\u6570\u7684color\u548cfontsize\u53c2\u6570\u6765\u8c03\u6574\u523b\u5ea6\u7ebf\u6807\u7b7e\u7684\u989c\u8272\u548c\u5b57\u4f53\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u4e2a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684matplotlib\u5e93\u5728\u5706\u5468\u4e0a\u7ed8\u5236\u523b\u5ea6\u7ebf\u3002\u9996\u5148\uff0c\u4f7f\u7528matplotlib\u5e93\u521b\u5efa\u4e00\u4e2a\u5706\uff0c\u7136\u540e\u5728\u5706\u5468\u4e0a\u7ed8\u5236\u523b\u5ea6\u7ebf\uff0c\u5e76\u6dfb\u52a0\u523b\u5ea6\u7ebf\u6807\u7b7e\u3002\u6700\u540e\uff0c\u901a\u8fc7\u8c03\u6574\u56fe\u5f62\u6837\u5f0f\u4f7f\u56fe\u5f62\u66f4\u52a0\u7f8e\u89c2\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u5e26\u523b\u5ea6\u7ebf\u7684\u5706\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u7ed8\u5236\u5e26\u523b\u5ea6\u7ebf\u7684\u5706\u3002\u4f7f\u7528<code>plt.Circle<\/code>\u51fd\u6570\u7ed8\u5236\u5706\u5f62\uff0c\u7136\u540e\u4f7f\u7528<code>plt.text<\/code>\u6216<code>plt.annotate<\/code>\u51fd\u6570\u6dfb\u52a0\u523b\u5ea6\u7ebf\u548c\u6807\u7b7e\u3002\u901a\u8fc7\u8bbe\u7f6e\u5750\u6807\u548c\u89d2\u5ea6\uff0c\u60a8\u53ef\u4ee5\u7cbe\u786e\u63a7\u5236\u523b\u5ea6\u7ebf\u7684\u4f4d\u7f6e\u548c\u65b9\u5411\u3002<\/p>\n<p><strong>\u523b\u5ea6\u7ebf\u7684\u95f4\u9694\u5982\u4f55\u8bbe\u7f6e\uff1f<\/strong><br \/>\u523b\u5ea6\u7ebf\u7684\u95f4\u9694\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u523b\u5ea6\u7684\u6570\u91cf\u548c\u8d77\u59cb\u89d2\u5ea6\u6765\u5b9e\u73b0\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u6765\u8ba1\u7b97\u6bcf\u4e2a\u523b\u5ea6\u7ebf\u7684\u89d2\u5ea6\uff0c\u5e76\u5728\u5faa\u73af\u4e2d\u4f7f\u7528\u4e09\u89d2\u51fd\u6570\uff08\u5982<code>sin<\/code>\u548c<code>cos<\/code>\uff09\u6765\u786e\u5b9a\u6bcf\u4e2a\u523b\u5ea6\u7ebf\u7684\u5750\u6807\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u8c03\u6574\u523b\u5ea6\u7ebf\u7684\u95f4\u9694\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u81ea\u5b9a\u4e49\u523b\u5ea6\u7ebf\u7684\u6837\u5f0f\u548c\u989c\u8272\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u5728Matplotlib\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539<code>plt.plot<\/code>\u51fd\u6570\u7684\u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u523b\u5ea6\u7ebf\u7684\u6837\u5f0f\u548c\u989c\u8272\u3002\u60a8\u53ef\u4ee5\u8bbe\u7f6e\u7ebf\u6761\u7684\u5bbd\u5ea6\u3001\u989c\u8272\u3001\u6837\u5f0f\uff08\u5982\u865a\u7ebf\u6216\u5b9e\u7ebf\uff09\u7b49\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7<code>plt.scatter<\/code>\u51fd\u6570\u6dfb\u52a0\u4e0d\u540c\u6837\u5f0f\u7684\u70b9\uff0c\u4ee5\u4f7f\u523b\u5ea6\u7ebf\u66f4\u52a0\u7f8e\u89c2\u548c\u4e2a\u6027\u5316\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u7ed9\u5706\u753b\u523b\u5ea6\u7ebf\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528matplotlib\u5e93\u3001\u521b\u5efa\u4e00\u4e2a\u5706\u3001\u5728\u5706\u5468\u4e0a\u7ed8\u5236\u523b\u5ea6\u7ebf\u3002 \u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1094311,"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\/1094304"}],"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=1094304"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094304\/revisions"}],"predecessor-version":[{"id":1094313,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1094304\/revisions\/1094313"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1094311"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1094304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1094304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1094304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}