{"id":1183783,"date":"2025-01-15T19:18:38","date_gmt":"2025-01-15T11:18:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1183783.html"},"modified":"2025-01-15T19:18:41","modified_gmt":"2025-01-15T11:18:41","slug":"python%e5%a6%82%e4%bd%95%e7%bb%99%e7%ba%bf%e5%8a%a0%e6%a0%87%e7%ad%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1183783.html","title":{"rendered":"python\u5982\u4f55\u7ed9\u7ebf\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\/25133421\/a449934a-1b57-4adb-96ca-fdf50f1aaecf.webp\" alt=\"python\u5982\u4f55\u7ed9\u7ebf\u52a0\u6807\u7b7e\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u7ed8\u56fe\u5e93\u7ed9\u7ebf\u52a0\u6807\u7b7e\u3002<strong>\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Matplotlib\u5e93<\/strong>\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u80fd\u591f\u8f7b\u677e\u7ed9\u7ebf\u6dfb\u52a0\u6807\u7b7e\u3002<strong>\u4f7f\u7528plt.plot()\u65b9\u6cd5\u7684label\u53c2\u6570\u3001\u901a\u8fc7plt.legend()\u663e\u793a\u6807\u7b7e\u3001\u4e3a\u6bcf\u6761\u7ebf\u5355\u72ec\u6dfb\u52a0\u6807\u7b7e<\/strong>\u7b49\u662f\u5e38\u89c1\u7684\u65b9\u5f0f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u5e93\u7ed9\u7ebf\u52a0\u6807\u7b7e\uff0c\u5e76\u63a2\u8ba8\u5176\u4ed6\u53ef\u80fd\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528Matplotlib\u5e93\u7ed9\u7ebf\u52a0\u6807\u7b7e<\/strong><\/h2>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u80fd\u591f\u521b\u5efa\u591a\u79cd\u7c7b\u578b\u7684\u56fe\u8868\uff0c\u5305\u62ec\u6298\u7ebf\u56fe\u3001\u6563\u70b9\u56fe\u3001\u67f1\u72b6\u56fe\u7b49\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728\u6298\u7ebf\u56fe\u4e2d\u7ed9\u7ebf\u6dfb\u52a0\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528<code>label<\/code>\u53c2\u6570<\/h2>\n<\/p>\n<p><p>Matplotlib\u7684<code>plt.plot()<\/code>\u51fd\u6570\u4e2d\u5305\u542b\u4e00\u4e2a<code>label<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u4e3a\u7ed8\u5236\u7684\u7ebf\u6dfb\u52a0\u6807\u7b7e\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>plt.legend()<\/code>\u51fd\u6570\u663e\u793a\u8fd9\u4e9b\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [2, 3, 5, 7, 11]<\/p>\n<p>y2 = [1, 4, 6, 8, 10]<\/p>\n<h2><strong>\u7ed8\u5236\u4e24\u6761\u7ebf\uff0c\u5e76\u6dfb\u52a0\u6807\u7b7e<\/strong><\/h2>\n<p>plt.plot(x, y1, label=&#39;Prime Numbers&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;Even Numbers&#39;)<\/p>\n<h2><strong>\u663e\u793a\u6807\u7b7e<\/strong><\/h2>\n<p>plt.legend()<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4e3a\u4e24\u6761\u7ebf\u5206\u522b\u6dfb\u52a0\u4e86\u6807\u7b7e\u201cPrime Numbers\u201d\u548c\u201cEven Numbers\u201d\uff0c\u5e76\u4f7f\u7528<code>plt.legend()<\/code>\u51fd\u6570\u663e\u793a\u8fd9\u4e9b\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4e3a\u6bcf\u6761\u7ebf\u5355\u72ec\u6dfb\u52a0\u6807\u7b7e<\/h2>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5728\u56fe\u8868\u4e2d\u4e3a\u6bcf\u6761\u7ebf\u5355\u72ec\u6dfb\u52a0\u6807\u7b7e\u3002\u53ef\u4ee5\u901a\u8fc7\u5728\u6bcf\u6761\u7ebf\u7684\u7279\u5b9a\u4f4d\u7f6e\u6dfb\u52a0\u6587\u672c\u6807\u7b7e\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [2, 3, 5, 7, 11]<\/p>\n<p>y2 = [1, 4, 6, 8, 10]<\/p>\n<h2><strong>\u7ed8\u5236\u4e24\u6761\u7ebf<\/strong><\/h2>\n<p>plt.plot(x, y1, label=&#39;Prime Numbers&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;Even Numbers&#39;)<\/p>\n<h2><strong>\u4e3a\u6bcf\u6761\u7ebf\u6dfb\u52a0\u6587\u672c\u6807\u7b7e<\/strong><\/h2>\n<p>for i, txt in enumerate(y1):<\/p>\n<p>    plt.annotate(txt, (x[i], y1[i]), textcoords=&quot;offset points&quot;, xytext=(0,10), ha=&#39;center&#39;)<\/p>\n<p>for i, txt in enumerate(y2):<\/p>\n<p>    plt.annotate(txt, (x[i], y2[i]), textcoords=&quot;offset points&quot;, xytext=(0,10), ha=&#39;center&#39;)<\/p>\n<h2><strong>\u663e\u793a\u6807\u7b7e<\/strong><\/h2>\n<p>plt.legend()<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>plt.annotate()<\/code>\u51fd\u6570\u4e3a\u6bcf\u6761\u7ebf\u7684\u6bcf\u4e2a\u70b9\u6dfb\u52a0\u4e86\u6587\u672c\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h2>3\u3001\u4f7f\u7528\u4e0d\u540c\u7684\u4f4d\u7f6e\u548c\u6837\u5f0f<\/h2>\n<\/p>\n<p><p>\u5728\u6709\u4e9b\u573a\u666f\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u4e3a\u7ebf\u6807\u7b7e\u8bbe\u7f6e\u4e0d\u540c\u7684\u4f4d\u7f6e\u548c\u6837\u5f0f\u3002\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574<code>plt.legend()<\/code>\u51fd\u6570\u7684\u53c2\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [2, 3, 5, 7, 11]<\/p>\n<p>y2 = [1, 4, 6, 8, 10]<\/p>\n<h2><strong>\u7ed8\u5236\u4e24\u6761\u7ebf\uff0c\u5e76\u6dfb\u52a0\u6807\u7b7e<\/strong><\/h2>\n<p>plt.plot(x, y1, label=&#39;Prime Numbers&#39;, linestyle=&#39;--&#39;, color=&#39;r&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;Even Numbers&#39;, linestyle=&#39;-.&#39;, color=&#39;b&#39;)<\/p>\n<h2><strong>\u663e\u793a\u6807\u7b7e\uff0c\u5e76\u8bbe\u7f6e\u4f4d\u7f6e\u548c\u6837\u5f0f<\/strong><\/h2>\n<p>plt.legend(loc=&#39;upper left&#39;, fontsize=&#39;x-large&#39;, frameon=True, shadow=True, title=&quot;Legend&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86\u6807\u7b7e\u7684\u4f4d\u7f6e\u4e3a\u5de6\u4e0a\u89d2\uff08<code>loc=&#39;upper left&#39;<\/code>\uff09\uff0c\u5b57\u4f53\u5927\u5c0f\u4e3a\u8f83\u5927\uff08<code>fontsize=&#39;x-large&#39;<\/code>\uff09\uff0c\u5e76\u6dfb\u52a0\u4e86\u8fb9\u6846\u548c\u9634\u5f71\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528Seaborn\u5e93\u7ed9\u7ebf\u52a0\u6807\u7b7e<\/strong><\/h2>\n<p><p>Seaborn\u662f\u4e00\u4e2a\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7ed8\u56fe\u529f\u80fd\u548c\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\u3002\u53ef\u4ee5\u4f7f\u7528Seaborn\u5e93\u4e2d\u7684<code>lineplot()<\/code>\u51fd\u6570\u8f7b\u677e\u7ed9\u7ebf\u6dfb\u52a0\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528<code>hue<\/code>\u53c2\u6570\u6dfb\u52a0\u6807\u7b7e<\/h2>\n<\/p>\n<p><p>Seaborn\u7684<code>lineplot()<\/code>\u51fd\u6570\u4e2d\u5305\u542b\u4e00\u4e2a<code>hue<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7684\u4e0d\u540c\u7c7b\u522b\u81ea\u52a8\u6dfb\u52a0\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;x&#39;: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5],<\/p>\n<p>    &#39;y&#39;: [2, 3, 5, 7, 11, 1, 4, 6, 8, 10],<\/p>\n<p>    &#39;category&#39;: [&#39;Prime&#39;] * 5 + [&#39;Even&#39;] * 5<\/p>\n<p>}<\/p>\n<h2><strong>\u4f7f\u7528Seaborn\u7ed8\u5236\u5e26\u6807\u7b7e\u7684\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>sns.lineplot(x=&#39;x&#39;, y=&#39;y&#39;, hue=&#39;category&#39;, data=data)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>hue<\/code>\u53c2\u6570\u6839\u636e\u7c7b\u522b\u81ea\u52a8\u6dfb\u52a0\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u8c03\u6574\u6807\u7b7e\u6837\u5f0f<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574Seaborn\u7684<code>lineplot()<\/code>\u51fd\u6570\u7684\u53c2\u6570\u6765\u8bbe\u7f6e\u6807\u7b7e\u7684\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;x&#39;: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5],<\/p>\n<p>    &#39;y&#39;: [2, 3, 5, 7, 11, 1, 4, 6, 8, 10],<\/p>\n<p>    &#39;category&#39;: [&#39;Prime&#39;] * 5 + [&#39;Even&#39;] * 5<\/p>\n<p>}<\/p>\n<h2><strong>\u4f7f\u7528Seaborn\u7ed8\u5236\u5e26\u6807\u7b7e\u7684\u6298\u7ebf\u56fe\uff0c\u5e76\u8c03\u6574\u6807\u7b7e\u6837\u5f0f<\/strong><\/h2>\n<p>sns.lineplot(x=&#39;x&#39;, y=&#39;y&#39;, hue=&#39;category&#39;, data=data, palette=&#39;Set1&#39;, style=&#39;category&#39;, markers=True, dashes=False)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u8c03\u6574<code>palette<\/code>\u3001<code>style<\/code>\u3001<code>markers<\/code>\u548c<code>dashes<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u6807\u7b7e\u7684\u6837\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528Plotly\u5e93\u7ed9\u7ebf\u52a0\u6807\u7b7e<\/strong><\/h2>\n<p><p>Plotly\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u5e93\uff0c\u80fd\u591f\u521b\u5efa\u7f8e\u89c2\u7684\u4ea4\u4e92\u5f0f\u56fe\u8868\u3002\u53ef\u4ee5\u4f7f\u7528Plotly\u5e93\u4e2d\u7684<code>go.Scatter<\/code>\u51fd\u6570\u7ed9\u7ebf\u6dfb\u52a0\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528<code>name<\/code>\u53c2\u6570\u6dfb\u52a0\u6807\u7b7e<\/h2>\n<\/p>\n<p><p>Plotly\u7684<code>go.Scatter<\/code>\u51fd\u6570\u4e2d\u5305\u542b\u4e00\u4e2a<code>name<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u4e3a\u7ed8\u5236\u7684\u7ebf\u6dfb\u52a0\u6807\u7b7e\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>fig.update_layout()<\/code>\u51fd\u6570\u663e\u793a\u8fd9\u4e9b\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.graph_objects as go<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [2, 3, 5, 7, 11]<\/p>\n<p>y2 = [1, 4, 6, 8, 10]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig = go.Figure()<\/p>\n<h2><strong>\u6dfb\u52a0\u4e24\u6761\u7ebf\uff0c\u5e76\u6dfb\u52a0\u6807\u7b7e<\/strong><\/h2>\n<p>fig.add_trace(go.Scatter(x=x, y=y1, mode=&#39;lines&#39;, name=&#39;Prime Numbers&#39;))<\/p>\n<p>fig.add_trace(go.Scatter(x=x, y=y2, mode=&#39;lines&#39;, name=&#39;Even Numbers&#39;))<\/p>\n<h2><strong>\u663e\u793a\u6807\u7b7e<\/strong><\/h2>\n<p>fig.update_layout(title=&#39;Line Chart with Labels&#39;, xaxis_title=&#39;X Axis&#39;, yaxis_title=&#39;Y Axis&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4e3a\u4e24\u6761\u7ebf\u5206\u522b\u6dfb\u52a0\u4e86\u6807\u7b7e\u201cPrime Numbers\u201d\u548c\u201cEven Numbers\u201d\uff0c\u5e76\u4f7f\u7528<code>fig.update_layout()<\/code>\u51fd\u6570\u663e\u793a\u8fd9\u4e9b\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4e3a\u6bcf\u6761\u7ebf\u5355\u72ec\u6dfb\u52a0\u6807\u7b7e<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5728\u6bcf\u6761\u7ebf\u7684\u7279\u5b9a\u4f4d\u7f6e\u6dfb\u52a0\u6587\u672c\u6807\u7b7e\u6765\u4e3a\u6bcf\u6761\u7ebf\u5355\u72ec\u6dfb\u52a0\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.graph_objects as go<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [2, 3, 5, 7, 11]<\/p>\n<p>y2 = [1, 4, 6, 8, 10]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig = go.Figure()<\/p>\n<h2><strong>\u6dfb\u52a0\u4e24\u6761\u7ebf<\/strong><\/h2>\n<p>fig.add_trace(go.Scatter(x=x, y=y1, mode=&#39;lines+text&#39;, name=&#39;Prime Numbers&#39;, text=[str(i) for i in y1], textposition=&#39;top center&#39;))<\/p>\n<p>fig.add_trace(go.Scatter(x=x, y=y2, mode=&#39;lines+text&#39;, name=&#39;Even Numbers&#39;, text=[str(i) for i in y2], textposition=&#39;top center&#39;))<\/p>\n<h2><strong>\u663e\u793a\u6807\u7b7e<\/strong><\/h2>\n<p>fig.update_layout(title=&#39;Line Chart with Labels&#39;, xaxis_title=&#39;X Axis&#39;, yaxis_title=&#39;Y Axis&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>mode=&#39;lines+text&#39;<\/code>\u548c<code>text<\/code>\u53c2\u6570\u4e3a\u6bcf\u6761\u7ebf\u7684\u6bcf\u4e2a\u70b9\u6dfb\u52a0\u4e86\u6587\u672c\u6807\u7b7e\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u4f7f\u7528Pandas\u5e93\u7ed9\u7ebf\u52a0\u6807\u7b7e<\/strong><\/h2>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u80fd\u591f\u8f7b\u677e\u8bfb\u53d6\u3001\u5904\u7406\u548c\u53ef\u89c6\u5316\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u4e2d\u7684<code>plot()<\/code>\u51fd\u6570\u7ed9\u7ebf\u6dfb\u52a0\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h2>1\u3001\u4f7f\u7528<code>label<\/code>\u53c2\u6570\u6dfb\u52a0\u6807\u7b7e<\/h2>\n<\/p>\n<p><p>Pandas\u7684<code>plot()<\/code>\u51fd\u6570\u4e2d\u5305\u542b\u4e00\u4e2a<code>label<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u4e3a\u7ed8\u5236\u7684\u7ebf\u6dfb\u52a0\u6807\u7b7e\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>plt.legend()<\/code>\u51fd\u6570\u663e\u793a\u8fd9\u4e9b\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/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;Prime Numbers&#39;: [2, 3, 5, 7, 11],<\/p>\n<p>    &#39;Even Numbers&#39;: [1, 4, 6, 8, 10]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7ed8\u5236\u5e26\u6807\u7b7e\u7684\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>df.plot(x=&#39;x&#39;, y=[&#39;Prime Numbers&#39;, &#39;Even Numbers&#39;], label=[&#39;Prime Numbers&#39;, &#39;Even Numbers&#39;])<\/p>\n<h2><strong>\u663e\u793a\u6807\u7b7e<\/strong><\/h2>\n<p>plt.legend()<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4e3a\u4e24\u6761\u7ebf\u5206\u522b\u6dfb\u52a0\u4e86\u6807\u7b7e\u201cPrime Numbers\u201d\u548c\u201cEven Numbers\u201d\uff0c\u5e76\u4f7f\u7528<code>plt.legend()<\/code>\u51fd\u6570\u663e\u793a\u8fd9\u4e9b\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><h2>2\u3001\u4e3a\u6bcf\u6761\u7ebf\u5355\u72ec\u6dfb\u52a0\u6807\u7b7e<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5728\u6bcf\u6761\u7ebf\u7684\u7279\u5b9a\u4f4d\u7f6e\u6dfb\u52a0\u6587\u672c\u6807\u7b7e\u6765\u4e3a\u6bcf\u6761\u7ebf\u5355\u72ec\u6dfb\u52a0\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/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;Prime Numbers&#39;: [2, 3, 5, 7, 11],<\/p>\n<p>    &#39;Even Numbers&#39;: [1, 4, 6, 8, 10]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7ed8\u5236\u5e26\u6807\u7b7e\u7684\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>ax = df.plot(x=&#39;x&#39;, y=[&#39;Prime Numbers&#39;, &#39;Even Numbers&#39;])<\/p>\n<h2><strong>\u4e3a\u6bcf\u6761\u7ebf\u6dfb\u52a0\u6587\u672c\u6807\u7b7e<\/strong><\/h2>\n<p>for line in ax.get_lines():<\/p>\n<p>    y_data = line.get_ydata()<\/p>\n<p>    x_data = line.get_xdata()<\/p>\n<p>    for i, txt in enumerate(y_data):<\/p>\n<p>        ax.annotate(txt, (x_data[i], y_data[i]), textcoords=&quot;offset points&quot;, xytext=(0,10), ha=&#39;center&#39;)<\/p>\n<h2><strong>\u663e\u793a\u6807\u7b7e<\/strong><\/h2>\n<p>plt.legend()<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>ax.annotate()<\/code>\u51fd\u6570\u4e3a\u6bcf\u6761\u7ebf\u7684\u6bcf\u4e2a\u70b9\u6dfb\u52a0\u4e86\u6587\u672c\u6807\u7b7e\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e86\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684\u7ed8\u56fe\u5e93\u7ed9\u7ebf\u52a0\u6807\u7b7e\u7684\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Matplotlib\u5e93\u3001Seaborn\u5e93\u3001Plotly\u5e93\u548cPandas\u5e93\u3002\u6bcf\u79cd\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u529f\u80fd\u548c\u7279\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u6765\u7ed8\u5236\u5e26\u6709\u6807\u7b7e\u7684\u6298\u7ebf\u56fe\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Matplotlib\u5e93\u53ef\u4ee5\u901a\u8fc7<code>label<\/code>\u53c2\u6570\u548c<code>plt.legend()<\/code>\u51fd\u6570\u8f7b\u677e\u6dfb\u52a0\u6807\u7b7e\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7<code>plt.annotate()<\/code>\u51fd\u6570\u4e3a\u6bcf\u6761\u7ebf\u5355\u72ec\u6dfb\u52a0\u6807\u7b7e<\/strong>\u3002<strong>Seaborn\u5e93\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7ed8\u56fe\u529f\u80fd\u548c\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\uff0c\u80fd\u591f\u6839\u636e\u6570\u636e\u7684\u4e0d\u540c\u7c7b\u522b\u81ea\u52a8\u6dfb\u52a0\u6807\u7b7e<\/strong>\u3002<strong>Plotly\u5e93\u80fd\u591f\u521b\u5efa\u7f8e\u89c2\u7684\u4ea4\u4e92\u5f0f\u56fe\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7<code>name<\/code>\u53c2\u6570\u548c<code>fig.update_layout()<\/code>\u51fd\u6570\u6dfb\u52a0\u6807\u7b7e<\/strong>\u3002<strong>Pandas\u5e93\u5219\u80fd\u591f\u8f7b\u677e\u8bfb\u53d6\u3001\u5904\u7406\u548c\u53ef\u89c6\u5316\u6570\u636e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>label<\/code>\u53c2\u6570\u548c<code>plt.legend()<\/code>\u51fd\u6570\u6dfb\u52a0\u6807\u7b7e<\/strong>\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7<code>ax.annotate()<\/code>\u51fd\u6570\u4e3a\u6bcf\u6761\u7ebf\u5355\u72ec\u6dfb\u52a0\u6807\u7b7e\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\u6765\u7ed8\u5236\u5e26\u6709\u6807\u7b7e\u7684\u6298\u7ebf\u56fe\uff0c\u4ee5\u66f4\u597d\u5730\u5448\u73b0\u6570\u636e\u548c\u5206\u6790\u7ed3\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4e3a\u56fe\u5f62\u7ebf\u6761\u6dfb\u52a0\u6807\u7b7e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u7ed8\u5236\u56fe\u5f62\u5e76\u4e3a\u7ebf\u6761\u6dfb\u52a0\u6807\u7b7e\u3002\u901a\u8fc7\u5728\u7ed8\u5236\u7ebf\u6761\u65f6\u4f7f\u7528<code>label<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4e3a\u6bcf\u6761\u7ebf\u6307\u5b9a\u6807\u7b7e\u3002\u5728\u4f7f\u7528<code>plt.legend()<\/code>\u51fd\u6570\u65f6\uff0c\u8fd9\u4e9b\u6807\u7b7e\u5c06\u4f1a\u88ab\u663e\u793a\u5728\u56fe\u4f8b\u4e2d\u3002\u4f7f\u7528\u793a\u4f8b\u4ee3\u7801\u53ef\u4ee5\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u8fd9\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9bPython\u5e93\u4e3a\u7ebf\u6761\u6dfb\u52a0\u6807\u7b7e\uff1f<\/strong><br \/>Matplotlib\u662f\u6700\u5e38\u7528\u7684\u5e93\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3002\u9664\u4e86Matplotlib\uff0cSeaborn\u5e93\u4e5f\u53ef\u4ee5\u7528\u4e8e\u521b\u5efa\u66f4\u7f8e\u89c2\u7684\u56fe\u8868\uff0c\u5e76\u4e3a\u7ebf\u6761\u6dfb\u52a0\u6807\u7b7e\u3002\u6b64\u5916\uff0cPlotly\u5e93\u4e5f\u652f\u6301\u4ea4\u4e92\u5f0f\u56fe\u5f62\uff0c\u80fd\u591f\u5b9e\u73b0\u52a8\u6001\u6807\u7b7e\u6548\u679c\uff0c\u9002\u5408\u9700\u8981\u4e0e\u7528\u6237\u4e92\u52a8\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<p><strong>\u6807\u7b7e\u7684\u683c\u5f0f\u548c\u6837\u5f0f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0cMatplotlib\u5141\u8bb8\u7528\u6237\u81ea\u5b9a\u4e49\u6807\u7b7e\u7684\u683c\u5f0f\u548c\u6837\u5f0f\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7<code>fontsize<\/code>\u3001<code>color<\/code>\u3001<code>fontweight<\/code>\u7b49\u53c2\u6570\u6765\u8bbe\u7f6e\u6807\u7b7e\u7684\u5b57\u4f53\u5927\u5c0f\u3001\u989c\u8272\u548c\u7c97\u7ec6\u7b49\u5c5e\u6027\u3002\u540c\u65f6\uff0c\u4f7f\u7528<code>plt.text()<\/code>\u51fd\u6570\u53ef\u4ee5\u5728\u7279\u5b9a\u4f4d\u7f6e\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6587\u672c\u6807\u7b7e\uff0c\u4ee5\u8fbe\u5230\u66f4\u7075\u6d3b\u7684\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u7ed8\u56fe\u5e93\u7ed9\u7ebf\u52a0\u6807\u7b7e\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Matplotlib\u5e93\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7ed8 [&hellip;]","protected":false},"author":3,"featured_media":1183785,"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\/1183783"}],"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=1183783"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183783\/revisions"}],"predecessor-version":[{"id":1183786,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1183783\/revisions\/1183786"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1183785"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1183783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1183783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1183783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}