{"id":1003944,"date":"2024-12-27T10:21:47","date_gmt":"2024-12-27T02:21:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1003944.html"},"modified":"2024-12-27T10:21:53","modified_gmt":"2024-12-27T02:21:53","slug":"python-%e6%95%a3%e7%82%b9%e5%9b%be%e5%a6%82%e4%bd%95%e6%b7%bb%e5%8a%a0%e5%9b%be%e4%be%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1003944.html","title":{"rendered":"python \u6563\u70b9\u56fe\u5982\u4f55\u6dfb\u52a0\u56fe\u4f8b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081059\/caa88d10-a92a-4b31-9d4a-d711e5b69920.webp\" alt=\"python \u6563\u70b9\u56fe\u5982\u4f55\u6dfb\u52a0\u56fe\u4f8b\" \/><\/p>\n<p><p> <strong>\u5728Python\u7684\u6563\u70b9\u56fe\u4e2d\u6dfb\u52a0\u56fe\u4f8b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684legend()\u51fd\u6570\u3001\u901a\u8fc7\u4e3a\u6bcf\u4e2a\u6570\u636e\u96c6\u5206\u914d\u6807\u7b7e\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u6807\u8bb0\u548c\u989c\u8272\u8fdb\u884c\u533a\u5206\u3002<\/strong>\u5728\u8fd9\u4e09\u79cd\u65b9\u6cd5\u4e2d\uff0c\u5408\u7406\u8bbe\u7f6e\u6807\u7b7e\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65\u3002\u901a\u8fc7\u5728\u7ed8\u56fe\u65f6\u4e3a\u6bcf\u4e2a\u6570\u636e\u96c6\u5206\u914d\u72ec\u7279\u7684\u6807\u7b7e\uff0c\u53ef\u4ee5\u786e\u4fdd\u56fe\u4f8b\u51c6\u786e\u5730\u53cd\u6620\u51fa\u56fe\u4e2d\u6bcf\u4e2a\u6570\u636e\u96c6\u7684\u542b\u4e49\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u5e76\u5b9a\u5236\u6563\u70b9\u56fe\u7684\u56fe\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001MATPLOTLIB\u5e93\u6982\u8ff0<\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u56e0\u5176\u5f3a\u5927\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\uff0c\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3002\u901a\u8fc7Matplotlib\uff0c\u7528\u6237\u53ef\u4ee5\u7ed8\u5236\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\uff0c\u5982\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u6563\u70b9\u56fe\u7b49\uff0c\u5e76\u53ef\u4ee5\u5bf9\u56fe\u8868\u8fdb\u884c\u9ad8\u5ea6\u81ea\u5b9a\u4e49\u3002\u8981\u5728\u6563\u70b9\u56fe\u4e2d\u6dfb\u52a0\u56fe\u4f8b\uff0c\u9996\u5148\u9700\u8981\u7406\u89e3Matplotlib\u7684\u57fa\u672c\u7528\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u4e0e\u5bfc\u5165<\/li>\n<\/ol>\n<p><p>Matplotlib\u53ef\u4ee5\u901a\u8fc7pip\u8fdb\u884c\u5b89\u88c5\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u57fa\u672c\u7ed8\u56fe<\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528Matplotlib\u7ed8\u5236\u56fe\u5f62\u65f6\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2aFigure\u5bf9\u8c61\uff0c\u7136\u540e\u5728\u5176\u4e0a\u6dfb\u52a0Axes\u5bf9\u8c61\u3002Figure\u662f\u6574\u4e2a\u7ed8\u56fe\u7684\u5bb9\u5668\uff0c\u800cAxes\u662f\u56fe\u5f62\u7684\u5177\u4f53\u533a\u57df\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, ax = plt.subplots()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u6dfb\u52a0\u56fe\u4f8b\u7684\u57fa\u672c\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u6563\u70b9\u56fe\u4e2d\u6dfb\u52a0\u56fe\u4f8b\u7684\u5173\u952e\u5728\u4e8e\u4e3a\u6bcf\u4e2a\u6570\u636e\u96c6\u5206\u914d\u6807\u7b7e\uff0c\u7136\u540e\u4f7f\u7528legend()\u51fd\u6570\u663e\u793a\u8fd9\u4e9b\u6807\u7b7e\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4e3a\u6570\u636e\u96c6\u5206\u914d\u6807\u7b7e<\/li>\n<\/ol>\n<p><p>\u5728\u7ed8\u5236\u6563\u70b9\u56fe\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528label\u53c2\u6570\u4e3a\u6bcf\u4e2a\u6570\u636e\u96c6\u5206\u914d\u4e00\u4e2a\u6807\u7b7e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.scatter(x_values, y_values, label=&#39;Data Set 1&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528legend()\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u5728\u5206\u914d\u5b8c\u6807\u7b7e\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528legend()\u51fd\u6570\u6765\u663e\u793a\u56fe\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.legend()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u793a\u4f8b\u4ee3\u7801<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u5982\u4f55\u5728\u6563\u70b9\u56fe\u4e2d\u6dfb\u52a0\u56fe\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>x1 = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [2, 3, 5, 7, 11]<\/p>\n<p>x2 = [1, 2, 3, 4, 5]<\/p>\n<p>y2 = [1, 4, 6, 8, 9]<\/p>\n<h2><strong>\u521b\u5efa\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.scatter(x1, y1, label=&#39;Prime Numbers&#39;, color=&#39;r&#39;)<\/p>\n<p>ax.scatter(x2, y2, label=&#39;Random Numbers&#39;, color=&#39;b&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u56fe\u4f8b<\/strong><\/h2>\n<p>ax.legend()<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u56fe\u4f8b\u7684\u9ad8\u7ea7\u5b9a\u5236<\/p>\n<\/p>\n<p><p>Matplotlib\u63d0\u4f9b\u4e86\u591a\u79cd\u9009\u9879\u6765\u5b9a\u5236\u56fe\u4f8b\u7684\u5916\u89c2\uff0c\u5305\u62ec\u4f4d\u7f6e\u3001\u5b57\u4f53\u3001\u8fb9\u6846\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\u56fe\u4f8b\u4f4d\u7f6e<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5728legend()\u51fd\u6570\u4e2d\u4f20\u9012loc\u53c2\u6570\u6765\u6307\u5b9a\u56fe\u4f8b\u7684\u4f4d\u7f6e\u3002\u5e38\u7528\u7684\u4f4d\u7f6e\u6709\uff1a<\/p>\n<\/p>\n<ul>\n<li>&#39;upper right&#39;<\/li>\n<li>&#39;upper left&#39;<\/li>\n<li>&#39;lower left&#39;<\/li>\n<li>&#39;lower right&#39;<\/li>\n<li>&#39;right&#39;<\/li>\n<li>&#39;center left&#39;<\/li>\n<li>&#39;center right&#39;<\/li>\n<li>&#39;lower center&#39;<\/li>\n<li>&#39;upper center&#39;<\/li>\n<li>&#39;center&#39;<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u5c06\u56fe\u4f8b\u653e\u5728\u5de6\u4e0a\u89d2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.legend(loc=&#39;upper left&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u56fe\u4f8b\u5b57\u4f53\u548c\u5927\u5c0f<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528prop\u53c2\u6570\u8bbe\u7f6e\u56fe\u4f8b\u7684\u5b57\u4f53\u548c\u5927\u5c0f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.legend(prop={&#39;size&#39;: 10, &#39;family&#39;: &#39;serif&#39;})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u56fe\u4f8b\u8fb9\u6846<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8bbe\u7f6eframeon\u53c2\u6570\uff0c\u53ef\u4ee5\u63a7\u5236\u662f\u5426\u663e\u793a\u56fe\u4f8b\u7684\u8fb9\u6846\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.legend(frameon=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u7ed3\u5408NumPy\u548cPandas\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u901a\u5e38\u4ee5NumPy\u6570\u7ec4\u6216Pandas\u6570\u636e\u6846\u7684\u5f62\u5f0f\u5b58\u5728\u3002\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u8fd9\u4e9b\u5de5\u5177\u8fdb\u884c\u66f4\u590d\u6742\u7684\u6570\u636e\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528NumPy\u751f\u6210\u6570\u636e<\/li>\n<\/ol>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u751f\u6210\u5927\u89c4\u6a21\u6570\u636e\u96c6\u3002\u4f8b\u5982\uff0c\u751f\u6210\u4e24\u4e2a\u6b63\u6001\u5206\u5e03\u7684\u6570\u636e\u96c6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>x = np.random.normal(0, 1, 100)<\/p>\n<p>y1 = np.random.normal(0, 1, 100)<\/p>\n<p>y2 = np.random.normal(1, 1, 100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u6570\u636e\u7ed8\u5236\u6563\u70b9\u56fe\u5e76\u6dfb\u52a0\u56fe\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, ax = plt.subplots()<\/p>\n<p>ax.scatter(x, y1, label=&#39;Dataset 1&#39;, color=&#39;g&#39;, alpha=0.5)<\/p>\n<p>ax.scatter(x, y2, label=&#39;Dataset 2&#39;, color=&#39;m&#39;, alpha=0.5)<\/p>\n<p>ax.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Pandas\u5904\u7406\u6570\u636e<\/li>\n<\/ol>\n<p><p>Pandas\u662f\u4e00\u4e2a\u7528\u4e8e\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u7684\u5e93\uff0c\u901a\u5e38\u7528\u4e8e\u8bfb\u53d6\u548c\u5904\u7406CSV\u6587\u4ef6\u7b49\u6570\u636e\u6e90\u3002\u53ef\u4ee5\u4f7f\u7528Pandas\u8bfb\u53d6\u6570\u636e\u5e76\u7ed8\u5236\u6563\u70b9\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2aCSV\u6587\u4ef6\uff0c\u5305\u542bx\u3001y1\u3001y2\u4e09\u5217\u6570\u636e<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.scatter(data[&#39;x&#39;], data[&#39;y1&#39;], label=&#39;Group 1&#39;, color=&#39;c&#39;)<\/p>\n<p>ax.scatter(data[&#39;x&#39;], data[&#39;y2&#39;], label=&#39;Group 2&#39;, color=&#39;y&#39;)<\/p>\n<p>ax.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528Seaborn\u8fdb\u884c\u9ad8\u7ea7\u53ef\u89c6\u5316<\/p>\n<\/p>\n<p><p>Seaborn\u662f\u57fa\u4e8eMatplotlib\u6784\u5efa\u7684\u4e00\u4e2a\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u7b80\u4fbf\u7684\u7ed8\u56fe\u63a5\u53e3\u548c\u66f4\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\u3002\u5b83\u7279\u522b\u9002\u5408\u7528\u4e8e\u7edf\u8ba1\u6570\u636e\u7684\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u4e0e\u5bfc\u5165<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5Seaborn\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install seaborn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Seaborn\u7ed8\u5236\u6563\u70b9\u56fe<\/li>\n<\/ol>\n<p><p>Seaborn\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u7ed8\u5236\u6563\u70b9\u56fe\uff0c\u5e76\u81ea\u52a8\u5904\u7406\u56fe\u4f8b\u3002\u4f8b\u5982\uff0c\u4f7f\u7528Seaborn\u7684scatterplot()\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>tips = sns.load_dataset(&#39;tips&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>sns.scatterplot(data=tips, x=&#39;total_bill&#39;, y=&#39;tip&#39;, hue=&#39;day&#39;, style=&#39;time&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cSeaborn\u81ea\u52a8\u4e3a\u4e0d\u540c\u7684\u201cday\u201d\u548c\u201ctime\u201d\u5206\u914d\u4e86\u4e0d\u540c\u7684\u989c\u8272\u548c\u5f62\u72b6\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2a\u56fe\u4f8b\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u5b9a\u5236Seaborn\u56fe\u4f8b<\/li>\n<\/ol>\n<p><p>Seaborn\u5141\u8bb8\u7528\u6237\u901a\u8fc7legend()\u51fd\u6570\u5b9a\u5236\u56fe\u4f8b\u7684\u5916\u89c2\uff0c\u4e0eMatplotlib\u7c7b\u4f3c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sns.scatterplot(data=tips, x=&#39;total_bill&#39;, y=&#39;tip&#39;, hue=&#39;day&#39;, style=&#39;time&#39;)<\/p>\n<p>plt.legend(loc=&#39;upper left&#39;, title=&#39;Legend&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5728\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u4f7f\u7528Plotly<\/p>\n<\/p>\n<p><p>Plotly\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868\u7684\u5e93\uff0c\u652f\u6301\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u5305\u62ecPython\u3002\u5728\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\uff0cPlotly\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u4e3a\u6570\u636e\u53ef\u89c6\u5316\u589e\u6dfb\u66f4\u591a\u52a8\u6001\u5143\u7d20\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u4e0e\u5bfc\u5165<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5Plotly\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install plotly<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528Plotly\u7ed8\u5236\u6563\u70b9\u56fe<\/li>\n<\/ol>\n<p><p>Plotly\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u7ed8\u5236\u4ea4\u4e92\u5f0f\u6563\u70b9\u56fe\uff0c\u5e76\u81ea\u52a8\u5904\u7406\u56fe\u4f8b\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>df = px.data.iris()<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>fig = px.scatter(df, x=&#39;sepal_width&#39;, y=&#39;sepal_length&#39;, color=&#39;species&#39;, symbol=&#39;species&#39;)<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0cPlotly\u81ea\u52a8\u4e3a\u4e0d\u540c\u7684\u201cspecies\u201d\u5206\u914d\u4e86\u4e0d\u540c\u7684\u989c\u8272\u548c\u5f62\u72b6\uff0c\u5e76\u521b\u5efa\u4e86\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u56fe\u4f8b\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u5b9a\u5236Plotly\u56fe\u4f8b<\/li>\n<\/ol>\n<p><p>Plotly\u5141\u8bb8\u7528\u6237\u901a\u8fc7update_layout()\u51fd\u6570\u5b9a\u5236\u56fe\u4f8b\u7684\u5916\u89c2\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig.update_layout(legend_title_text=&#39;Species&#39;)<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u4e3a\u6563\u70b9\u56fe\u6dfb\u52a0\u56fe\u4f8b\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u53ef\u89c6\u5316\u6280\u5de7\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u89c2\u4f17\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u3002\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528Matplotlib\u3001Seaborn\u6216Plotly\u6765\u521b\u5efa\u548c\u5b9a\u5236\u56fe\u4f8b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4e3a\u6bcf\u4e2a\u6570\u636e\u96c6\u5206\u914d\u6e05\u6670\u7684\u6807\u7b7e<\/strong>\uff1a\u786e\u4fdd\u56fe\u4f8b\u4e2d\u7684\u6bcf\u4e2a\u6807\u7b7e\u90fd\u51c6\u786e\u63cf\u8ff0\u6570\u636e\u96c6\u7684\u542b\u4e49\u3002<\/li>\n<li><strong>\u9009\u62e9\u5408\u9002\u7684\u989c\u8272\u548c\u6807\u8bb0<\/strong>\uff1a\u4f7f\u7528\u4e0d\u540c\u7684\u989c\u8272\u548c\u6807\u8bb0\u6765\u533a\u5206\u6570\u636e\u96c6\uff0c\u4f7f\u56fe\u4f8b\u66f4\u5177\u53ef\u8bfb\u6027\u3002<\/li>\n<li><strong>\u5b9a\u5236\u56fe\u4f8b\u7684\u4f4d\u7f6e\u548c\u5916\u89c2<\/strong>\uff1a\u6839\u636e\u56fe\u8868\u7684\u5e03\u5c40\u548c\u8bbe\u8ba1\u9700\u6c42\uff0c\u5b9a\u5236\u56fe\u4f8b\u7684\u4f4d\u7f6e\u3001\u5b57\u4f53\u548c\u8fb9\u6846\u3002<\/li>\n<li><strong>\u4f7f\u7528\u9ad8\u7ea7\u5de5\u5177\u8fdb\u884c\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316<\/strong>\uff1a\u5728\u9700\u8981\u4ea4\u4e92\u529f\u80fd\u7684\u60c5\u51b5\u4e0b\uff0c\u8003\u8651\u4f7f\u7528Plotly\u7b49\u5de5\u5177\u6765\u589e\u5f3a\u7528\u6237\u4f53\u9a8c\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u548c\u6280\u672f\uff0c\u53ef\u4ee5\u521b\u5efa\u51fa\u66f4\u52a0\u4e13\u4e1a\u548c\u6613\u4e8e\u7406\u89e3\u7684\u6570\u636e\u53ef\u89c6\u5316\u4f5c\u54c1\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\u56fe\u4f8b\uff1f<\/strong><br \/>\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u7ed8\u5236\u6563\u70b9\u56fe\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>plt.legend()<\/code>\u51fd\u6570\u6765\u6dfb\u52a0\u56fe\u4f8b\u3002\u5728\u7ed8\u5236\u6563\u70b9\u56fe\u65f6\uff0c\u4e3a\u6bcf\u4e00\u7ec4\u6570\u636e\u6307\u5b9a\u6807\u7b7e\uff0c\u7136\u540e\u5728\u8c03\u7528<code>legend()<\/code>\u51fd\u6570\u65f6\uff0c\u56fe\u4f8b\u5c06\u81ea\u52a8\u663e\u793a\u8fd9\u4e9b\u6807\u7b7e\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\n# \u793a\u4f8b\u6570\u636e\nx1 = [1, 2, 3, 4]\ny1 = [10, 20, 25, 30]\nx2 = [1, 2, 3, 4]\ny2 = [5, 15, 20, 25]\n\n# \u7ed8\u5236\u6563\u70b9\u56fe\nplt.scatter(x1, y1, label=&#39;\u6570\u636e\u7ec41&#39;, color=&#39;blue&#39;)\nplt.scatter(x2, y2, label=&#39;\u6570\u636e\u7ec42&#39;, color=&#39;red&#39;)\n\n# \u6dfb\u52a0\u56fe\u4f8b\nplt.legend()\nplt.show()\n<\/code><\/pre>\n<p><strong>\u6563\u70b9\u56fe\u4e2d\u7684\u56fe\u4f8b\u53ef\u4ee5\u81ea\u5b9a\u4e49\u54ea\u4e9b\u5c5e\u6027\uff1f<\/strong><br \/>\u5728\u521b\u5efa\u56fe\u4f8b\u65f6\uff0c\u7528\u6237\u53ef\u4ee5\u81ea\u5b9a\u4e49\u591a\u4e2a\u5c5e\u6027\uff0c\u5305\u62ec\u56fe\u4f8b\u7684\u4f4d\u7f6e\u3001\u5b57\u4f53\u5927\u5c0f\u3001\u8fb9\u6846\u6837\u5f0f\u7b49\u3002\u901a\u8fc7\u4f20\u5165\u53c2\u6570<code>loc<\/code>\u53ef\u4ee5\u8bbe\u7f6e\u56fe\u4f8b\u7684\u4f4d\u7f6e\uff0c<code>fontsize<\/code>\u53ef\u4ee5\u8c03\u6574\u5b57\u4f53\u5927\u5c0f\uff0c<code>frameon<\/code>\u53ef\u4ee5\u63a7\u5236\u662f\u5426\u663e\u793a\u8fb9\u6846\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">plt.legend(loc=&#39;upper left&#39;, fontsize=&#39;large&#39;, frameon=False)\n<\/code><\/pre>\n<p><strong>\u5982\u679c\u6211\u60f3\u4e3a\u6563\u70b9\u56fe\u4e2d\u7684\u4e0d\u540c\u70b9\u4f7f\u7528\u4e0d\u540c\u7684\u6807\u8bb0\u6837\u5f0f\uff0c\u8be5\u5982\u4f55\u64cd\u4f5c\uff1f<\/strong><br \/>\u53ef\u4ee5\u4e3a\u6bcf\u7ec4\u6570\u636e\u4f7f\u7528\u4e0d\u540c\u7684\u6807\u8bb0\u6837\u5f0f\uff08\u5982\u5706\u5f62\u3001\u4e09\u89d2\u5f62\u3001\u6b63\u65b9\u5f62\u7b49\uff09\uff0c\u5728\u8c03\u7528<code>scatter()<\/code>\u51fd\u6570\u65f6\uff0c\u901a\u8fc7<code>marker<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>marker=&#39;o&#39;<\/code>\u8868\u793a\u5706\u5f62\uff0c<code>marker=&#39;^&#39;<\/code>\u8868\u793a\u4e09\u89d2\u5f62\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">plt.scatter(x1, y1, label=&#39;\u5706\u5f62\u6807\u8bb0&#39;, marker=&#39;o&#39;, color=&#39;blue&#39;)\nplt.scatter(x2, y2, label=&#39;\u4e09\u89d2\u5f62\u6807\u8bb0&#39;, marker=&#39;^&#39;, color=&#39;red&#39;)\nplt.legend()\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u56fe\u4f8b\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u89c6\u89c9\u51b2\u51fb\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u7684\u6563\u70b9\u56fe\u4e2d\u6dfb\u52a0\u56fe\u4f8b\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684legend()\u51fd\u6570\u3001\u901a\u8fc7\u4e3a\u6bcf\u4e2a\u6570\u636e\u96c6 [&hellip;]","protected":false},"author":3,"featured_media":1003960,"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\/1003944"}],"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=1003944"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1003944\/revisions"}],"predecessor-version":[{"id":1003966,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1003944\/revisions\/1003966"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1003960"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1003944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1003944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1003944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}