{"id":981080,"date":"2024-12-27T06:59:32","date_gmt":"2024-12-26T22:59:32","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/981080.html"},"modified":"2024-12-27T06:59:34","modified_gmt":"2024-12-26T22:59:34","slug":"python%e6%95%a3%e7%82%b9%e5%9b%be%e5%a6%82%e4%bd%95%e5%8a%a0%e6%a0%87%e7%ad%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/981080.html","title":{"rendered":"python\u6563\u70b9\u56fe\u5982\u4f55\u52a0\u6807\u7b7e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210205\/2d6b968a-86f7-41b9-bcb2-de37ec41eef1.webp\" alt=\"python\u6563\u70b9\u56fe\u5982\u4f55\u52a0\u6807\u7b7e\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u4e3a\u6563\u70b9\u56fe\u6dfb\u52a0\u6807\u7b7e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3002<strong>\u901a\u8fc7\u4f7f\u7528Matplotlib\u7684<code>annotate<\/code>\u51fd\u6570\u3001\u5faa\u73af\u904d\u5386\u6570\u636e\u70b9\u3001\u8bbe\u7f6e\u6807\u7b7e\u4f4d\u7f6e<\/strong>\u7b49\u65b9\u5f0f\u5b9e\u73b0\u5bf9\u6bcf\u4e2a\u6570\u636e\u70b9\u8fdb\u884c\u6807\u8bb0\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5728Python\u4e2d\u4e3a\u6563\u70b9\u56fe\u6dfb\u52a0\u6807\u7b7e\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6563\u70b9\u56fe\u4e4b\u524d\uff0c\u786e\u4fdd\u5b89\u88c5Matplotlib\u5e93\u3002\u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\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\u5165Matplotlib\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u57fa\u672c\u6563\u70b9\u56fe<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u6563\u70b9\u56fe\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u51c6\u5907\u6570\u636e\u3002\u901a\u5e38\uff0c\u6570\u636e\u4ee5\u4e24\u4e2a\u5217\u8868\u6216\u6570\u7ec4\u5f62\u5f0f\u5b58\u5728\uff0c\u5206\u522b\u4ee3\u8868X\u8f74\u548cY\u8f74\u7684\u5750\u6807\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u6570\u636e<\/p>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [10, 15, 13, 20, 18]<\/p>\n<h2><strong>\u521b\u5efa\u57fa\u672c\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>plt.scatter(x, y)<\/p>\n<p>plt.xlabel(&#39;X\u8f74\u6807\u7b7e&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74\u6807\u7b7e&#39;)<\/p>\n<p>plt.title(&#39;\u6563\u70b9\u56fe\u793a\u4f8b&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4e3a\u6563\u70b9\u56fe\u6dfb\u52a0\u6807\u7b7e<\/h3>\n<\/p>\n<p><p>\u8981\u4e3a\u6bcf\u4e2a\u6570\u636e\u70b9\u6dfb\u52a0\u6807\u7b7e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>plt.annotate<\/code>\u51fd\u6570\u3002<strong>\u901a\u8fc7\u5faa\u73af\u904d\u5386\u6570\u636e\u70b9\u3001\u4f7f\u7528<code>annotate<\/code>\u51fd\u6570\u6807\u8bb0\u6bcf\u4e2a\u70b9<\/strong>\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e3a\u6563\u70b9\u56fe\u6dfb\u52a0\u6807\u7b7e\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u6570\u636e<\/p>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [10, 15, 13, 20, 18]<\/p>\n<h2><strong>\u521b\u5efa\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>plt.scatter(x, y)<\/p>\n<p>plt.xlabel(&#39;X\u8f74\u6807\u7b7e&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74\u6807\u7b7e&#39;)<\/p>\n<p>plt.title(&#39;\u5e26\u6807\u7b7e\u7684\u6563\u70b9\u56fe\u793a\u4f8b&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u7b7e<\/strong><\/h2>\n<p>for i, (xi, yi) in enumerate(zip(x, y)):<\/p>\n<p>    plt.annotate(f&#39;({xi},{yi})&#39;, (xi, yi), textcoords=&quot;offset points&quot;, xytext=(5,-5), ha=&#39;center&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>enumerate<\/code>\u7528\u4e8e\u83b7\u53d6\u6bcf\u4e2a\u6570\u636e\u70b9\u7684\u7d22\u5f15\u548c\u503c\uff0c<code>plt.annotate<\/code>\u7528\u4e8e\u5728\u6bcf\u4e2a\u6570\u636e\u70b9\u65c1\u8fb9\u6dfb\u52a0\u6587\u672c\u6807\u7b7e\u3002<code>textcoords<\/code>\u53c2\u6570\u6307\u5b9a\u6587\u672c\u7684\u5750\u6807\u7cfb\u7edf\uff0c<code>xytext<\/code>\u53c2\u6570\u6307\u5b9a\u6587\u672c\u76f8\u5bf9\u4e8e\u6570\u636e\u70b9\u7684\u504f\u79fb\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8c03\u6574\u6807\u7b7e\u6837\u5f0f\u548c\u4f4d\u7f6e<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u6839\u636e\u6570\u636e\u7684\u590d\u6742\u6027\u548c\u53ef\u8bfb\u6027\u8981\u6c42\u6765\u8c03\u6574\u6807\u7b7e\u6837\u5f0f\u548c\u4f4d\u7f6e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u8c03\u6574\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u8c03\u6574\u6807\u7b7e\u504f\u79fb\u91cf<\/strong>\uff1a\u901a\u8fc7\u4fee\u6539<code>xytext<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u8c03\u6574\u6807\u7b7e\u76f8\u5bf9\u4e8e\u6570\u636e\u70b9\u7684\u4f4d\u7f6e\u3002<\/li>\n<li><strong>\u66f4\u6539\u5b57\u4f53\u5927\u5c0f\u548c\u989c\u8272<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u5728<code>annotate<\/code>\u51fd\u6570\u4e2d\u6dfb\u52a0<code>fontsize<\/code>\u548c<code>color<\/code>\u53c2\u6570\u6765\u8c03\u6574\u5b57\u4f53\u6837\u5f0f\u3002<\/li>\n<li><strong>\u907f\u514d\u6807\u7b7e\u91cd\u53e0<\/strong>\uff1a\u5bf9\u4e8e\u5bc6\u96c6\u7684\u6570\u636e\u70b9\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b97\u6cd5\u52a8\u6001\u8c03\u6574\u6807\u7b7e\u4f4d\u7f6e\uff0c\u6216\u4f7f\u7528\u7f29\u653e\u529f\u80fd\uff0c\u4f7f\u6807\u7b7e\u66f4\u52a0\u6e05\u6670\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u8c03\u6574\u6807\u7b7e\u6837\u5f0f\u548c\u4f4d\u7f6e<\/p>\n<p>for i, (xi, yi) in enumerate(zip(x, y)):<\/p>\n<p>    plt.annotate(f&#39;({xi},{yi})&#39;, (xi, yi), textcoords=&quot;offset points&quot;, xytext=(5,-5), ha=&#39;center&#39;,<\/p>\n<p>                 fontsize=9, color=&#39;red&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u9879\u76ee\u4e2d\uff0c\u6563\u70b9\u56fe\u7684\u6807\u7b7e\u529f\u80fd\u975e\u5e38\u91cd\u8981\u3002\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5206\u6790\u4eba\u5458\u5feb\u901f\u8bc6\u522b\u7279\u5b9a\u6570\u636e\u70b9\u7684\u8be6\u7ec6\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u5728\u5206\u6790\u5e02\u573a\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u6807\u8bb0\u51fa\u7279\u5b9a\u7684\u9500\u552e\u6570\u636e\u70b9\uff0c\u4ee5\u4fbf\u66f4\u6df1\u5165\u5730\u7814\u7a76\u8fd9\u4e9b\u6570\u636e\u70b9\u7684\u5f71\u54cd\u56e0\u7d20\u548c\u8d8b\u52bf\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u7ed3\u5408\u5176\u4ed6\u53ef\u89c6\u5316\u6280\u672f\uff08\u5982\u989c\u8272\u7f16\u7801\u3001\u5f62\u72b6\u6807\u8bb0\uff09\u53ef\u4ee5\u8fdb\u4e00\u6b65\u589e\u5f3a\u6563\u70b9\u56fe\u7684\u8868\u8fbe\u80fd\u529b\uff0c\u4f7f\u5f97\u590d\u6742\u6570\u636e\u96c6\u7684\u5206\u6790\u53d8\u5f97\u66f4\u52a0\u76f4\u89c2\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4e3aPython\u4e2d\u7684\u6563\u70b9\u56fe\u6dfb\u52a0\u6807\u7b7e\u662f\u4e00\u9879\u5b9e\u7528\u4e14\u5e38\u89c1\u7684\u4efb\u52a1\u3002<strong>\u901a\u8fc7\u4f7f\u7528Matplotlib\u5e93\u7684<code>annotate<\/code>\u529f\u80fd\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u4e3a\u6bcf\u4e2a\u6570\u636e\u70b9\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6807\u7b7e\uff0c\u589e\u5f3a\u6570\u636e\u53ef\u89c6\u5316\u7684\u53ef\u8bfb\u6027\u548c\u8868\u8fbe\u529b<\/strong>\u3002\u901a\u8fc7\u5408\u7406\u8c03\u6574\u6807\u7b7e\u7684\u6837\u5f0f\u548c\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u5f97\u6563\u70b9\u56fe\u5728\u5c55\u793a\u6570\u636e\u7279\u5f81\u65f6\u66f4\u5177\u8bf4\u670d\u529b\u548c\u7f8e\u89c2\u6027\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ed3\u5408\u5176\u4ed6\u53ef\u89c6\u5316\u6280\u672f\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u5347\u6570\u636e\u5206\u6790\u7684\u6548\u7387\u548c\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7684\u6563\u70b9\u56fe\u4e2d\u6dfb\u52a0\u6570\u636e\u70b9\u6807\u7b7e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u6563\u70b9\u56fe\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>plt.text()<\/code>\u6216<code>ax.text()<\/code>\u65b9\u6cd5\u4e3a\u6bcf\u4e2a\u6570\u636e\u70b9\u6dfb\u52a0\u6807\u7b7e\u3002\u4f60\u53ef\u4ee5\u5728\u7ed8\u5236\u6563\u70b9\u56fe\u540e\uff0c\u5faa\u73af\u904d\u5386\u6570\u636e\u70b9\u7684\u4f4d\u7f6e\uff0c\u5e76\u5728\u76f8\u5e94\u7684\u4f4d\u7f6e\u6dfb\u52a0\u6807\u7b7e\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\nx = [1, 2, 3, 4]\ny = [10, 20, 25, 30]\nlabels = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]\n\nplt.scatter(x, y)\n\nfor i, label in enumerate(labels):\n    plt.text(x[i], y[i], label)\n\nplt.show()\n<\/code><\/pre>\n<p><strong>\u5728\u6563\u70b9\u56fe\u4e2d\u5982\u4f55\u8c03\u6574\u6807\u7b7e\u7684\u5b57\u4f53\u548c\u989c\u8272\uff1f<\/strong><br \/>Matplotlib\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u8c03\u6574\u6563\u70b9\u56fe\u4e2d\u6807\u7b7e\u7684\u6837\u5f0f\u3002\u4f7f\u7528<code>plt.text()<\/code>\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>fontsize<\/code>\u3001<code>color<\/code>\u7b49\u53c2\u6570\u6765\u8bbe\u7f6e\u6807\u7b7e\u7684\u5b57\u4f53\u5927\u5c0f\u548c\u989c\u8272\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">plt.text(x[i], y[i], label, fontsize=12, color=&#39;red&#39;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u6807\u7b7e\u66f4\u52a0\u7a81\u51fa\uff0c\u4fbf\u4e8e\u89c2\u4f17\u8bc6\u522b\u3002<\/p>\n<p><strong>\u5982\u4f55\u63a7\u5236\u6563\u70b9\u56fe\u6807\u7b7e\u7684\u91cd\u53e0\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u6570\u636e\u70b9\u8f83\u5bc6\u96c6\u7684\u60c5\u51b5\u4e0b\uff0c\u6807\u7b7e\u53ef\u80fd\u4f1a\u91cd\u53e0\uff0c\u5bfc\u81f4\u96be\u4ee5\u9605\u8bfb\u3002\u53ef\u4ee5\u4f7f\u7528<code>adjustText<\/code>\u5e93\u6765\u81ea\u52a8\u8c03\u6574\u6807\u7b7e\u7684\u4f4d\u7f6e\uff0c\u907f\u514d\u91cd\u53e0\u3002\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from adjustText import adjust_text\n\ntexts = [plt.text(x[i], y[i], label) for i, label in enumerate(labels)]\nadjust_text(texts)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6bcf\u4e2a\u6807\u7b7e\u90fd\u80fd\u6e05\u6670\u53ef\u89c1\uff0c\u63d0\u5347\u56fe\u8868\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4e3a\u6563\u70b9\u56fe\u6dfb\u52a0\u6807\u7b7e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3002\u901a\u8fc7\u4f7f\u7528Matplotlib\u7684annota [&hellip;]","protected":false},"author":3,"featured_media":981083,"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\/981080"}],"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=981080"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981080\/revisions"}],"predecessor-version":[{"id":981084,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/981080\/revisions\/981084"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/981083"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=981080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=981080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=981080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}