{"id":1132610,"date":"2025-01-08T20:58:55","date_gmt":"2025-01-08T12:58:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1132610.html"},"modified":"2025-01-08T20:58:55","modified_gmt":"2025-01-08T12:58:55","slug":"python%e6%95%a3%e7%82%b9%e5%9b%be%e4%b8%ad%e5%a6%82%e4%bd%95%e7%bb%99%e6%af%8f%e7%a7%8d%e7%82%b9%e5%8a%a0%e4%b8%8a%e6%a0%87%e6%b3%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1132610.html","title":{"rendered":"python\u6563\u70b9\u56fe\u4e2d\u5982\u4f55\u7ed9\u6bcf\u79cd\u70b9\u52a0\u4e0a\u6807\u6ce8"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102116\/bb2a555b-fba2-45b6-8c0e-6d35b8ebf189.webp\" alt=\"python\u6563\u70b9\u56fe\u4e2d\u5982\u4f55\u7ed9\u6bcf\u79cd\u70b9\u52a0\u4e0a\u6807\u6ce8\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4e3a\u6563\u70b9\u56fe\u4e2d\u7684\u6bcf\u79cd\u70b9\u52a0\u4e0a\u6807\u6ce8\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684<code>annotate<\/code>\u51fd\u6570\u3001\u5229\u7528Seaborn\u5e93\u4e2d\u7684\u529f\u80fd\u3001\u4ee5\u53ca\u7ed3\u5408Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u6709\u6548\u5730\u4e3a\u6563\u70b9\u56fe\u4e2d\u7684\u4e0d\u540c\u70b9\u6dfb\u52a0\u6807\u6ce8\uff0c\u63d0\u5347\u6570\u636e\u53ef\u89c6\u5316\u6548\u679c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>Matplotlib<\/strong> \u7684 <code>annotate<\/code> \u529f\u80fd\u6700\u4e3a\u5e38\u7528\uff0c\u80fd\u591f\u4e3a\u6bcf\u4e00\u4e2a\u6570\u636e\u70b9\u6dfb\u52a0\u6587\u672c\u6807\u6ce8\uff0c\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/strong>\uff1a\u9996\u5148\u9700\u8981\u5bfc\u5165Matplotlib\u53ca\u5176\u4ed6\u53ef\u80fd\u9700\u8981\u7684\u5e93\uff0c\u5982NumPy\u548cPandas\u3002<\/li>\n<li><strong>\u521b\u5efa\u6570\u636e<\/strong>\uff1a\u751f\u6210\u6216\u5bfc\u5165\u4f60\u7684\u6570\u636e\u96c6\uff0c\u901a\u5e38\u6570\u636e\u4ee5Pandas DataFrame\u7684\u5f62\u5f0f\u5b58\u50a8\u3002<\/li>\n<li><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong>\uff1a\u4f7f\u7528Matplotlib\u7684 <code>scatter<\/code> \u51fd\u6570\u7ed8\u5236\u57fa\u7840\u6563\u70b9\u56fe\u3002<\/li>\n<li><strong>\u6dfb\u52a0\u6807\u6ce8<\/strong>\uff1a\u4f7f\u7528 <code>annotate<\/code> \u51fd\u6570\u4e3a\u6bcf\u4e00\u4e2a\u6570\u636e\u70b9\u6dfb\u52a0\u6807\u6ce8\uff0c\u901a\u5e38\u9700\u8981\u5faa\u73af\u904d\u5386\u6bcf\u4e00\u4e2a\u70b9\u3002<\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5bfc\u5165\u4ee5\u4e0b\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u521b\u5efa\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u751f\u6210\u6216\u5bfc\u5165\u6570\u636e\u96c6\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542bX\u3001Y\u5750\u6807\u4ee5\u53ca\u6807\u7b7e\u7684DataFrame\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = {<\/p>\n<p>    &#39;X&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>    &#39;Y&#39;: [2, 3, 5, 7, 11],<\/p>\n<p>    &#39;Label&#39;: [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u7ed8\u5236\u6563\u70b9\u56fe<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u7684 <code>scatter<\/code> \u51fd\u6570\u6765\u7ed8\u5236\u57fa\u7840\u6563\u70b9\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.scatter(df[&#39;X&#39;], df[&#39;Y&#39;])<\/p>\n<p>plt.xlabel(&#39;X axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y axis&#39;)<\/p>\n<p>plt.title(&#39;Scatter Plot with Annotations&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u6dfb\u52a0\u6807\u6ce8<\/h2>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u6563\u70b9\u56fe\u4e4b\u540e\uff0c\u4f7f\u7528 <code>annotate<\/code> \u51fd\u6570\u4e3a\u6bcf\u4e00\u4e2a\u6570\u636e\u70b9\u6dfb\u52a0\u6807\u6ce8\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(len(df)):<\/p>\n<p>    plt.annotate(df[&#39;Label&#39;][i], (df[&#39;X&#39;][i], df[&#39;Y&#39;][i]), textcoords=&quot;offset points&quot;, xytext=(0,10), ha=&#39;center&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>annotate<\/code> \u51fd\u6570\u4e2d\u7684\u53c2\u6570\u89e3\u91ca\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8981\u6dfb\u52a0\u7684\u6587\u672c\u3002<\/li>\n<li>\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u6570\u636e\u70b9\u7684\u5750\u6807\u3002<\/li>\n<li><code>textcoords=&quot;offset points&quot;<\/code> \u8868\u793a\u504f\u79fb\u91cf\u7684\u5355\u4f4d\u662f\u70b9\u3002<\/li>\n<li><code>xytext=(0,10)<\/code> \u8868\u793a\u6587\u672c\u76f8\u5bf9\u4e8e\u6570\u636e\u70b9\u7684\u504f\u79fb\u91cf\uff08\u6a2a\u54110\uff0c\u7eb5\u541110\uff09\u3002<\/li>\n<li><code>ha=&#39;center&#39;<\/code> \u8868\u793a\u6587\u672c\u5728\u6c34\u5e73\u65b9\u5411\u5c45\u4e2d\u3002<\/li>\n<\/ul>\n<p><h2>\u4e94\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h2>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4e3a\u6563\u70b9\u56fe\u4e2d\u7684\u6bcf\u4e2a\u70b9\u6dfb\u52a0\u6807\u6ce8\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;X&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>    &#39;Y&#39;: [2, 3, 5, 7, 11],<\/p>\n<p>    &#39;Label&#39;: [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>plt.scatter(df[&#39;X&#39;], df[&#39;Y&#39;])<\/p>\n<p>plt.xlabel(&#39;X axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y axis&#39;)<\/p>\n<p>plt.title(&#39;Scatter Plot with Annotations&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u6ce8<\/strong><\/h2>\n<p>for i in range(len(df)):<\/p>\n<p>    plt.annotate(df[&#39;Label&#39;][i], (df[&#39;X&#39;][i], df[&#39;Y&#39;][i]), textcoords=&quot;offset points&quot;, xytext=(0,10), ha=&#39;center&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u6269\u5c55\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528Seaborn\u5e93<\/h3>\n<\/p>\n<p><p>\u9664\u4e86Matplotlib\uff0cSeaborn\u5e93\u4e5f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u53ef\u89c6\u5316\u529f\u80fd\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u590d\u6742\u6570\u636e\u96c6\u3002Seaborn\u7684 <code>scatterplot<\/code> \u51fd\u6570\u53ef\u4ee5\u4e0eMatplotlib\u7684 <code>annotate<\/code> \u51fd\u6570\u7ed3\u5408\u4f7f\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<h2><strong>\u4f7f\u7528Seaborn\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>sns.scatterplot(x=&#39;X&#39;, y=&#39;Y&#39;, data=df)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u6ce8<\/strong><\/h2>\n<p>for i in range(len(df)):<\/p>\n<p>    plt.annotate(df[&#39;Label&#39;][i], (df[&#39;X&#39;][i], df[&#39;Y&#39;][i]), textcoords=&quot;offset points&quot;, xytext=(0,10), ha=&#39;center&#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>\u4f7f\u7528Pandas\u7684 <code>plot<\/code> \u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Pandas\u7684 <code>plot<\/code> \u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u6563\u70b9\u56fe\uff0c\u5e76\u7ed3\u5408Matplotlib\u8fdb\u884c\u6807\u6ce8\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax = df.plot(kind=&#39;scatter&#39;, x=&#39;X&#39;, y=&#39;Y&#39;, title=&#39;Scatter Plot with Annotations&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u6ce8<\/strong><\/h2>\n<p>for i in range(len(df)):<\/p>\n<p>    ax.annotate(df[&#39;Label&#39;][i], (df[&#39;X&#39;][i], df[&#39;Y&#39;][i]), textcoords=&quot;offset points&quot;, xytext=(0,10), ha=&#39;center&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e03\u3001\u4f18\u5316\u56fe\u5f62<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u56fe\u5f62\u7684\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u5ea6\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u6807\u6ce8\uff0c\u4f8b\u5982\u8c03\u6574\u5b57\u4f53\u5927\u5c0f\u3001\u989c\u8272\u548c\u6837\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for i in range(len(df)):<\/p>\n<p>    plt.annotate(<\/p>\n<p>        df[&#39;Label&#39;][i], <\/p>\n<p>        (df[&#39;X&#39;][i], df[&#39;Y&#39;][i]), <\/p>\n<p>        textcoords=&quot;offset points&quot;, <\/p>\n<p>        xytext=(0,10), <\/p>\n<p>        ha=&#39;center&#39;,<\/p>\n<p>        fontsize=12, <\/p>\n<p>        color=&#39;red&#39;, <\/p>\n<p>        fontstyle=&#39;italic&#39;<\/p>\n<p>    )<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u4e3a\u6563\u70b9\u56fe\u4e2d\u7684\u6bcf\u4e2a\u70b9\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6807\u6ce8\uff0c\u4f7f\u56fe\u5f62\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002\u65e0\u8bba\u662f\u4f7f\u7528Matplotlib\u3001Seaborn\u8fd8\u662fPandas\uff0c\u90fd\u80fd\u5b9e\u73b0\u8fd9\u4e00\u6548\u679c\uff0c\u5177\u4f53\u9009\u62e9\u53d6\u51b3\u4e8e\u4f60\u7684\u6570\u636e\u96c6\u548c\u53ef\u89c6\u5316\u9700\u6c42\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\u4e3a\u4e0d\u540c\u7c7b\u522b\u7684\u70b9\u6dfb\u52a0\u6807\u6ce8\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u53ef\u4ee5\u8f7b\u677e\u4e3a\u6563\u70b9\u56fe\u4e2d\u7684\u70b9\u6dfb\u52a0\u6807\u6ce8\u3002\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u70b9\u7684\u5750\u6807\u548c\u7c7b\u522b\uff0c\u7136\u540e\u4f7f\u7528<code>plt.text()<\/code>\u51fd\u6570\u5728\u76f8\u5e94\u7684\u4f4d\u7f6e\u6dfb\u52a0\u6807\u6ce8\u3002\u786e\u4fdd\u4f20\u5165\u7684\u5750\u6807\u4e0e\u7c7b\u522b\u4fe1\u606f\u6b63\u786e\u5339\u914d\uff0c\u4ee5\u4fdd\u8bc1\u6807\u6ce8\u7684\u51c6\u786e\u6027\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6563\u70b9\u56fe\u4e2d\u6807\u6ce8\u7684\u6837\u5f0f\u548c\u4f4d\u7f6e\uff1f<\/strong><br \/>\u662f\u7684\uff0cMatplotlib\u5141\u8bb8\u7528\u6237\u81ea\u5b9a\u4e49\u6807\u6ce8\u7684\u6837\u5f0f\u548c\u4f4d\u7f6e\u3002\u53ef\u4ee5\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u3001\u989c\u8272\u3001\u80cc\u666f\u8272\u7b49\u53c2\u6570\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u5750\u6807\u4f4d\u7f6e\uff0c\u786e\u4fdd\u6807\u6ce8\u4e0e\u70b9\u4e4b\u95f4\u6709\u5408\u9002\u7684\u95f4\u8ddd\uff0c\u589e\u52a0\u53ef\u8bfb\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6563\u70b9\u56fe\u4e2d\u91cd\u53e0\u7684\u6807\u6ce8\uff1f<\/strong><br \/>\u5904\u7406\u91cd\u53e0\u7684\u6807\u6ce8\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u4f18\u5316\u3002\u53ef\u4ee5\u4f7f\u7528\u504f\u79fb\u91cf\u8c03\u6574\u6807\u6ce8\u7684\u4f4d\u7f6e\uff0c\u6216\u5229\u7528<code>adjustText<\/code>\u5e93\u6765\u81ea\u52a8\u8c03\u6574\u6807\u6ce8\u7684\u4f4d\u7f6e\uff0c\u907f\u514d\u91cd\u53e0\u3002\u6b64\u5916\uff0c\u964d\u4f4e\u6807\u6ce8\u7684\u6570\u91cf\uff0c\u9009\u62e9\u4ec5\u6807\u8bb0\u6700\u91cd\u8981\u7684\u70b9\uff0c\u6216\u8005\u4f7f\u7528\u4ea4\u4e92\u5f0f\u56fe\u5f62\u5e93\u5982Plotly\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u89c6\u89c9\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4e3a\u6563\u70b9\u56fe\u4e2d\u7684\u6bcf\u79cd\u70b9\u52a0\u4e0a\u6807\u6ce8\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684annotate [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132610"}],"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=1132610"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132610\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1132610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1132610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1132610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}