{"id":1156064,"date":"2025-01-13T18:11:33","date_gmt":"2025-01-13T10:11:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1156064.html"},"modified":"2025-01-13T18:11:36","modified_gmt":"2025-01-13T10:11:36","slug":"python%e5%9b%be%e4%be%8b%e5%a4%a7%e5%b0%8f%e5%a6%82%e4%bd%95%e6%8e%a7%e5%88%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1156064.html","title":{"rendered":"python\u56fe\u4f8b\u5927\u5c0f\u5982\u4f55\u63a7\u5236"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25194606\/c3c85403-9324-46a1-b047-ac900e01e193.webp\" alt=\"python\u56fe\u4f8b\u5927\u5c0f\u5982\u4f55\u63a7\u5236\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u63a7\u5236\u56fe\u4f8b\u5927\u5c0f\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1a<strong>\u8c03\u6574\u56fe\u4f8b\u5b57\u4f53\u5927\u5c0f\u3001\u8bbe\u7f6e\u56fe\u4f8b\u6846\u7684\u5927\u5c0f\u3001\u4f7f\u7528bbox_to_anchor\u8c03\u6574\u56fe\u4f8b\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\u3001\u8bbe\u7f6e\u56fe\u4f8b\u7684\u900f\u660e\u5ea6<\/strong>\u3002\u5176\u4e2d\uff0c\u8c03\u6574\u56fe\u4f8b\u5b57\u4f53\u5927\u5c0f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u4fee\u6539\u5b57\u4f53\u5927\u5c0f\u53ef\u4ee5\u5feb\u901f\u6539\u53d8\u56fe\u4f8b\u7684\u663e\u793a\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8c03\u6574\u56fe\u4f8b\u5b57\u4f53\u5927\u5c0f<\/h3>\n<\/p>\n<p><p>\u8c03\u6574\u56fe\u4f8b\u5b57\u4f53\u5927\u5c0f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u8bbe\u7f6e<code>fontsize<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u5feb\u901f\u63a7\u5236\u56fe\u4f8b\u7684\u5b57\u4f53\u5927\u5c0f\uff0c\u4ece\u800c\u95f4\u63a5\u5f71\u54cd\u56fe\u4f8b\u7684\u6574\u4f53\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [1, 4, 9, 16, 25]<\/p>\n<p>y2 = [1, 3, 6, 10, 15]<\/p>\n<p>plt.plot(x, y1, label=&#39;y = x^2&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;y = x + 2&#39;)<\/p>\n<h2><strong>\u8c03\u6574\u56fe\u4f8b\u5b57\u4f53\u5927\u5c0f<\/strong><\/h2>\n<p>plt.legend(fontsize=&#39;large&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>fontsize<\/code>\u53c2\u6570\u88ab\u8bbe\u7f6e\u4e3a&#39;large&#39;\uff0c\u8fd9\u4f1a\u4f7f\u56fe\u4f8b\u7684\u5b57\u4f53\u53d8\u5927\u3002\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6570\u5b57\u6765\u7cbe\u786e\u63a7\u5236\u5b57\u4f53\u5927\u5c0f\uff0c\u4f8b\u5982<code>fontsize=12<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bbe\u7f6e\u56fe\u4f8b\u6846\u7684\u5927\u5c0f<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u8c03\u6574\u5b57\u4f53\u5927\u5c0f\uff0c\u76f4\u63a5\u8bbe\u7f6e\u56fe\u4f8b\u6846\u7684\u5927\u5c0f\u4e5f\u662f\u4e00\u79cd\u65b9\u6cd5\u3002\u53ef\u4ee5\u901a\u8fc7<code>bbox_to_anchor<\/code>\u53c2\u6570\u6765\u5b9e\u73b0\uff0c\u8be5\u53c2\u6570\u5141\u8bb8\u4f60\u8bbe\u7f6e\u56fe\u4f8b\u6846\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [1, 4, 9, 16, 25]<\/p>\n<p>y2 = [1, 3, 6, 10, 15]<\/p>\n<p>plt.plot(x, y1, label=&#39;y = x^2&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;y = x + 2&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u56fe\u4f8b\u6846\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f<\/strong><\/h2>\n<p>plt.legend(bbox_to_anchor=(1.05, 1), loc=&#39;upper left&#39;, borderaxespad=0.)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>bbox_to_anchor<\/code>\u53c2\u6570\u88ab\u8bbe\u7f6e\u4e3a(1.05, 1)\uff0c\u8fd9\u4f1a\u5c06\u56fe\u4f8b\u6846\u8c03\u6574\u5230\u56fe\u7684\u53f3\u4e0a\u89d2\uff0c\u5e76\u4f7f\u5176\u5927\u5c0f\u5408\u9002\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8c03\u6574\u56fe\u4f8b\u7684\u900f\u660e\u5ea6<\/h3>\n<\/p>\n<p><p>\u900f\u660e\u5ea6\u4e5f\u662f\u4e00\u4e2a\u53ef\u4ee5\u5f71\u54cd\u56fe\u4f8b\u663e\u793a\u6548\u679c\u7684\u56e0\u7d20\u3002\u901a\u8fc7\u8bbe\u7f6e<code>framealpha<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u63a7\u5236\u56fe\u4f8b\u6846\u7684\u900f\u660e\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [1, 4, 9, 16, 25]<\/p>\n<p>y2 = [1, 3, 6, 10, 15]<\/p>\n<p>plt.plot(x, y1, label=&#39;y = x^2&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;y = x + 2&#39;)<\/p>\n<h2><strong>\u8c03\u6574\u56fe\u4f8b\u7684\u900f\u660e\u5ea6<\/strong><\/h2>\n<p>plt.legend(framealpha=0.5)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>framealpha<\/code>\u53c2\u6570\u88ab\u8bbe\u7f6e\u4e3a0.5\uff0c\u8fd9\u4f1a\u4f7f\u56fe\u4f8b\u6846\u53d8\u5f97\u534a\u900f\u660e\uff0c\u4ece\u800c\u5f71\u54cd\u56fe\u4f8b\u7684\u89c6\u89c9\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528bbox_transform\u8c03\u6574\u56fe\u4f8b\u6846\u7684\u5927\u5c0f<\/h3>\n<\/p>\n<p><p><code>bbox_transform<\/code>\u662f\u4e00\u4e2a\u66f4\u9ad8\u7ea7\u7684\u53c2\u6570\uff0c\u5b83\u5141\u8bb8\u4f60\u901a\u8fc7\u53d8\u6362\u6765\u8c03\u6574\u56fe\u4f8b\u6846\u7684\u5927\u5c0f\u548c\u4f4d\u7f6e\u3002\u8fd9\u4e2a\u53c2\u6570\u53ef\u4ee5\u4e0e<code>bbox_to_anchor<\/code>\u7ed3\u5408\u4f7f\u7528\uff0c\u4ee5\u5b9e\u73b0\u66f4\u590d\u6742\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>from matplotlib.transforms import Bbox<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [1, 4, 9, 16, 25]<\/p>\n<p>y2 = [1, 3, 6, 10, 15]<\/p>\n<p>plt.plot(x, y1, label=&#39;y = x^2&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;y = x + 2&#39;)<\/p>\n<h2><strong>\u4f7f\u7528bbox_transform\u8c03\u6574\u56fe\u4f8b\u6846\u7684\u5927\u5c0f<\/strong><\/h2>\n<p>bbox = Bbox.from_extents(0, 0, 0.5, 0.5)<\/p>\n<p>plt.legend(bbox_to_anchor=bbox, bbox_transform=plt.gcf().transFigure)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>Bbox.from_extents<\/code>\u88ab\u7528\u6765\u521b\u5efa\u4e00\u4e2aBbox\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5b9a\u4e49\u4e86\u56fe\u4f8b\u6846\u7684\u5927\u5c0f\u548c\u4f4d\u7f6e\u3002<code>bbox_transform=plt.gcf().transFigure<\/code>\u786e\u4fdd\u53d8\u6362\u5e94\u7528\u5230\u5f53\u524d\u56fe\u5f62\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u8bbe\u7f6e\u56fe\u4f8b\u7684\u8fb9\u6846\u548c\u80cc\u666f\u989c\u8272<\/h3>\n<\/p>\n<p><p>\u56fe\u4f8b\u7684\u8fb9\u6846\u548c\u80cc\u666f\u989c\u8272\u4e5f\u662f\u53ef\u4ee5\u8c03\u6574\u7684\uff0c\u8fd9\u4e9b\u53c2\u6570\u53ef\u4ee5\u901a\u8fc7<code>edgecolor<\/code>\u548c<code>facecolor<\/code>\u6765\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [1, 4, 9, 16, 25]<\/p>\n<p>y2 = [1, 3, 6, 10, 15]<\/p>\n<p>plt.plot(x, y1, label=&#39;y = x^2&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;y = x + 2&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u56fe\u4f8b\u7684\u8fb9\u6846\u548c\u80cc\u666f\u989c\u8272<\/strong><\/h2>\n<p>plt.legend(edgecolor=&#39;red&#39;, facecolor=&#39;lightgray&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>edgecolor<\/code>\u88ab\u8bbe\u7f6e\u4e3a&#39;red&#39;\uff0c<code>facecolor<\/code>\u88ab\u8bbe\u7f6e\u4e3a&#39;lightgray&#39;\uff0c\u8fd9\u4f1a\u6539\u53d8\u56fe\u4f8b\u7684\u8fb9\u6846\u548c\u80cc\u666f\u989c\u8272\uff0c\u4ece\u800c\u5f71\u54cd\u56fe\u4f8b\u7684\u89c6\u89c9\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528prop\u53c2\u6570\u8bbe\u7f6e\u56fe\u4f8b\u5c5e\u6027<\/h3>\n<\/p>\n<p><p><code>prop<\/code>\u53c2\u6570\u5141\u8bb8\u4f60\u4f20\u9012\u4e00\u4e2a\u5b57\u5178\u6765\u8bbe\u7f6e\u56fe\u4f8b\u7684\u5404\u79cd\u5c5e\u6027\uff0c\u5305\u62ec\u5b57\u4f53\u5927\u5c0f\u3001\u5b57\u4f53\u989c\u8272\u3001\u5b57\u4f53\u6837\u5f0f\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [1, 4, 9, 16, 25]<\/p>\n<p>y2 = [1, 3, 6, 10, 15]<\/p>\n<p>plt.plot(x, y1, label=&#39;y = x^2&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;y = x + 2&#39;)<\/p>\n<h2><strong>\u4f7f\u7528prop\u53c2\u6570\u8bbe\u7f6e\u56fe\u4f8b\u5c5e\u6027<\/strong><\/h2>\n<p>plt.legend(prop={&#39;size&#39;: 12, &#39;weight&#39;: &#39;bold&#39;, &#39;family&#39;: &#39;serif&#39;})<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>prop<\/code>\u53c2\u6570\u88ab\u7528\u6765\u8bbe\u7f6e\u56fe\u4f8b\u7684\u5b57\u4f53\u5927\u5c0f\u3001\u5b57\u4f53\u52a0\u7c97\u548c\u5b57\u4f53\u5bb6\u65cf\uff0c\u4ece\u800c\u5f71\u54cd\u56fe\u4f8b\u7684\u663e\u793a\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528rcParams\u5168\u5c40\u8bbe\u7f6e\u56fe\u4f8b\u5c5e\u6027<\/h3>\n<\/p>\n<p><p><code>rcParams<\/code>\u662fMatplotlib\u7684\u5168\u5c40\u914d\u7f6e\u53c2\u6570\uff0c\u901a\u8fc7\u8bbe\u7f6e<code>rcParams<\/code>\uff0c\u4f60\u53ef\u4ee5\u5168\u5c40\u8c03\u6574\u56fe\u4f8b\u7684\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u5168\u5c40\u8bbe\u7f6e\u56fe\u4f8b\u5b57\u4f53\u5927\u5c0f<\/strong><\/h2>\n<p>plt.rcParams[&#39;legend.fontsize&#39;] = &#39;large&#39;<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [1, 4, 9, 16, 25]<\/p>\n<p>y2 = [1, 3, 6, 10, 15]<\/p>\n<p>plt.plot(x, y1, label=&#39;y = x^2&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;y = x + 2&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>rcParams[&#39;legend.fontsize&#39;]<\/code>\u88ab\u8bbe\u7f6e\u4e3a&#39;large&#39;\uff0c\u8fd9\u4f1a\u5168\u5c40\u8c03\u6574\u6240\u6709\u56fe\u7684\u56fe\u4f8b\u5b57\u4f53\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528handles\u548clabels\u53c2\u6570\u81ea\u5b9a\u4e49\u56fe\u4f8b<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4f7f\u7528<code>handles<\/code>\u548c<code>labels<\/code>\u53c2\u6570\uff0c\u4f60\u53ef\u4ee5\u5b8c\u5168\u81ea\u5b9a\u4e49\u56fe\u4f8b\u7684\u5185\u5bb9\u548c\u663e\u793a\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [1, 4, 9, 16, 25]<\/p>\n<p>y2 = [1, 3, 6, 10, 15]<\/p>\n<p>line1, = plt.plot(x, y1, label=&#39;y = x^2&#39;)<\/p>\n<p>line2, = plt.plot(x, y2, label=&#39;y = x + 2&#39;)<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49\u56fe\u4f8b<\/strong><\/h2>\n<p>plt.legend(handles=[line1, line2], labels=[&#39;Square&#39;, &#39;Linear&#39;], fontsize=&#39;medium&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>handles<\/code>\u548c<code>labels<\/code>\u53c2\u6570\u88ab\u7528\u6765\u81ea\u5b9a\u4e49\u56fe\u4f8b\u7684\u5185\u5bb9\u548c\u663e\u793a\u6548\u679c\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u7684\u56fe\u4f8b\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528legend\u7684loc\u53c2\u6570\u8bbe\u7f6e\u56fe\u4f8b\u4f4d\u7f6e<\/h3>\n<\/p>\n<p><p><code>loc<\/code>\u53c2\u6570\u5141\u8bb8\u4f60\u5feb\u901f\u8bbe\u7f6e\u56fe\u4f8b\u7684\u4f4d\u7f6e\uff0c\u4f8b\u5982&#39;best&#39;\u3001&#39;upper right&#39;\u3001&#39;lower left&#39;\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [1, 4, 9, 16, 25]<\/p>\n<p>y2 = [1, 3, 6, 10, 15]<\/p>\n<p>plt.plot(x, y1, label=&#39;y = x^2&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;y = x + 2&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u56fe\u4f8b\u4f4d\u7f6e<\/strong><\/h2>\n<p>plt.legend(loc=&#39;upper right&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>loc<\/code>\u53c2\u6570\u88ab\u8bbe\u7f6e\u4e3a&#39;upper right&#39;\uff0c\u8fd9\u4f1a\u5c06\u56fe\u4f8b\u653e\u7f6e\u5728\u56fe\u7684\u53f3\u4e0a\u89d2\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528ncol\u53c2\u6570\u8bbe\u7f6e\u56fe\u4f8b\u5217\u6570<\/h3>\n<\/p>\n<p><p><code>ncol<\/code>\u53c2\u6570\u5141\u8bb8\u4f60\u8bbe\u7f6e\u56fe\u4f8b\u7684\u5217\u6570\uff0c\u4ece\u800c\u5f71\u54cd\u56fe\u4f8b\u7684\u5e03\u5c40\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [1, 4, 9, 16, 25]<\/p>\n<p>y2 = [1, 3, 6, 10, 15]<\/p>\n<p>plt.plot(x, y1, label=&#39;y = x^2&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;y = x + 2&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u56fe\u4f8b\u5217\u6570<\/strong><\/h2>\n<p>plt.legend(ncol=2)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>ncol<\/code>\u53c2\u6570\u88ab\u8bbe\u7f6e\u4e3a2\uff0c\u8fd9\u4f1a\u5c06\u56fe\u4f8b\u5206\u4e3a\u4e24\u5217\uff0c\u4ece\u800c\u5f71\u54cd\u56fe\u4f8b\u7684\u5e03\u5c40\u548c\u663e\u793a\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528frameon\u53c2\u6570\u63a7\u5236\u56fe\u4f8b\u6846\u7684\u663e\u793a<\/h3>\n<\/p>\n<p><p><code>frameon<\/code>\u53c2\u6570\u5141\u8bb8\u4f60\u63a7\u5236\u662f\u5426\u663e\u793a\u56fe\u4f8b\u6846\uff0c\u901a\u8fc7\u8bbe\u7f6e<code>frameon<\/code>\u4e3aFalse\uff0c\u53ef\u4ee5\u9690\u85cf\u56fe\u4f8b\u6846\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [1, 4, 9, 16, 25]<\/p>\n<p>y2 = [1, 3, 6, 10, 15]<\/p>\n<p>plt.plot(x, y1, label=&#39;y = x^2&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;y = x + 2&#39;)<\/p>\n<h2><strong>\u9690\u85cf\u56fe\u4f8b\u6846<\/strong><\/h2>\n<p>plt.legend(frameon=False)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>frameon<\/code>\u53c2\u6570\u88ab\u8bbe\u7f6e\u4e3aFalse\uff0c\u8fd9\u4f1a\u9690\u85cf\u56fe\u4f8b\u6846\uff0c\u4ece\u800c\u4f7f\u56fe\u4f8b\u770b\u8d77\u6765\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528title\u53c2\u6570\u4e3a\u56fe\u4f8b\u6dfb\u52a0\u6807\u9898<\/h3>\n<\/p>\n<p><p><code>title<\/code>\u53c2\u6570\u5141\u8bb8\u4f60\u4e3a\u56fe\u4f8b\u6dfb\u52a0\u6807\u9898\uff0c\u4ece\u800c\u63d0\u4f9b\u66f4\u591a\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [1, 4, 9, 16, 25]<\/p>\n<p>y2 = [1, 3, 6, 10, 15]<\/p>\n<p>plt.plot(x, y1, label=&#39;y = x^2&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;y = x + 2&#39;)<\/p>\n<h2><strong>\u4e3a\u56fe\u4f8b\u6dfb\u52a0\u6807\u9898<\/strong><\/h2>\n<p>plt.legend(title=&#39;Equations&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>title<\/code>\u53c2\u6570\u88ab\u7528\u6765\u4e3a\u56fe\u4f8b\u6dfb\u52a0\u6807\u9898\uff0c\u4ece\u800c\u63d0\u4f9b\u66f4\u591a\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u4f7f\u7528scatter\u51fd\u6570\u7ed8\u5236\u5e26\u6709\u56fe\u4f8b\u7684\u6563\u70b9\u56fe<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u6563\u70b9\u56fe\u65f6\uff0c\u4f60\u540c\u6837\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u6765\u63a7\u5236\u56fe\u4f8b\u7684\u5927\u5c0f\u548c\u663e\u793a\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [1, 4, 9, 16, 25]<\/p>\n<p>y2 = [1, 3, 6, 10, 15]<\/p>\n<p>plt.scatter(x, y1, label=&#39;y = x^2&#39;)<\/p>\n<p>plt.scatter(x, y2, label=&#39;y = x + 2&#39;)<\/p>\n<h2><strong>\u8c03\u6574\u56fe\u4f8b\u5b57\u4f53\u5927\u5c0f<\/strong><\/h2>\n<p>plt.legend(fontsize=&#39;large&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>scatter<\/code>\u51fd\u6570\u88ab\u7528\u6765\u7ed8\u5236\u6563\u70b9\u56fe\uff0c\u5e76\u4e14\u901a\u8fc7\u8bbe\u7f6e<code>fontsize<\/code>\u53c2\u6570\u6765\u8c03\u6574\u56fe\u4f8b\u5b57\u4f53\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u56db\u3001\u4f7f\u7528legend_handles_labels\u65b9\u6cd5\u83b7\u53d6\u56fe\u4f8b\u4fe1\u606f<\/h3>\n<\/p>\n<p><p><code>legend_handles_labels<\/code>\u65b9\u6cd5\u5141\u8bb8\u4f60\u83b7\u53d6\u56fe\u4f8b\u7684\u53e5\u67c4\u548c\u6807\u7b7e\uff0c\u4ece\u800c\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5b9a\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [1, 4, 9, 16, 25]<\/p>\n<p>y2 = [1, 3, 6, 10, 15]<\/p>\n<p>plt.plot(x, y1, label=&#39;y = x^2&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;y = x + 2&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u56fe\u4f8b\u7684\u53e5\u67c4\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>handles, labels = plt.gca().get_legend_handles_labels()<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49\u56fe\u4f8b<\/strong><\/h2>\n<p>plt.legend(handles, labels, fontsize=&#39;medium&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>get_legend_handles_labels<\/code>\u65b9\u6cd5\u88ab\u7528\u6765\u83b7\u53d6\u56fe\u4f8b\u7684\u53e5\u67c4\u548c\u6807\u7b7e\uff0c\u7136\u540e\u901a\u8fc7<code>plt.legend<\/code>\u65b9\u6cd5\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u5b9a\u5236\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u4e94\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u51fd\u6570\u8c03\u6574\u56fe\u4f8b<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u8c03\u6574\u56fe\u4f8b\u7684\u663e\u793a\u6548\u679c\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u52a0\u590d\u6742\u548c\u7075\u6d3b\u7684\u56fe\u4f8b\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [1, 4, 9, 16, 25]<\/p>\n<p>y2 = [1, 3, 6, 10, 15]<\/p>\n<p>plt.plot(x, y1, label=&#39;y = x^2&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;y = x + 2&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u81ea\u5b9a\u4e49\u51fd\u6570\u8c03\u6574\u56fe\u4f8b<\/strong><\/h2>\n<p>def custom_legend(ax, fontsize, loc):<\/p>\n<p>    ax.legend(fontsize=fontsize, loc=loc)<\/p>\n<h2><strong>\u8c03\u7528\u81ea\u5b9a\u4e49\u51fd\u6570<\/strong><\/h2>\n<p>custom_legend(plt.gca(), &#39;large&#39;, &#39;upper left&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>custom_legend<\/code>\u7684\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5141\u8bb8\u4f60\u4f20\u9012\u8f74\u5bf9\u8c61\u3001\u5b57\u4f53\u5927\u5c0f\u548c\u4f4d\u7f6e\u53c2\u6570\u6765\u8c03\u6574\u56fe\u4f8b\u7684\u663e\u793a\u6548\u679c\u3002\u901a\u8fc7\u8c03\u7528\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u5b9e\u73b0\u66f4\u52a0\u7075\u6d3b\u7684\u56fe\u4f8b\u63a7\u5236\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u63a7\u5236\u56fe\u4f8b\u7684\u5927\u5c0f\u548c\u663e\u793a\u6548\u679c\uff0c\u5305\u62ec\u8c03\u6574\u56fe\u4f8b\u5b57\u4f53\u5927\u5c0f\u3001\u8bbe\u7f6e\u56fe\u4f8b\u6846\u7684\u5927\u5c0f\u3001\u4f7f\u7528<code>bbox_to_anchor<\/code>\u8c03\u6574\u56fe\u4f8b\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\u3001\u8bbe\u7f6e\u56fe\u4f8b\u7684\u900f\u660e\u5ea6\u7b49\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5b9e\u73b0\u5404\u79cd\u590d\u6742\u7684\u56fe\u4f8b\u5b9a\u5236\uff0c\u4ece\u800c\u4f7f\u4f60\u7684\u56fe\u5f62\u66f4\u52a0\u7f8e\u89c2\u548c\u4e13\u4e1a\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u8c03\u6574Python\u56fe\u4f8b\u7684\u5b57\u4f53\u5927\u5c0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u56fe\u8868\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>fontsize<\/code>\u53c2\u6570\u6765\u8c03\u6574\u56fe\u4f8b\u7684\u5b57\u4f53\u5927\u5c0f\u3002\u4f8b\u5982\uff0c\u5728\u8c03\u7528<code>plt.legend()<\/code>\u65f6\uff0c\u60a8\u53ef\u4ee5\u4f20\u5165<code>fontsize=12<\/code>\uff0c\u8fd9\u5c06\u4f7f\u56fe\u4f8b\u7684\u5b57\u4f53\u5927\u5c0f\u4e3a12\u53f7\u3002\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u5b57\u4f53\u5927\u5c0f\uff0c\u4ee5\u63d0\u5347\u56fe\u8868\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u901a\u8fc7\u4ee3\u7801\u52a8\u6001\u8c03\u6574\u56fe\u4f8b\u7684\u5927\u5c0f\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u60a8\u53ef\u4ee5\u6839\u636e\u56fe\u8868\u7684\u5927\u5c0f\u6216\u6570\u636e\u7684\u53d8\u5316\u52a8\u6001\u8c03\u6574\u56fe\u4f8b\u7684\u5927\u5c0f\u3002\u901a\u8fc7\u8bbe\u7f6e<code>bbox_to_anchor<\/code>\u53c2\u6570\uff0c\u53ef\u4ee5\u63a7\u5236\u56fe\u4f8b\u7684\u4f4d\u7f6e\uff0c\u800c<code>prop<\/code>\u53c2\u6570\u5219\u53ef\u4ee5\u7528\u4e8e\u8bbe\u7f6e\u56fe\u4f8b\u7684\u5b57\u4f53\u5927\u5c0f\u3002\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u6839\u636e\u9700\u6c42\u6539\u53d8\u56fe\u4f8b\u7684\u663e\u793a\u6548\u679c\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Seaborn\u65f6\uff0c\u5982\u4f55\u63a7\u5236\u56fe\u4f8b\u7684\u5927\u5c0f\uff1f<\/strong><br \/>Seaborn\u662f\u5efa\u7acb\u5728Matplotlib\u57fa\u7840\u4e4b\u4e0a\u7684\u5e93\uff0c\u56e0\u6b64\u56fe\u4f8b\u7684\u63a7\u5236\u65b9\u5f0f\u4e0eMatplotlib\u76f8\u4f3c\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>plt.legend()<\/code>\u6765\u8bbe\u7f6e\u56fe\u4f8b\u7684\u5c5e\u6027\u3002Seaborn\u8fd8\u6709\u4e00\u4e9b\u5185\u7f6e\u7684\u6837\u5f0f\u9009\u9879\uff0c\u53ef\u4ee5\u901a\u8fc7<code>legend<\/code>\u53c2\u6570\u81ea\u5b9a\u4e49\u56fe\u4f8b\u7684\u5916\u89c2\uff0c\u5305\u62ec\u989c\u8272\u548c\u5b57\u4f53\u5927\u5c0f\u7b49\uff0c\u786e\u4fdd\u56fe\u4f8b\u4e0e\u6574\u4f53\u56fe\u8868\u7684\u98ce\u683c\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u63a7\u5236\u56fe\u4f8b\u5927\u5c0f\u7684\u51e0\u79cd\u65b9\u6cd5\u5305\u62ec\uff1a\u8c03\u6574\u56fe\u4f8b\u5b57\u4f53\u5927\u5c0f\u3001\u8bbe\u7f6e\u56fe\u4f8b\u6846\u7684\u5927\u5c0f\u3001\u4f7f\u7528bbox_to_anch [&hellip;]","protected":false},"author":3,"featured_media":1156075,"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\/1156064"}],"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=1156064"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156064\/revisions"}],"predecessor-version":[{"id":1156078,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156064\/revisions\/1156078"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1156075"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1156064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1156064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1156064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}