{"id":1004191,"date":"2024-12-27T10:24:06","date_gmt":"2024-12-27T02:24:06","guid":{"rendered":""},"modified":"2024-12-27T10:24:11","modified_gmt":"2024-12-27T02:24:11","slug":"python%e7%94%bb%e5%9b%be%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1004191.html","title":{"rendered":"python\u753b\u56fe\u5982\u4f55\u663e\u793a\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081236\/8ca5e31a-8bd1-4740-9ba7-1d82b16bb6fc.webp\" alt=\"python\u753b\u56fe\u5982\u4f55\u663e\u793a\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u753b\u56fe\u663e\u793a\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u5b9e\u73b0\u3002\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528<code>annotate<\/code>\u51fd\u6570\u3001\u5728\u67f1\u72b6\u56fe\u4e2d\u663e\u793a\u503c\u3001\u5728\u6298\u7ebf\u56fe\u6216\u6563\u70b9\u56fe\u4e0a\u663e\u793a\u503c\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728\u4e0d\u540c\u7c7b\u578b\u7684\u56fe\u4e2d\u663e\u793a\u503c\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528ANNOTATE\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>annotate<\/code>\u662fMatplotlib\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u5728\u56fe\u5f62\u4e2d\u6dfb\u52a0\u6ce8\u91ca\u3002\u901a\u8fc7\u8fd9\u4e2a\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u5728\u56fe\u4e2d\u7684\u4efb\u610f\u4f4d\u7f6e\u6dfb\u52a0\u6587\u672c\uff0c\u5305\u62ec\u6570\u636e\u70b9\u7684\u503c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u57fa\u672c\u7528\u6cd5<\/strong><br \/>\u5728\u7ed8\u5236\u56fe\u5f62\u4e4b\u540e\uff0c\u4f7f\u7528<code>annotate<\/code>\u51fd\u6570\u53ef\u4ee5\u5728\u7279\u5b9a\u4f4d\u7f6e\u6dfb\u52a0\u6587\u5b57\u3002<code>annotate<\/code>\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>annotate(text, xy, xytext, arrowprops)<\/code>\uff0c\u5176\u4e2d<code>text<\/code>\u662f\u8981\u663e\u793a\u7684\u6587\u672c\uff0c<code>xy<\/code>\u662f\u8981\u6ce8\u91ca\u7684\u70b9\u7684\u5750\u6807\uff0c<code>xytext<\/code>\u662f\u6587\u672c\u7684\u8d77\u59cb\u4f4d\u7f6e\uff08\u53ef\u9009\uff09\uff0c<code>arrowprops<\/code>\u662f\u7bad\u5934\u7684\u5c5e\u6027\uff08\u53ef\u9009\uff09\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>x = [1, 2, 3, 4]<\/p>\n<p>y = [10, 20, 25, 30]<\/p>\n<p>plt.plot(x, y, &#39;ro-&#39;)<\/p>\n<p>for i, txt in enumerate(y):<\/p>\n<p>    plt.annotate(txt, (x[i], y[i]), textcoords=&quot;offset points&quot;, xytext=(0,10), ha=&#39;center&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>annotate<\/code>\u51fd\u6570\u7528\u4e8e\u5728\u6bcf\u4e2a\u6570\u636e\u70b9\u4e0a\u65b9\u663e\u793a\u5176y\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u6587\u672c\u4f4d\u7f6e\u548c\u6837\u5f0f<\/strong><br \/><code>annotate<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u591a\u79cd\u9009\u9879\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6587\u672c\u7684\u4f4d\u7f6e\u3001\u989c\u8272\u3001\u5b57\u4f53\u7b49\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7<code>fontsize<\/code>\u53c2\u6570\u8c03\u6574\u6587\u672c\u7684\u5927\u5c0f\uff0c\u901a\u8fc7<code>color<\/code>\u53c2\u6570\u6539\u53d8\u6587\u672c\u7684\u989c\u8272\uff0c\u7b49\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.annotate(txt, (x[i], y[i]), textcoords=&quot;offset points&quot;, xytext=(0,10), ha=&#39;center&#39;, fontsize=9, color=&#39;blue&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u5728\u67f1\u72b6\u56fe\u4e2d\u663e\u793a\u503c<\/p>\n<\/p>\n<p><p>\u5728\u67f1\u72b6\u56fe\u4e2d\u663e\u793a\u6bcf\u4e2a\u67f1\u5b50\u7684\u9ad8\u5ea6\u503c\u4e5f\u662f\u4e00\u4e2a\u5e38\u89c1\u9700\u6c42\u3002\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u67f1\u5b50\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528text\u51fd\u6570<\/strong><br \/><code>text<\/code>\u51fd\u6570\u53ef\u4ee5\u5728\u6307\u5b9a\u5750\u6807\u4f4d\u7f6e\u6dfb\u52a0\u6587\u672c\u3002\u7ed8\u5236\u67f1\u72b6\u56fe\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>text<\/code>\u51fd\u6570\u5728\u6bcf\u4e2a\u67f1\u5b50\u4e0a\u65b9\u663e\u793a\u5176\u9ad8\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>x = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]<\/p>\n<p>y = [3, 7, 5, 9]<\/p>\n<p>plt.bar(x, y)<\/p>\n<p>for i, v in enumerate(y):<\/p>\n<p>    plt.text(i, v + 0.1, str(v), ha=&#39;center&#39;, va=&#39;bottom&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>text<\/code>\u51fd\u6570\u7528\u4e8e\u5728\u6bcf\u4e2a\u67f1\u5b50\u7684\u9876\u7aef\u663e\u793a\u5176\u9ad8\u5ea6\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u52a8\u8c03\u6574\u6587\u672c\u4f4d\u7f6e<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u6587\u672c\u4e0d\u91cd\u53e0\uff0c\u53ef\u4ee5\u52a8\u6001\u8c03\u6574\u6587\u672c\u7684\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u6839\u636e\u67f1\u5b50\u7684\u9ad8\u5ea6\u81ea\u52a8\u8c03\u6574\u6587\u672c\u7684\u7eb5\u5750\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.text(i, v + 0.1 if v &gt; 0 else v - 0.1, str(v), ha=&#39;center&#39;, va=&#39;bottom&#39; if v &gt; 0 else &#39;top&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5728\u6298\u7ebf\u56fe\u6216\u6563\u70b9\u56fe\u4e0a\u663e\u793a\u503c<\/p>\n<\/p>\n<p><p>\u5728\u6298\u7ebf\u56fe\u6216\u6563\u70b9\u56fe\u4e0a\u663e\u793a\u6bcf\u4e2a\u6570\u636e\u70b9\u7684\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>annotate<\/code>\u6216<code>text<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u636e\u70b9<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u6570\u636e\u70b9\uff0c\u5728\u6bcf\u4e2a\u70b9\u65c1\u8fb9\u6dfb\u52a0\u6587\u672c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>x = [1, 2, 3, 4]<\/p>\n<p>y = [10, 20, 25, 30]<\/p>\n<p>plt.scatter(x, y)<\/p>\n<p>for i, txt in enumerate(y):<\/p>\n<p>    plt.text(x[i], y[i], str(txt), fontsize=9, ha=&#39;right&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>text<\/code>\u51fd\u6570\u7528\u4e8e\u5728\u6bcf\u4e2a\u6570\u636e\u70b9\u65c1\u8fb9\u663e\u793a\u5176y\u503c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u6587\u672c\u504f\u79fb<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570<code>xytext<\/code>\u548c<code>textcoords<\/code>\u81ea\u5b9a\u4e49\u6587\u672c\u7684\u504f\u79fb\u4f4d\u7f6e\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u5c55\u793a\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.annotate(txt, (x[i], y[i]), textcoords=&quot;offset points&quot;, xytext=(5,-5), ha=&#39;center&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u5176\u4ed6\u9ad8\u7ea7\u7ed8\u56fe\u5e93\u7684\u4f7f\u7528<\/p>\n<\/p>\n<p><p>\u9664\u4e86Matplotlib\uff0cPython\u8fd8\u6709\u5176\u4ed6\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u5982Seaborn\u3001Plotly\u7b49\uff0c\u5b83\u4eec\u4e5f\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u663e\u793a\u6570\u636e\u503c\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f7f\u7528Seaborn<\/strong><br \/>Seaborn\u662f\u5efa\u7acb\u5728Matplotlib\u4e4b\u4e0a\u7684\u9ad8\u7ea7\u63a5\u53e3\uff0c\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u7684API\u3002\u4f8b\u5982\uff0c\u5728Seaborn\u4e2d\u7ed8\u5236\u67f1\u72b6\u56fe\u5e76\u663e\u793a\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>data = sns.load_dataset(&#39;tips&#39;)<\/p>\n<p>ax = sns.barplot(x=&#39;day&#39;, y=&#39;total_bill&#39;, data=data, ci=None)<\/p>\n<p>for p in ax.patches:<\/p>\n<p>    ax.annotate(format(p.get_height(), &#39;.1f&#39;), <\/p>\n<p>                (p.get_x() + p.get_width() \/ 2., p.get_height()), <\/p>\n<p>                ha=&#39;center&#39;, va=&#39;center&#39;, <\/p>\n<p>                xytext=(0, 9), <\/p>\n<p>                textcoords=&#39;offset points&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528Plotly<\/strong><br \/>Plotly\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u5e93\uff0c\u652f\u6301\u5728\u56fe\u4e2d\u663e\u793a\u6570\u636e\u70b9\u7684\u4fe1\u606f\u3002\u901a\u8fc7\u8bbe\u7f6e<code>text<\/code>\u53c2\u6570\u53ef\u4ee5\u76f4\u63a5\u5728\u56fe\u4e2d\u663e\u793a\u503c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<p>df = px.data.iris()<\/p>\n<p>fig = px.scatter(df, x=&quot;sepal_width&quot;, y=&quot;sepal_length&quot;, text=&quot;species&quot;)<\/p>\n<p>fig.update_traces(textposition=&#39;top center&#39;)<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u5728Python\u4e2d\u4e0d\u540c\u7c7b\u578b\u56fe\u5f62\u4e0a\u663e\u793a\u6570\u636e\u503c\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528Matplotlib\u7684<code>annotate<\/code>\u548c<code>text<\/code>\u51fd\u6570\u3001\u5728\u67f1\u72b6\u56fe\u548c\u6298\u7ebf\u56fe\u4e0a\u663e\u793a\u503c\u7684\u6280\u5de7\uff0c\u4ee5\u53ca\u4f7f\u7528Seaborn\u548cPlotly\u5e93\u7684\u9ad8\u7ea7\u529f\u80fd\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6570\u636e\u53ef\u89c6\u5316\u7684\u76f4\u89c2\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u5728\u56fe\u8868\u4e0a\u663e\u793a\u6570\u636e\u70b9\u7684\u5177\u4f53\u6570\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u56fe\u5f62\u5e76\u5728\u56fe\u8868\u4e0a\u663e\u793a\u6570\u636e\u70b9\u7684\u6570\u503c\u3002\u901a\u8fc7\u8c03\u7528<code>text()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u6307\u5b9a\u7684\u4f4d\u7f6e\u6dfb\u52a0\u6587\u672c\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u5728\u7ed8\u5236\u6563\u70b9\u56fe\u540e\uff0c\u904d\u5386\u6bcf\u4e2a\u6570\u636e\u70b9\uff0c\u901a\u8fc7<code>plt.text(x, y, str(value))<\/code>\u5c06\u503c\u663e\u793a\u5728\u5bf9\u5e94\u7684\u5750\u6807\u4e0a\u3002\u8fd9\u6837\uff0c\u7528\u6237\u5728\u67e5\u770b\u56fe\u8868\u65f6\uff0c\u53ef\u4ee5\u66f4\u76f4\u89c2\u5730\u4e86\u89e3\u6bcf\u4e2a\u6570\u636e\u70b9\u7684\u5177\u4f53\u6570\u503c\u3002<\/p>\n<p><strong>\u4f7f\u7528\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5728Python\u4e2d\u7ed8\u56fe\u5e76\u663e\u793a\u503c\uff1f<\/strong><br \/>Matplotlib\u662f\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u652f\u6301\u591a\u79cd\u7c7b\u578b\u7684\u56fe\u8868\uff0c\u5e76\u80fd\u65b9\u4fbf\u5730\u5728\u56fe\u4e0a\u6dfb\u52a0\u6570\u503c\u3002Seaborn\u662f\u4e00\u4e2a\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u5e93\uff0c\u63d0\u4f9b\u66f4\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\u548c\u66f4\u7b80\u5316\u7684\u63a5\u53e3\u3002Plotly\u4e5f\u662f\u4e00\u4e2a\u6d41\u884c\u7684\u5e93\uff0c\u652f\u6301\u4ea4\u4e92\u5f0f\u56fe\u8868\uff0c\u53ef\u4ee5\u5728\u7528\u6237\u60ac\u505c\u6570\u636e\u70b9\u65f6\u663e\u793a\u5bf9\u5e94\u7684\u503c\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u76f4\u89c2\u7684\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u6570\u636e\u663e\u793a\u7684\u683c\u5f0f\u548c\u6837\u5f0f\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>fmt<\/code>\u53c2\u6570\u81ea\u5b9a\u4e49\u6587\u672c\u7684\u683c\u5f0f\uff0c\u4f8b\u5982\u8bbe\u7f6e\u5c0f\u6570\u70b9\u7684\u4f4d\u6570\u3001\u5b57\u4f53\u5927\u5c0f\u548c\u989c\u8272\u7b49\u3002\u4f7f\u7528<code>plt.text()<\/code>\u65f6\uff0c\u53ef\u4ee5\u52a0\u5165\u53c2\u6570\u5982<code>fontsize=12, color=&#39;red&#39;<\/code>\u6765\u6539\u53d8\u6587\u672c\u6837\u5f0f\u3002\u5bf9\u4e8eSeaborn\u548cPlotly\u7b49\u5e93\uff0c\u4e5f\u6709\u7c7b\u4f3c\u7684\u9009\u9879\uff0c\u5141\u8bb8\u7528\u6237\u6839\u636e\u9700\u8981\u8c03\u6574\u663e\u793a\u6548\u679c\uff0c\u4ee5\u63d0\u9ad8\u56fe\u8868\u7684\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u753b\u56fe\u663e\u793a\u503c\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u5b9e\u73b0\u3002\u4e3b\u8981\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528annot [&hellip;]","protected":false},"author":3,"featured_media":1004200,"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\/1004191"}],"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=1004191"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004191\/revisions"}],"predecessor-version":[{"id":1004202,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1004191\/revisions\/1004202"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1004200"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1004191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1004191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1004191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}