{"id":1035694,"date":"2024-12-31T12:00:59","date_gmt":"2024-12-31T04:00:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1035694.html"},"modified":"2024-12-31T12:01:01","modified_gmt":"2024-12-31T04:01:01","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e7%bb%98%e5%88%b6%e5%9b%be%e4%b8%ad%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1035694.html","title":{"rendered":"python\u5982\u4f55\u5728\u7ed8\u5236\u56fe\u4e2d\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/66787dca-802b-47d7-b5fa-40e14114be4f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5728\u7ed8\u5236\u56fe\u4e2d\u56fe\" \/><\/p>\n<p><p> <strong>Python\u5728\u7ed8\u5236\u56fe\u4e2d\u56fe\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528matplotlib\u5e93\u3001\u6dfb\u52a0\u5b50\u56fe\u3001\u8c03\u6574\u5b50\u56fe\u4f4d\u7f6e<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528matplotlib\u5e93<\/strong> \u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u4e3b\u56fe\u548c\u4e00\u4e2a\u5d4c\u5957\u7684\u5b50\u56fe\u6765\u5b9e\u73b0\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u56fe\u4e2d\u56fe\uff0c\u4ee5\u53ca\u5982\u4f55\u8fdb\u884c\u76f8\u5e94\u7684\u8c03\u6574\u548c\u7f8e\u5316\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528matplotlib\u5e93<\/p>\n<\/p>\n<p><p>matplotlib\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684Python\u5e93\uff0c\u7528\u4e8e\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002\u5b83\u63d0\u4f9b\u4e86\u975e\u5e38\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u7b80\u5355\u7684\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u997c\u56fe\u7b49\uff0c\u4e5f\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u590d\u6742\u7684\u56fe\u4e2d\u56fe\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528matplotlib\u5e93\u6765\u7ed8\u5236\u56fe\u4e2d\u56fe\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u5b89\u88c5matplotlib\u5e93<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5matplotlib\u5e93\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u521b\u5efa\u4e3b\u56fe\u548c\u5b50\u56fe<\/p>\n<\/p>\n<p><p>\u5728matplotlib\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>figure<\/code>\u548c<code>add_axes<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e3b\u56fe\u548c\u5b50\u56fe\u3002<code>figure<\/code>\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u5f62\u5bf9\u8c61\uff0c\u800c<code>add_axes<\/code>\u51fd\u6570\u7528\u4e8e\u5728\u56fe\u5f62\u5bf9\u8c61\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u5750\u6807\u8f74\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>fig = plt.figure()<\/p>\n<h2><strong>\u6dfb\u52a0\u4e3b\u56fe<\/strong><\/h2>\n<p>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])<\/p>\n<p>main_ax.plot([1, 2, 3, 4], [10, 20, 25, 30])<\/p>\n<p>main_ax.set_title(&#39;Main Plot&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u5b50\u56fe<\/strong><\/h2>\n<p>inset_ax = fig.add_axes([0.6, 0.6, 0.25, 0.25])<\/p>\n<p>inset_ax.plot([1, 2, 3, 4], [30, 25, 20, 10])<\/p>\n<p>inset_ax.set_title(&#39;Inset Plot&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/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\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684\u56fe\u5f62\u5bf9\u8c61<code>fig<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>add_axes<\/code>\u51fd\u6570\u6dfb\u52a0\u4e86\u4e00\u4e2a\u4e3b\u56fe<code>main_ax<\/code>\u548c\u4e00\u4e2a\u5b50\u56fe<code>inset_ax<\/code>\u3002<code>add_axes<\/code>\u51fd\u6570\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u5305\u542b\u56db\u4e2a\u5143\u7d20\u7684\u5217\u8868\uff0c\u5206\u522b\u8868\u793a\u5b50\u56fe\u7684\u5de6\u3001\u4e0b\u3001\u5bbd\u3001\u9ad8\u7684\u767e\u5206\u6bd4\u4f4d\u7f6e\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>plot<\/code>\u51fd\u6570\u7ed8\u5236\u4e86\u4e3b\u56fe\u548c\u5b50\u56fe\uff0c\u5e76\u4f7f\u7528<code>set_title<\/code>\u51fd\u6570\u8bbe\u7f6e\u4e86\u5b83\u4eec\u7684\u6807\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u6dfb\u52a0\u5b50\u56fe<\/p>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u56fe\u4e2d\u56fe\u65f6\uff0c\u5b50\u56fe\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574<code>add_axes<\/code>\u51fd\u6570\u7684\u53c2\u6570\u6765\u63a7\u5236\u5b50\u56fe\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u8c03\u6574\u6280\u5de7\uff1a<\/p>\n<\/p>\n<p><p>1\u3001\u8c03\u6574\u5b50\u56fe\u7684\u4f4d\u7f6e<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8c03\u6574<code>add_axes<\/code>\u51fd\u6570\u53c2\u6570\u4e2d\u7684\u524d\u4e24\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u63a7\u5236\u5b50\u56fe\u5728\u4e3b\u56fe\u4e2d\u7684\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u5c06\u5b50\u56fe\u79fb\u52a8\u5230\u4e3b\u56fe\u7684\u53f3\u4e0a\u89d2\uff0c\u53ef\u4ee5\u5c06<code>add_axes<\/code>\u51fd\u6570\u7684\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>[0.7, 0.7, 0.25, 0.25]<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">inset_ax = fig.add_axes([0.7, 0.7, 0.25, 0.25])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u8c03\u6574\u5b50\u56fe\u7684\u5927\u5c0f<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8c03\u6574<code>add_axes<\/code>\u51fd\u6570\u53c2\u6570\u4e2d\u7684\u540e\u4e24\u4e2a\u5143\u7d20\uff0c\u53ef\u4ee5\u63a7\u5236\u5b50\u56fe\u7684\u5927\u5c0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u5c06\u5b50\u56fe\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u90fd\u7f29\u5c0f\u4e00\u534a\uff0c\u53ef\u4ee5\u5c06<code>add_axes<\/code>\u51fd\u6570\u7684\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>[0.6, 0.6, 0.125, 0.125]<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">inset_ax = fig.add_axes([0.6, 0.6, 0.125, 0.125])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u8c03\u6574\u5b50\u56fe\u4f4d\u7f6e<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u6839\u636e\u6570\u636e\u7684\u7279\u70b9\u548c\u56fe\u5f62\u7684\u5e03\u5c40\u6765\u7cbe\u786e\u8c03\u6574\u5b50\u56fe\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u8c03\u6574\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p>1\u3001\u4f7f\u7528<code>transforms<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p>matplotlib\u7684<code>transforms<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u7cbe\u786e\u8c03\u6574\u56fe\u5f62\u5bf9\u8c61\u7684\u4f4d\u7f6e\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>transforms.Bbox<\/code>\u7c7b\u6765\u5b9a\u4e49\u4e00\u4e2a\u8fb9\u754c\u6846\uff0c\u7136\u540e\u5c06\u5b50\u56fe\u6dfb\u52a0\u5230\u8fd9\u4e2a\u8fb9\u754c\u6846\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.transforms as transforms<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>fig = plt.figure()<\/p>\n<h2><strong>\u6dfb\u52a0\u4e3b\u56fe<\/strong><\/h2>\n<p>main_ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])<\/p>\n<p>main_ax.plot([1, 2, 3, 4], [10, 20, 25, 30])<\/p>\n<p>main_ax.set_title(&#39;Main Plot&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u8fb9\u754c\u6846<\/strong><\/h2>\n<p>bbox = transforms.Bbox([[0.6, 0.6], [0.85, 0.85]])<\/p>\n<h2><strong>\u6dfb\u52a0\u5b50\u56fe<\/strong><\/h2>\n<p>inset_ax = fig.add_axes(bbox)<\/p>\n<p>inset_ax.plot([1, 2, 3, 4], [30, 25, 20, 10])<\/p>\n<p>inset_ax.set_title(&#39;Inset Plot&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/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\u9996\u5148\u4f7f\u7528<code>transforms.Bbox<\/code>\u7c7b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u8fb9\u754c\u6846<code>bbox<\/code>\uff0c\u7136\u540e\u5c06\u5b50\u56fe\u6dfb\u52a0\u5230\u8fd9\u4e2a\u8fb9\u754c\u6846\u4e2d\u3002\u8fd9\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u8c03\u6574\u5b50\u56fe\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><p>2\u3001\u4f7f\u7528<code>inset_axes<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p>matplotlib\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e2a<code>inset_axes<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u7b80\u5316\u5b50\u56fe\u7684\u6dfb\u52a0\u548c\u8c03\u6574\u8fc7\u7a0b\u3002<code>inset_axes<\/code>\u51fd\u6570\u7684\u53c2\u6570\u4e0e<code>add_axes<\/code>\u51fd\u6570\u76f8\u4f3c\uff0c\u4f46\u662f\u5b83\u53ef\u4ee5\u81ea\u52a8\u8c03\u6574\u5b50\u56fe\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\uff0c\u4ee5\u9002\u5e94\u4e3b\u56fe\u7684\u5e03\u5c40\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mpl_toolkits.axes_grid1.inset_locator import inset_axes<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>fig = plt.figure()<\/p>\n<h2><strong>\u6dfb\u52a0\u4e3b\u56fe<\/strong><\/h2>\n<p>main_ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])<\/p>\n<p>main_ax.plot([1, 2, 3, 4], [10, 20, 25, 30])<\/p>\n<p>main_ax.set_title(&#39;Main Plot&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u5b50\u56fe<\/strong><\/h2>\n<p>inset_ax = inset_axes(main_ax, width=&quot;30%&quot;, height=&quot;30%&quot;, loc=1)<\/p>\n<p>inset_ax.plot([1, 2, 3, 4], [30, 25, 20, 10])<\/p>\n<p>inset_ax.set_title(&#39;Inset Plot&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/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>inset_axes<\/code>\u51fd\u6570\u5728\u4e3b\u56fe<code>main_ax<\/code>\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u5b50\u56fe\u3002<code>inset_axes<\/code>\u51fd\u6570\u7684\u53c2\u6570\u5305\u62ec\u5b50\u56fe\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff08\u53ef\u4ee5\u662f\u767e\u5206\u6bd4\u5f62\u5f0f\uff09\uff0c\u4ee5\u53ca\u5b50\u56fe\u7684\u4f4d\u7f6e\uff08\u4f7f\u7528<code>loc<\/code>\u53c2\u6570\uff0c\u503c\u4e3a1\u52309\uff0c\u8868\u793a\u4e0d\u540c\u7684\u65b9\u4f4d\uff09\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5b50\u56fe\u7684\u7f8e\u5316<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u8c03\u6574\u5b50\u56fe\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e9b\u7f8e\u5316\u6280\u5de7\u6765\u63d0\u9ad8\u56fe\u4e2d\u56fe\u7684\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u6027\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u7f8e\u5316\u6280\u5de7\uff1a<\/p>\n<\/p>\n<p><p>1\u3001\u8bbe\u7f6e\u5b50\u56fe\u7684\u80cc\u666f\u989c\u8272<\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>set_facecolor<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u5b50\u56fe\u7684\u80cc\u666f\u989c\u8272\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u5c06\u5b50\u56fe\u7684\u80cc\u666f\u989c\u8272\u8bbe\u7f6e\u4e3a\u6d45\u7070\u8272\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">inset_ax.set_facecolor(&#39;#f0f0f0&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u9690\u85cf\u5b50\u56fe\u7684\u5750\u6807\u8f74<\/p>\n<\/p>\n<p><p>\u5982\u679c\u5b50\u56fe\u4e2d\u7684\u5750\u6807\u8f74\u5bf9\u4e8e\u56fe\u5f62\u7684\u7406\u89e3\u6ca1\u6709\u5e2e\u52a9\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u9690\u85cf\u3002\u53ef\u4ee5\u4f7f\u7528<code>axis<\/code>\u51fd\u6570\u6765\u9690\u85cf\u5b50\u56fe\u7684\u5750\u6807\u8f74\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">inset_ax.axis(&#39;off&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3\u3001\u8bbe\u7f6e\u5b50\u56fe\u7684\u8fb9\u6846\u989c\u8272\u548c\u7ebf\u5bbd<\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>spines<\/code>\u5c5e\u6027\u6765\u8bbe\u7f6e\u5b50\u56fe\u7684\u8fb9\u6846\u989c\u8272\u548c\u7ebf\u5bbd\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u5c06\u5b50\u56fe\u7684\u8fb9\u6846\u989c\u8272\u8bbe\u7f6e\u4e3a\u7ea2\u8272\uff0c\u5e76\u5c06\u7ebf\u5bbd\u8bbe\u7f6e\u4e3a2\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for spine in inset_ax.spines.values():<\/p>\n<p>    spine.set_edgecolor(&#39;red&#39;)<\/p>\n<p>    spine.set_linewidth(2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>4\u3001\u6dfb\u52a0\u6ce8\u91ca\u548c\u7bad\u5934<\/p>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>annotate<\/code>\u51fd\u6570\u6765\u5728\u4e3b\u56fe\u548c\u5b50\u56fe\u4e2d\u6dfb\u52a0\u6ce8\u91ca\u548c\u7bad\u5934\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u5728\u4e3b\u56fe\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u6ce8\u91ca\uff0c\u5e76\u4f7f\u7528\u7bad\u5934\u6307\u5411\u5b50\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">main_ax.annotate(&#39;Inset Plot&#39;, xy=(3, 25), xytext=(2, 30),<\/p>\n<p>                 arrowprops=dict(facecolor=&#39;black&#39;, shrink=0.05))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5b9e\u6218\u793a\u4f8b<\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5b8c\u6574\u7684\u5b9e\u6218\u793a\u4f8b\u6765\u603b\u7ed3\u4e0a\u8ff0\u5185\u5bb9\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u7ec4\u6570\u636e\uff0c\u9700\u8981\u5728\u4e3b\u56fe\u4e2d\u7ed8\u5236\u6298\u7ebf\u56fe\uff0c\u5e76\u5728\u5b50\u56fe\u4e2d\u653e\u5927\u5176\u4e2d\u7684\u4e00\u90e8\u5206\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>from mpl_toolkits.axes_grid1.inset_locator import inset_axes<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>y = [10, 20, 25, 30, 35, 40, 45, 50, 55, 60]<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>fig = plt.figure()<\/p>\n<h2><strong>\u6dfb\u52a0\u4e3b\u56fe<\/strong><\/h2>\n<p>main_ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])<\/p>\n<p>main_ax.plot(x, y, label=&#39;Main Data&#39;)<\/p>\n<p>main_ax.set_title(&#39;Main Plot&#39;)<\/p>\n<p>main_ax.set_xlabel(&#39;X Axis&#39;)<\/p>\n<p>main_ax.set_ylabel(&#39;Y Axis&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u5b50\u56fe<\/strong><\/h2>\n<p>inset_ax = inset_axes(main_ax, width=&quot;30%&quot;, height=&quot;30%&quot;, loc=2)<\/p>\n<p>inset_ax.plot(x[2:6], y[2:6], label=&#39;Inset Data&#39;)<\/p>\n<p>inset_ax.set_title(&#39;Inset Plot&#39;)<\/p>\n<p>inset_ax.set_xlabel(&#39;X Axis&#39;)<\/p>\n<p>inset_ax.set_ylabel(&#39;Y Axis&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b50\u56fe\u7684\u80cc\u666f\u989c\u8272<\/strong><\/h2>\n<p>inset_ax.set_facecolor(&#39;#f0f0f0&#39;)<\/p>\n<h2><strong>\u9690\u85cf\u5b50\u56fe\u7684\u5750\u6807\u8f74<\/strong><\/h2>\n<p>inset_ax.axis(&#39;off&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5b50\u56fe\u7684\u8fb9\u6846\u989c\u8272\u548c\u7ebf\u5bbd<\/strong><\/h2>\n<p>for spine in inset_ax.spines.values():<\/p>\n<p>    spine.set_edgecolor(&#39;red&#39;)<\/p>\n<p>    spine.set_linewidth(2)<\/p>\n<h2><strong>\u6dfb\u52a0\u6ce8\u91ca\u548c\u7bad\u5934<\/strong><\/h2>\n<p>main_ax.annotate(&#39;Inset Plot&#39;, xy=(4, 30), xytext=(6, 50),<\/p>\n<p>                 arrowprops=dict(facecolor=&#39;black&#39;, shrink=0.05))<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u7ec4\u6570\u636e<code>x<\/code>\u548c<code>y<\/code>\uff0c\u5e76\u5728\u4e3b\u56fe\u4e2d\u7ed8\u5236\u4e86\u6298\u7ebf\u56fe\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>inset_axes<\/code>\u51fd\u6570\u6dfb\u52a0\u4e86\u4e00\u4e2a\u5b50\u56fe\uff0c\u5e76\u5728\u5b50\u56fe\u4e2d\u653e\u5927\u4e86\u4e3b\u56fe\u4e2d\u7684\u4e00\u90e8\u5206\u6570\u636e\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86\u5b50\u56fe\u7684\u80cc\u666f\u989c\u8272\u3001\u9690\u85cf\u4e86\u5b50\u56fe\u7684\u5750\u6807\u8f74\u3001\u8bbe\u7f6e\u4e86\u5b50\u56fe\u7684\u8fb9\u6846\u989c\u8272\u548c\u7ebf\u5bbd\uff0c\u6700\u540e\u5728\u4e3b\u56fe\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6ce8\u91ca\u548c\u7bad\u5934\u6307\u5411\u5b50\u56fe\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u7b80\u5355\u800c\u7f8e\u89c2\u7684\u56fe\u4e2d\u56fe\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u56fe\u4e2d\u56fe\u662f\u4e00\u9879\u975e\u5e38\u5b9e\u7528\u7684\u6280\u5de7\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5c55\u793a\u548c\u5206\u6790\u6570\u636e\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528matplotlib\u5e93\u521b\u5efa\u4e3b\u56fe\u548c\u5b50\u56fe\uff0c\u5982\u4f55\u8c03\u6574\u5b50\u56fe\u7684\u4f4d\u7f6e\u548c\u5927\u5c0f\uff0c\u4ee5\u53ca\u5982\u4f55\u7f8e\u5316\u5b50\u56fe\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u80fd\u591f\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u6280\u5de7\u6765\u521b\u5efa\u66f4\u52a0\u4e13\u4e1a\u548c\u7f8e\u89c2\u7684\u56fe\u8868\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u7ed8\u5236\u56fe\u4e2d\u56fe\u7684\u6700\u4f73\u5e93\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0cMatplotlib\u662f\u7ed8\u5236\u56fe\u4e2d\u56fe\u7684\u6700\u4f73\u5e93\u4e4b\u4e00\u3002\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u7528\u6237\u521b\u5efa\u590d\u6742\u7684\u591a\u5c42\u56fe\u5f62\u3002\u4f7f\u7528Matplotlib\u7684<code>subplot<\/code>\u6216<code>Axes<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u540c\u4e00\u56fe\u5f62\u4e2d\u6dfb\u52a0\u591a\u4e2a\u56fe\u5f62\uff0c\u5f62\u6210\u56fe\u4e2d\u56fe\u7684\u6548\u679c\u3002\u6b64\u5916\uff0cSeaborn\u548cPlotly\u7b49\u5e93\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\uff0c\u5c24\u5176\u662f\u5f53\u9700\u8981\u66f4\u52a0\u7f8e\u89c2\u6216\u4ea4\u4e92\u5f0f\u7684\u56fe\u5f62\u65f6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Matplotlib\u4e2d\u5b9e\u73b0\u56fe\u4e2d\u56fe\u7684\u6548\u679c\uff1f<\/strong><br \/>\u8981\u5728Matplotlib\u4e2d\u5b9e\u73b0\u56fe\u4e2d\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528<code>inset_axes<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u63d2\u5165\u7684\u5750\u6807\u8f74\u3002\u9996\u5148\uff0c\u7ed8\u5236\u4e3b\u56fe\uff0c\u7136\u540e\u901a\u8fc7<code>inset_axes<\/code>\u6dfb\u52a0\u65b0\u7684\u5750\u6807\u8f74\uff0c\u6307\u5b9a\u5176\u4f4d\u7f6e\u548c\u5927\u5c0f\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u5728\u8fd9\u4e2a\u65b0\u7684\u5750\u6807\u8f74\u4e0a\u7ed8\u5236\u7b2c\u4e8c\u4e2a\u56fe\u5f62\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5728\u4e00\u4e2a\u56fe\u4e2d\u6dfb\u52a0\u53e6\u4e00\u4e2a\u5c0f\u56fe\uff0c\u63d0\u4f9b\u66f4\u8be6\u7ec6\u7684\u89c6\u56fe\u6216\u6bd4\u8f83\u6570\u636e\u3002<\/p>\n<p><strong>\u5728\u56fe\u4e2d\u56fe\u4e2d\u5982\u4f55\u81ea\u5b9a\u4e49\u6837\u5f0f\u548c\u6807\u7b7e\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u56fe\u4e2d\u56fe\u65f6\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u6837\u5f0f\u548c\u6807\u7b7e\u4ee5\u589e\u5f3a\u53ef\u8bfb\u6027\u3002\u4f7f\u7528Matplotlib\u7684<code>set_title<\/code>\u3001<code>set_xlabel<\/code>\u548c<code>set_ylabel<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4e3a\u4e3b\u56fe\u548c\u63d2\u5165\u56fe\u5206\u522b\u8bbe\u7f6e\u6807\u9898\u548c\u5750\u6807\u8f74\u6807\u7b7e\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528<code>legend<\/code>\u65b9\u6cd5\u6dfb\u52a0\u56fe\u4f8b\uff0c\u4ee5\u5e2e\u52a9\u89e3\u91ca\u6570\u636e\u3002\u901a\u8fc7\u8c03\u6574\u989c\u8272\u3001\u7ebf\u578b\u548c\u6807\u8bb0\u6837\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u56fe\u4e2d\u56fe\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u4e8e\u7406\u89e3\uff0c\u786e\u4fdd\u89c2\u4f17\u80fd\u591f\u8f7b\u677e\u83b7\u53d6\u5173\u952e\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5728\u7ed8\u5236\u56fe\u4e2d\u56fe\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528matplotlib\u5e93\u3001\u6dfb\u52a0\u5b50\u56fe\u3001\u8c03\u6574\u5b50\u56fe\u4f4d\u7f6e\u3002\u5176\u4e2d\uff0c\u4f7f\u7528matplot [&hellip;]","protected":false},"author":3,"featured_media":1035702,"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\/1035694"}],"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=1035694"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035694\/revisions"}],"predecessor-version":[{"id":1035704,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035694\/revisions\/1035704"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1035702"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1035694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1035694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1035694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}