{"id":999893,"date":"2024-12-27T09:45:14","date_gmt":"2024-12-27T01:45:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/999893.html"},"modified":"2024-12-27T09:45:17","modified_gmt":"2024-12-27T01:45:17","slug":"python%e6%95%a3%e7%82%b9%e5%9b%be%e5%a6%82%e4%bd%95%e5%8a%a0%e5%9b%be%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/999893.html","title":{"rendered":"Python\u6563\u70b9\u56fe\u5982\u4f55\u52a0\u56fe\u793a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074711\/4bf49ee6-2f95-4147-ac29-1cf670e81344.webp\" alt=\"Python\u6563\u70b9\u56fe\u5982\u4f55\u52a0\u56fe\u793a\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684scatter\u51fd\u6570\u7ed8\u5236\u6563\u70b9\u56fe\uff0c\u5e76\u901a\u8fc7legend\u51fd\u6570\u6dfb\u52a0\u56fe\u4f8b<\/strong>\u3002\u5728\u6dfb\u52a0\u56fe\u4f8b\u65f6\uff0c\u901a\u5e38\u4f1a\u4e3a\u4e0d\u540c\u7684\u6570\u636e\u96c6\u6216\u7c7b\u522b\u5206\u914d\u4e0d\u540c\u7684\u989c\u8272\u6216\u6807\u8bb0\uff0c\u4ee5\u4fbf\u6e05\u6670\u5730\u5c55\u793a\u6570\u636e\u7684\u5206\u7c7b\u6216\u5206\u7ec4\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u56fe\u8868\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u5e2e\u52a9\u89c2\u4f17\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u80cc\u540e\u7684\u4fe1\u606f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Matplotlib\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001MATPLOTLIB\u5e93\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u7279\u522b\u9002\u7528\u4e8e\u521b\u5efa\u9759\u6001\u3001\u4ea4\u4e92\u5f0f\u548c\u52a8\u753b\u7684\u53ef\u89c6\u5316\u56fe\u8868\u3002\u5b83\u63d0\u4f9b\u4e86\u5404\u79cd\u56fe\u5f62\u7684\u7ed8\u5236\u529f\u80fd\uff0c\u5305\u62ec\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u6563\u70b9\u56fe\u7b49\u3002\u5728\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0cMatplotlib\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>Matplotlib\u7684\u6838\u5fc3\u662fpyplot\u5b50\u6a21\u5757\uff0c\u5b83\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u4e8eMATLAB\u7684\u7ed8\u56fe\u63a5\u53e3\uff0c\u4f7f\u5f97\u7ed8\u56fe\u8fc7\u7a0b\u66f4\u52a0\u76f4\u89c2\u548c\u7b80\u5355\u3002\u901a\u8fc7pyplot\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u3001\u4fee\u6539\u548c\u4fdd\u5b58\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7ed8\u5236\u6563\u70b9\u56fe<\/h3>\n<\/p>\n<p><h4>1. \u521b\u5efa\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u6563\u70b9\u56fe\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u51c6\u5907\u4e00\u4e9b\u6570\u636e\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u6570\u636e\u53ef\u4ee5\u6765\u81ea\u4e8e\u6587\u4ef6\u3001\u6570\u636e\u5e93\u6216\u76f4\u63a5\u5728\u4ee3\u7801\u4e2d\u5b9a\u4e49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u521b\u5efa\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u968f\u673a\u6570\u636e<\/strong><\/h2>\n<p>x = np.random.rand(50)<\/p>\n<p>y = np.random.rand(50)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86NumPy\u5e93\u6765\u751f\u621050\u4e2a\u968f\u673a\u6570\uff0c\u5206\u522b\u4f5c\u4e3ax\u548cy\u5750\u6807\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528scatter\u51fd\u6570\u7ed8\u5236\u6563\u70b9\u56fe<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>plt.scatter(x, y)<\/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 Example&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>scatter<\/code>\u51fd\u6570\u7528\u4e8e\u7ed8\u5236\u6563\u70b9\u56fe\uff0c<code>xlabel<\/code>\u548c<code>ylabel<\/code>\u7528\u4e8e\u8bbe\u7f6e\u5750\u6807\u8f74\u6807\u7b7e\uff0c<code>title<\/code>\u7528\u4e8e\u8bbe\u7f6e\u56fe\u8868\u6807\u9898\uff0c<code>show<\/code>\u51fd\u6570\u7528\u4e8e\u663e\u793a\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6dfb\u52a0\u56fe\u4f8b<\/h3>\n<\/p>\n<p><h4>1. \u4e3a\u4e0d\u540c\u7c7b\u522b\u7684\u6570\u636e\u5206\u914d\u4e0d\u540c\u7684\u989c\u8272\u6216\u6807\u8bb0<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u9700\u8981\u5c06\u6570\u636e\u5206\u4e3a\u591a\u4e2a\u7c7b\u522b\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u7c7b\u522b\u5206\u914d\u4e0d\u540c\u7684\u989c\u8272\u6216\u6807\u8bb0\uff0c\u4ee5\u4fbf\u5728\u56fe\u4e2d\u533a\u5206\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e09\u7ec4\u6570\u636e<\/p>\n<p>x1, y1 = np.random.rand(50), np.random.rand(50)<\/p>\n<p>x2, y2 = np.random.rand(50), np.random.rand(50)<\/p>\n<p>x3, y3 = np.random.rand(50), np.random.rand(50)<\/p>\n<h2><strong>\u7ed8\u5236\u4e0d\u540c\u989c\u8272\u7684\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>plt.scatter(x1, y1, c=&#39;r&#39;, label=&#39;Category 1&#39;)<\/p>\n<p>plt.scatter(x2, y2, c=&#39;g&#39;, label=&#39;Category 2&#39;)<\/p>\n<p>plt.scatter(x3, y3, c=&#39;b&#39;, label=&#39;Category 3&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e09\u7ec4\u6570\u636e\uff0c\u5e76\u901a\u8fc7<code>c<\/code>\u53c2\u6570\u4e3a\u6bcf\u7ec4\u6570\u636e\u5206\u914d\u4e86\u4e0d\u540c\u7684\u989c\u8272\u3002<\/p>\n<\/p>\n<p><h4>2. \u4f7f\u7528legend\u51fd\u6570\u6dfb\u52a0\u56fe\u4f8b<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u56fe\u4f8b<\/p>\n<p>plt.legend()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>legend<\/code>\u51fd\u6570\u7528\u4e8e\u6dfb\u52a0\u56fe\u4f8b\u3002\u901a\u8fc7\u4e3a\u6bcf\u7ec4\u6570\u636e\u6307\u5b9a<code>label<\/code>\u53c2\u6570\uff0clegend\u51fd\u6570\u5c06\u81ea\u52a8\u751f\u6210\u76f8\u5e94\u7684\u56fe\u4f8b\u9879\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9a\u5236\u56fe\u4f8b<\/h3>\n<\/p>\n<p><p>\u56fe\u4f8b\u7684\u6837\u5f0f\u548c\u4f4d\u7f6e\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u5b9a\u5236\uff0c\u4ee5\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bbe\u7f6e\u56fe\u4f8b\u7684\u4f4d\u7f6e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u56fe\u4f8b\u5e76\u8bbe\u7f6e\u4f4d\u7f6e<\/p>\n<p>plt.legend(loc=&#39;upper right&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>loc<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u56fe\u4f8b\u7684\u4f4d\u7f6e\u3002\u5e38\u7528\u7684\u4f4d\u7f6e\u9009\u9879\u5305\u62ec<code>upper right<\/code>\u3001<code>upper left<\/code>\u3001<code>lower left<\/code>\u3001<code>lower right<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2. \u8bbe\u7f6e\u56fe\u4f8b\u7684\u5b57\u4f53\u5927\u5c0f<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u56fe\u4f8b\u5e76\u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f<\/p>\n<p>plt.legend(fontsize=&#39;small&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7<code>fontsize<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u56fe\u4f8b\u6587\u5b57\u7684\u5927\u5c0f\u3002\u53ef\u9009\u503c\u5305\u62ec<code>small<\/code>\u3001<code>medium<\/code>\u3001<code>large<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h4>3. \u81ea\u5b9a\u4e49\u56fe\u4f8b\u6807\u8bb0<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u81ea\u5b9a\u4e49\u56fe\u4f8b\u6807\u8bb0\u7684\u6837\u5f0f\u3002\u53ef\u4ee5\u901a\u8fc7<code>Handler<\/code>\u7c7b\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from matplotlib.legend_handler import HandlerLine2D<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49\u56fe\u4f8b\u6807\u8bb0\u6837\u5f0f<\/strong><\/h2>\n<p>plt.legend(handler_map={plt.Line2D: HandlerLine2D(numpoints=2)})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>HandlerLine2D<\/code>\u7c7b\u7684<code>numpoints<\/code>\u53c2\u6570\u7528\u4e8e\u8bbe\u7f6e\u56fe\u4f8b\u6807\u8bb0\u7684\u6837\u672c\u70b9\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u7ed8\u5236\u5e26\u6709\u56fe\u4f8b\u7684\u6563\u70b9\u56fe\uff0c\u5e76\u8fdb\u884c\u5404\u79cd\u5b9a\u5236\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x1, y1 = np.random.rand(50), np.random.rand(50)<\/p>\n<p>x2, y2 = np.random.rand(50), np.random.rand(50)<\/p>\n<p>x3, y3 = np.random.rand(50), np.random.rand(50)<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>plt.scatter(x1, y1, c=&#39;r&#39;, label=&#39;Category 1&#39;)<\/p>\n<p>plt.scatter(x2, y2, c=&#39;g&#39;, label=&#39;Category 2&#39;)<\/p>\n<p>plt.scatter(x3, y3, c=&#39;b&#39;, label=&#39;Category 3&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u56fe\u4f8b<\/strong><\/h2>\n<p>plt.legend(loc=&#39;upper right&#39;, fontsize=&#39;medium&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u548c\u6807\u9898<\/strong><\/h2>\n<p>plt.xlabel(&#39;X Axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y Axis&#39;)<\/p>\n<p>plt.title(&#39;Customized Scatter Plot with Legend&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5176\u4ed6\u53ef\u89c6\u5316\u6280\u5de7<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u53ef\u89c6\u5316\u4e2d\uff0c\u9664\u4e86\u57fa\u672c\u7684\u6563\u70b9\u56fe\u548c\u56fe\u4f8b\u4e4b\u5916\uff0c\u8fd8\u6709\u8bb8\u591a\u5176\u4ed6\u7684\u53ef\u89c6\u5316\u6280\u5de7\u548c\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u66f4\u597d\u5730\u5c55\u793a\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u6807\u8bb0\u5927\u5c0f\u548c\u989c\u8272\u8868\u793a\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u4e0d\u4ec5\u9700\u8981\u901a\u8fc7\u4f4d\u7f6e\u6765\u8868\u793a\u6570\u636e\uff0c\u8fd8\u9700\u8981\u901a\u8fc7\u6807\u8bb0\u7684\u5927\u5c0f\u548c\u989c\u8272\u6765\u4f20\u8fbe\u66f4\u591a\u7684\u4fe1\u606f\u3002<code>scatter<\/code>\u51fd\u6570\u7684<code>s<\/code>\u548c<code>c<\/code>\u53c2\u6570\u53ef\u4ee5\u7528\u4e8e\u8bbe\u7f6e\u6807\u8bb0\u7684\u5927\u5c0f\u548c\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u6807\u8bb0\u5927\u5c0f\u548c\u989c\u8272<\/p>\n<p>sizes = 100 * np.random.rand(50)<\/p>\n<p>colors = np.random.rand(50)<\/p>\n<p>plt.scatter(x, y, s=sizes, c=colors, alpha=0.5, cmap=&#39;viridis&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>sizes<\/code>\u6570\u7ec4\u7528\u4e8e\u6307\u5b9a\u6bcf\u4e2a\u70b9\u7684\u5927\u5c0f\uff0c<code>colors<\/code>\u6570\u7ec4\u7528\u4e8e\u6307\u5b9a\u6bcf\u4e2a\u70b9\u7684\u989c\u8272\uff0c<code>alpha<\/code>\u53c2\u6570\u7528\u4e8e\u8bbe\u7f6e\u900f\u660e\u5ea6\uff0c<code>cmap<\/code>\u53c2\u6570\u7528\u4e8e\u9009\u62e9\u989c\u8272\u6620\u5c04\u3002<\/p>\n<\/p>\n<p><h4>2. \u7ed8\u5236\u5e26\u56de\u5f52\u7ebf\u7684\u6563\u70b9\u56fe<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u6563\u70b9\u56fe\u5e38\u5e38\u4e0e\u56de\u5f52\u5206\u6790\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u8bc6\u522b\u6570\u636e\u4e2d\u7684\u8d8b\u52bf\u3002\u53ef\u4ee5\u4f7f\u7528<code>numpy.polyfit<\/code>\u51fd\u6570\u62df\u5408\u56de\u5f52\u7ebf\uff0c\u5e76\u4f7f\u7528<code>plot<\/code>\u51fd\u6570\u7ed8\u5236\u56de\u5f52\u7ebf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u56de\u5f52\u7cfb\u6570<\/p>\n<p>slope, intercept = np.polyfit(x, y, 1)<\/p>\n<h2><strong>\u7ed8\u5236\u56de\u5f52\u7ebf<\/strong><\/h2>\n<p>plt.plot(x, slope * x + intercept, &#39;r&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>polyfit<\/code>\u51fd\u6570\u8fd4\u56de\u56de\u5f52\u7cfb\u6570<code>(slope, intercept)<\/code>\uff0c\u7528\u4e8e\u5b9a\u4e49\u56de\u5f52\u7ebf\u7684\u65b9\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>3. \u6dfb\u52a0\u6ce8\u91ca<\/h4>\n<\/p>\n<p><p>\u6ce8\u91ca\u53ef\u4ee5\u7528\u4e8e\u6807\u6ce8\u7279\u6b8a\u7684\u6570\u636e\u70b9\u6216\u8bf4\u660e\u56fe\u8868\u7684\u67d0\u4e9b\u90e8\u5206\u3002<code>annotate<\/code>\u51fd\u6570\u53ef\u7528\u4e8e\u6dfb\u52a0\u6ce8\u91ca\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u6ce8\u91ca<\/p>\n<p>plt.annotate(&#39;Special Point&#39;, xy=(x[0], y[0]), xytext=(x[0]+0.1, y[0]+0.1),<\/p>\n<p>             arrowprops=dict(facecolor=&#39;black&#39;, shrink=0.05))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>xy<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u88ab\u6ce8\u91ca\u70b9\u7684\u5750\u6807\uff0c<code>xytext<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u6ce8\u91ca\u6587\u672c\u7684\u4f4d\u7f6e\uff0c<code>arrowprops<\/code>\u53c2\u6570\u7528\u4e8e\u8bbe\u7f6e\u7bad\u5934\u7684\u6837\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528Matplotlib\u5e93\uff0cPython\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5730\u7ed8\u5236\u51fa\u5e26\u6709\u56fe\u4f8b\u7684\u6563\u70b9\u56fe\uff0c\u5e76\u901a\u8fc7\u5b9a\u5236\u56fe\u4f8b\u3001\u6807\u8bb0\u5927\u5c0f\u548c\u989c\u8272\u3001\u6dfb\u52a0\u56de\u5f52\u7ebf\u548c\u6ce8\u91ca\u7b49\u65b9\u6cd5\uff0c\u63d0\u5347\u56fe\u8868\u7684\u53ef\u8bfb\u6027\u548c\u4fe1\u606f\u4f20\u8fbe\u80fd\u529b\u3002\u8fd9\u4e9b\u6280\u5de7\u4e0d\u4ec5\u9002\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u62a5\u544a\u548c\u6f14\u793a\u4e2d\uff0c\u5e2e\u52a9\u89c2\u4f17\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u80cc\u540e\u7684\u6545\u4e8b\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Matplotlib\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u65f6\uff0c\u7075\u6d3b\u8fd0\u7528\u5404\u79cd\u53c2\u6570\u548c\u51fd\u6570\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u4e30\u5bcc\u56fe\u8868\u7684\u8868\u73b0\u529b\uff0c\u5e76\u4f7f\u5176\u66f4\u5177\u4e13\u4e1a\u6027\u548c\u5438\u5f15\u529b\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u8bfb\u8005\u80fd\u591f\u638c\u63e1\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u5e26\u6709\u56fe\u4f8b\u7684\u6563\u70b9\u56fe\uff0c\u5e76\u8fdb\u884c\u5404\u79cd\u81ea\u5b9a\u4e49\u8bbe\u7f6e\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\u5e93\u7ed8\u5236\u6563\u70b9\u56fe\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>plt.legend()<\/code>\u51fd\u6570\u8f7b\u677e\u6dfb\u52a0\u56fe\u4f8b\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5728\u7ed8\u5236\u6563\u70b9\u56fe\u65f6\u4e3a\u6bcf\u4e2a\u6570\u636e\u96c6\u6307\u5b9a\u6807\u7b7e\uff0c\u4f8b\u5982<code>plt.scatter(x, y, label=&#39;\u6570\u636e\u96c61&#39;)<\/code>\u3002\u5b8c\u6210\u7ed8\u56fe\u540e\u8c03\u7528<code>plt.legend()<\/code>\u5373\u53ef\u663e\u793a\u56fe\u4f8b\uff0c\u60a8\u8fd8\u53ef\u4ee5\u81ea\u5b9a\u4e49\u56fe\u4f8b\u7684\u4f4d\u7f6e\u548c\u6837\u5f0f\u3002<\/p>\n<p><strong>\u6563\u70b9\u56fe\u4e2d\u56fe\u4f8b\u7684\u4f4d\u7f6e\u53ef\u4ee5\u8c03\u6574\u5417\uff1f<\/strong><br \/>\u53ef\u4ee5\u8c03\u6574\u56fe\u4f8b\u7684\u4f4d\u7f6e\u4ee5\u6ee1\u8db3\u60a8\u7684\u9700\u6c42\u3002<code>plt.legend()<\/code>\u51fd\u6570\u5141\u8bb8\u60a8\u4f20\u5165<code>loc<\/code>\u53c2\u6570\uff0c\u4f8b\u5982<code>loc=&#39;upper right&#39;<\/code>\u6216<code>loc=&#39;lower left&#39;<\/code>\uff0c\u8fd9\u6837\u60a8\u53ef\u4ee5\u5c06\u56fe\u4f8b\u653e\u7f6e\u5728\u56fe\u5f62\u7684\u7279\u5b9a\u533a\u57df\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>bbox_to_anchor<\/code>\u53c2\u6570\u8fdb\u4e00\u6b65\u81ea\u5b9a\u4e49\u56fe\u4f8b\u7684\u4f4d\u7f6e\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u6563\u70b9\u56fe\u7684\u56fe\u4f8b\u6837\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>plt.legend()<\/code>\u51fd\u6570\u7684\u591a\u79cd\u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u56fe\u4f8b\u7684\u6837\u5f0f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>fontsize<\/code>\u53c2\u6570\u8c03\u6574\u5b57\u4f53\u5927\u5c0f\uff0c\u4f7f\u7528<code>frameon=False<\/code>\u6765\u53bb\u6389\u56fe\u4f8b\u7684\u8fb9\u6846\uff0c\u6216\u8005\u901a\u8fc7<code>title<\/code>\u53c2\u6570\u4e3a\u56fe\u4f8b\u6dfb\u52a0\u6807\u9898\u3002\u501f\u52a9\u8fd9\u4e9b\u9009\u9879\uff0c\u60a8\u53ef\u4ee5\u4f7f\u56fe\u4f8b\u66f4\u7b26\u5408\u6574\u4f53\u56fe\u5f62\u7684\u98ce\u683c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528Matplotlib\u5e93\u4e2d\u7684scatter\u51fd\u6570\u7ed8\u5236\u6563\u70b9\u56fe\uff0c\u5e76\u901a\u8fc7legend\u51fd\u6570\u6dfb [&hellip;]","protected":false},"author":3,"featured_media":999905,"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\/999893"}],"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=999893"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999893\/revisions"}],"predecessor-version":[{"id":999906,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/999893\/revisions\/999906"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/999905"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=999893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=999893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=999893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}