{"id":1187672,"date":"2025-01-15T20:08:18","date_gmt":"2025-01-15T12:08:18","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1187672.html"},"modified":"2025-01-15T20:08:20","modified_gmt":"2025-01-15T12:08:20","slug":"python%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e5%9d%90%e6%a0%87%e8%bd%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1187672.html","title":{"rendered":"python\u5982\u4f55\u8868\u793a\u5750\u6807\u8f74"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25140201\/f5f680b6-ff6f-473d-8722-a961aec6faa7.webp\" alt=\"python\u5982\u4f55\u8868\u793a\u5750\u6807\u8f74\" \/><\/p>\n<p><p> <strong>Python\u8868\u793a\u5750\u6807\u8f74\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528Matplotlib\u5e93\u3001Plotly\u5e93\u3001Seaborn\u5e93\u7b49\u3002<\/strong>\u5176\u4e2d\uff0cMatplotlib\u662f\u6700\u5e38\u7528\u7684\u5e93\uff0c\u56e0\u4e3a\u5b83\u529f\u80fd\u5f3a\u5927\u4e14\u6613\u4e8e\u4f7f\u7528\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Matplotlib\u8868\u793a\u5750\u6807\u8f74\uff0c\u5e76\u5bf9\u8fd9\u4e00\u65b9\u6cd5\u8fdb\u884c\u8be6\u7ec6\u63cf\u8ff0\u3002<\/p>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u7ed8\u56fe\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ed8\u56fe\u529f\u80fd\uff0c\u53ef\u4ee5\u7ed8\u5236\u51fa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u5f62\u3002\u4f7f\u7528Matplotlib\u8868\u793a\u5750\u6807\u8f74\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a\u9996\u5148\uff0c\u5bfc\u5165Matplotlib\u5e93\uff1b\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61\u548c\u4e00\u4e2a\u8f74\u5bf9\u8c61\uff1b\u63a5\u7740\uff0c\u4f7f\u7528\u8f74\u5bf9\u8c61\u7684\u65b9\u6cd5\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6807\u7b7e\u3001\u523b\u5ea6\u3001\u8303\u56f4\u7b49\u5c5e\u6027\uff1b\u6700\u540e\uff0c\u663e\u793a\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6807\u7b7e<\/strong><\/h2>\n<p>ax.set_xlabel(&#39;X\u8f74\u6807\u7b7e&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Y\u8f74\u6807\u7b7e&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_xlim(0, 10)<\/p>\n<p>ax.set_ylim(0, 10)<\/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\u5bfc\u5165\u4e86Matplotlib\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>subplots()<\/code>\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61<code>fig<\/code>\u548c\u4e00\u4e2a\u8f74\u5bf9\u8c61<code>ax<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>ax.set_xlabel()<\/code>\u548c<code>ax.set_ylabel()<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u4e86\u5750\u6807\u8f74\u7684\u6807\u7b7e\uff0c\u4f7f\u7528<code>ax.set_xlim()<\/code>\u548c<code>ax.set_ylim()<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u4e86\u5750\u6807\u8f74\u7684\u8303\u56f4\uff0c\u6700\u540e\u4f7f\u7528<code>plt.show()<\/code>\u65b9\u6cd5\u663e\u793a\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Matplotlib\u8868\u793a\u5750\u6807\u8f74\u7684\u5404\u4e2a\u65b9\u9762\uff0c\u5305\u62ec\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6807\u7b7e\u3001\u523b\u5ea6\u3001\u8303\u56f4\u3001\u7f51\u683c\u3001\u989c\u8272\u3001\u7ebf\u578b\u7b49\u5c5e\u6027\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u5bfc\u5165Matplotlib\u5e93<\/h2>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Matplotlib\u8868\u793a\u5750\u6807\u8f74\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165Matplotlib\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165Matplotlib\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>pyplot<\/code>\u662fMatplotlib\u5e93\u4e2d\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u5305\u542b\u4e86\u7ed8\u56fe\u6240\u9700\u7684\u6240\u6709\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61\u548c\u8f74\u5bf9\u8c61<\/h2>\n<\/p>\n<p><p>\u5728\u5bfc\u5165Matplotlib\u5e93\u4e4b\u540e\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61\u548c\u4e00\u4e2a\u8f74\u5bf9\u8c61\u3002\u53ef\u4ee5\u4f7f\u7528<code>subplots()<\/code>\u51fd\u6570\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61\u548c\u8f74\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, ax = plt.subplots()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>fig<\/code>\u662f\u56fe\u5f62\u5bf9\u8c61\uff0c<code>ax<\/code>\u662f\u8f74\u5bf9\u8c61\u3002\u56fe\u5f62\u5bf9\u8c61\u662f\u6574\u4e2a\u7ed8\u56fe\u533a\u57df\uff0c\u8f74\u5bf9\u8c61\u662f\u56fe\u5f62\u5bf9\u8c61\u4e2d\u7684\u4e00\u4e2a\u5b50\u533a\u57df\uff0c\u5305\u542b\u4e86\u5750\u6807\u8f74\u3001\u6807\u7b7e\u3001\u523b\u5ea6\u3001\u7f51\u683c\u7b49\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6807\u7b7e<\/h2>\n<\/p>\n<p><p>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6807\u7b7e\u662f\u8868\u793a\u5750\u6807\u8f74\u7684\u4e00\u4e2a\u91cd\u8981\u65b9\u9762\u3002\u53ef\u4ee5\u4f7f\u7528\u8f74\u5bf9\u8c61\u7684<code>set_xlabel()<\/code>\u548c<code>set_ylabel()<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6807\u7b7e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.set_xlabel(&#39;X\u8f74\u6807\u7b7e&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Y\u8f74\u6807\u7b7e&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u5b57\u7b26\u4e32\u53c2\u6570\u6765\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6807\u7b7e\u3002\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6807\u7b7e\u6709\u52a9\u4e8e\u63cf\u8ff0\u56fe\u5f62\u4e2d\u7684\u6570\u636e\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u523b\u5ea6<\/h2>\n<\/p>\n<p><p>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u523b\u5ea6\u662f\u8868\u793a\u5750\u6807\u8f74\u7684\u53e6\u4e00\u4e2a\u91cd\u8981\u65b9\u9762\u3002\u53ef\u4ee5\u4f7f\u7528\u8f74\u5bf9\u8c61\u7684<code>set_xticks()<\/code>\u548c<code>set_yticks()<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u523b\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.set_xticks([0, 2, 4, 6, 8, 10])<\/p>\n<p>ax.set_yticks([0, 2, 4, 6, 8, 10])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e00\u4e2a\u5217\u8868\u53c2\u6570\u6765\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u523b\u5ea6\u3002\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u523b\u5ea6\u6709\u52a9\u4e8e\u66f4\u51c6\u786e\u5730\u8bfb\u53d6\u56fe\u5f62\u4e2d\u7684\u6570\u636e\u503c\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u8303\u56f4<\/h2>\n<\/p>\n<p><p>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u8303\u56f4\u662f\u8868\u793a\u5750\u6807\u8f74\u7684\u53e6\u4e00\u4e2a\u91cd\u8981\u65b9\u9762\u3002\u53ef\u4ee5\u4f7f\u7528\u8f74\u5bf9\u8c61\u7684<code>set_xlim()<\/code>\u548c<code>set_ylim()<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u8303\u56f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.set_xlim(0, 10)<\/p>\n<p>ax.set_ylim(0, 10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e24\u4e2a\u53c2\u6570\u6765\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u8303\u56f4\u3002\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u8303\u56f4\u6709\u52a9\u4e8e\u663e\u793a\u56fe\u5f62\u4e2d\u7684\u7279\u5b9a\u6570\u636e\u533a\u57df\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u7f51\u683c<\/h2>\n<\/p>\n<p><p>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u7f51\u683c\u662f\u8868\u793a\u5750\u6807\u8f74\u7684\u53e6\u4e00\u4e2a\u91cd\u8981\u65b9\u9762\u3002\u53ef\u4ee5\u4f7f\u7528\u8f74\u5bf9\u8c61\u7684<code>grid()<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u7f51\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.grid(True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u5e03\u5c14\u53c2\u6570\u6765\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u7f51\u683c\u3002\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u7f51\u683c\u6709\u52a9\u4e8e\u66f4\u6e05\u6670\u5730\u663e\u793a\u56fe\u5f62\u4e2d\u7684\u6570\u636e\u70b9\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u989c\u8272\u548c\u7ebf\u578b<\/h2>\n<\/p>\n<p><p>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u989c\u8272\u548c\u7ebf\u578b\u662f\u8868\u793a\u5750\u6807\u8f74\u7684\u53e6\u4e00\u4e2a\u91cd\u8981\u65b9\u9762\u3002\u53ef\u4ee5\u4f7f\u7528\u8f74\u5bf9\u8c61\u7684<code>spines<\/code>\u5c5e\u6027\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u989c\u8272\u548c\u7ebf\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.spines[&#39;left&#39;].set_color(&#39;red&#39;)<\/p>\n<p>ax.spines[&#39;left&#39;].set_linestyle(&#39;--&#39;)<\/p>\n<p>ax.spines[&#39;bottom&#39;].set_color(&#39;blue&#39;)<\/p>\n<p>ax.spines[&#39;bottom&#39;].set_linestyle(&#39;:&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u989c\u8272\u5b57\u7b26\u4e32\u548c\u7ebf\u578b\u5b57\u7b26\u4e32\u6765\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u989c\u8272\u548c\u7ebf\u578b\u3002\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u989c\u8272\u548c\u7ebf\u578b\u6709\u52a9\u4e8e\u7f8e\u5316\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u7efc\u5408\u793a\u4f8b<\/h2>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Matplotlib\u8868\u793a\u5750\u6807\u8f74\u7684\u5404\u4e2a\u65b9\u9762\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6807\u7b7e<\/strong><\/h2>\n<p>ax.set_xlabel(&#39;X\u8f74\u6807\u7b7e&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Y\u8f74\u6807\u7b7e&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u523b\u5ea6<\/strong><\/h2>\n<p>ax.set_xticks([0, 2, 4, 6, 8, 10])<\/p>\n<p>ax.set_yticks([0, 2, 4, 6, 8, 10])<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_xlim(0, 10)<\/p>\n<p>ax.set_ylim(0, 10)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u7f51\u683c<\/strong><\/h2>\n<p>ax.grid(True)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u989c\u8272\u548c\u7ebf\u578b<\/strong><\/h2>\n<p>ax.spines[&#39;left&#39;].set_color(&#39;red&#39;)<\/p>\n<p>ax.spines[&#39;left&#39;].set_linestyle(&#39;--&#39;)<\/p>\n<p>ax.spines[&#39;bottom&#39;].set_color(&#39;blue&#39;)<\/p>\n<p>ax.spines[&#39;bottom&#39;].set_linestyle(&#39;:&#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>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Matplotlib\u8868\u793a\u5750\u6807\u8f74\u7684\u6807\u7b7e\u3001\u523b\u5ea6\u3001\u8303\u56f4\u3001\u7f51\u683c\u3001\u989c\u8272\u548c\u7ebf\u578b\u3002\u901a\u8fc7\u8fd9\u4e9b\u8bbe\u7f6e\uff0c\u53ef\u4ee5\u521b\u5efa\u51fa\u4e00\u4e2a\u7f8e\u89c2\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h2>\u4e5d\u3001\u4f7f\u7528Plotly\u8868\u793a\u5750\u6807\u8f74<\/h2>\n<\/p>\n<p><p>\u9664\u4e86Matplotlib\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528Plotly\u5e93\u8868\u793a\u5750\u6807\u8f74\u3002Plotly\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u5e93\uff0c\u5177\u6709\u5f3a\u5927\u7684\u7ed8\u56fe\u529f\u80fd\u548c\u7f8e\u89c2\u7684\u56fe\u5f62\u6548\u679c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Plotly\u8868\u793a\u5750\u6807\u8f74\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.graph_objects as go<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>fig = go.Figure()<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e<\/strong><\/h2>\n<p>fig.add_trace(go.Scatter(x=[0, 2, 4, 6, 8, 10], y=[0, 1, 4, 9, 16, 25], mode=&#39;lines+markers&#39;))<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6807\u7b7e<\/strong><\/h2>\n<p>fig.update_layout(<\/p>\n<p>    xaxis_title=&#39;X\u8f74\u6807\u7b7e&#39;,<\/p>\n<p>    yaxis_title=&#39;Y\u8f74\u6807\u7b7e&#39;<\/p>\n<p>)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u523b\u5ea6<\/strong><\/h2>\n<p>fig.update_xaxes(tickvals=[0, 2, 4, 6, 8, 10])<\/p>\n<p>fig.update_yaxes(tickvals=[0, 5, 10, 15, 20, 25])<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u8303\u56f4<\/strong><\/h2>\n<p>fig.update_xaxes(range=[0, 10])<\/p>\n<p>fig.update_yaxes(range=[0, 25])<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/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\u9996\u5148\u5bfc\u5165\u4e86Plotly\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>go.Figure()<\/code>\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61<code>fig<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>fig.add_trace()<\/code>\u65b9\u6cd5\u6dfb\u52a0\u4e86\u6570\u636e\uff0c\u4f7f\u7528<code>fig.update_layout()<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u4e86\u5750\u6807\u8f74\u7684\u6807\u7b7e\uff0c\u4f7f\u7528<code>fig.update_xaxes()<\/code>\u548c<code>fig.update_yaxes()<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u4e86\u5750\u6807\u8f74\u7684\u523b\u5ea6\u548c\u8303\u56f4\uff0c\u6700\u540e\u4f7f\u7528<code>fig.show()<\/code>\u65b9\u6cd5\u663e\u793a\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u3001\u4f7f\u7528Seaborn\u8868\u793a\u5750\u6807\u8f74<\/h2>\n<\/p>\n<p><p>Seaborn\u662f\u53e6\u4e00\u4e2a\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\uff0c\u5b83\u57fa\u4e8eMatplotlib\u5e76\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u7ed8\u56fe\u63a5\u53e3\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Seaborn\u8868\u793a\u5750\u6807\u8f74\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/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 = sns.load_dataset(&#39;iris&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u6570\u636e<\/strong><\/h2>\n<p>sns.scatterplot(x=&#39;sepal_length&#39;, y=&#39;sepal_width&#39;, data=data, ax=ax)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6807\u7b7e<\/strong><\/h2>\n<p>ax.set_xlabel(&#39;\u82b1\u843c\u957f\u5ea6&#39;)<\/p>\n<p>ax.set_ylabel(&#39;\u82b1\u843c\u5bbd\u5ea6&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u523b\u5ea6<\/strong><\/h2>\n<p>ax.set_xticks([4, 5, 6, 7, 8])<\/p>\n<p>ax.set_yticks([2, 3, 4, 5])<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_xlim(4, 8)<\/p>\n<p>ax.set_ylim(2, 5)<\/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\u5bfc\u5165\u4e86Seaborn\u548cMatplotlib\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>sns.load_dataset()<\/code>\u51fd\u6570\u52a0\u8f7d\u4e86\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u96c6\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>plt.subplots()<\/code>\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61<code>fig<\/code>\u548c\u4e00\u4e2a\u8f74\u5bf9\u8c61<code>ax<\/code>\uff0c\u4f7f\u7528<code>sns.scatterplot()<\/code>\u51fd\u6570\u7ed8\u5236\u4e86\u6570\u636e\uff0c\u4f7f\u7528<code>ax.set_xlabel()<\/code>\u548c<code>ax.set_ylabel()<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u4e86\u5750\u6807\u8f74\u7684\u6807\u7b7e\uff0c\u4f7f\u7528<code>ax.set_xticks()<\/code>\u548c<code>ax.set_yticks()<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u4e86\u5750\u6807\u8f74\u7684\u523b\u5ea6\uff0c\u4f7f\u7528<code>ax.set_xlim()<\/code>\u548c<code>ax.set_ylim()<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u4e86\u5750\u6807\u8f74\u7684\u8303\u56f4\uff0c\u6700\u540e\u4f7f\u7528<code>plt.show()<\/code>\u65b9\u6cd5\u663e\u793a\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><h2>\u5341\u4e00\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Matplotlib\u8868\u793a\u5750\u6807\u8f74\uff0c\u5305\u62ec\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6807\u7b7e\u3001\u523b\u5ea6\u3001\u8303\u56f4\u3001\u7f51\u683c\u3001\u989c\u8272\u3001\u7ebf\u578b\u7b49\u5c5e\u6027\u3002\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Plotly\u548cSeaborn\u8868\u793a\u5750\u6807\u8f74\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u521b\u5efa\u51fa\u7f8e\u89c2\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u4f7f\u7528Matplotlib\u3001Plotly\u8fd8\u662fSeaborn\uff0c\u8868\u793a\u5750\u6807\u8f74\u7684\u57fa\u672c\u6b65\u9aa4\u90fd\u662f\u76f8\u4f3c\u7684\uff1a\u9996\u5148\u5bfc\u5165\u5e93\uff0c\u7136\u540e\u521b\u5efa\u56fe\u5f62\u5bf9\u8c61\u548c\u8f74\u5bf9\u8c61\uff0c\u63a5\u7740\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u5404\u4e2a\u5c5e\u6027\uff0c\u6700\u540e\u663e\u793a\u56fe\u5f62\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u8868\u793a\u5750\u6807\u8f74\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u5750\u6807\u8f74\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u521b\u5efa\u5750\u6807\u8f74\u3002\u901a\u8fc7\u4f7f\u7528<code>plt.subplots()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u56fe\u5f62\u548c\u4e00\u7ec4\u5750\u6807\u8f74\u3002\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u8303\u56f4\u548c\u6807\u7b7e\u4e5f\u76f8\u5bf9\u7b80\u5355\uff0c\u4f8b\u5982\uff0c\u4f7f\u7528<code>ax.set_xlim()<\/code>\u548c<code>ax.set_ylim()<\/code>\u6765\u5b9a\u4e49\u5750\u6807\u8f74\u7684\u8303\u56f4\uff0c\u4f7f\u7528<code>ax.set_xlabel()<\/code>\u548c<code>ax.set_ylabel()<\/code>\u6765\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6807\u7b7e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u81ea\u5b9a\u4e49\u5750\u6807\u8f74\u7684\u6837\u5f0f\uff1f<\/strong><br \/>\u4e3a\u4e86\u81ea\u5b9a\u4e49\u5750\u6807\u8f74\u7684\u6837\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684<code>tick_params()<\/code>\u65b9\u6cd5\u6765\u8c03\u6574\u5750\u6807\u8f74\u7684\u523b\u5ea6\u548c\u6807\u7b7e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u66f4\u6539\u523b\u5ea6\u7684\u5927\u5c0f\u3001\u989c\u8272\u6216\u65b9\u5411\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>ax.spines<\/code>\u6765\u4fee\u6539\u5750\u6807\u8f74\u7684\u8fb9\u6846\u6837\u5f0f\uff0c\u5982\u989c\u8272\u3001\u5bbd\u5ea6\u548c\u7ebf\u578b\uff0c\u589e\u5f3a\u56fe\u5f62\u7684\u89c6\u89c9\u6548\u679c\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u6dfb\u52a0\u7f51\u683c\u5230\u5750\u6807\u8f74\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Matplotlib\u7ed8\u5236\u56fe\u5f62\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>ax.grid()<\/code>\u51fd\u6570\u8f7b\u677e\u6dfb\u52a0\u7f51\u683c\u7ebf\u3002\u60a8\u53ef\u4ee5\u9009\u62e9\u7f51\u683c\u7ebf\u7684\u989c\u8272\u3001\u7ebf\u578b\u548c\u900f\u660e\u5ea6\uff0c\u4ee5\u63d0\u9ad8\u56fe\u5f62\u7684\u53ef\u8bfb\u6027\u3002\u901a\u8fc7\u8bbe\u7f6e\u53c2\u6570\u5982<code>which=&#39;both&#39;<\/code>\uff0c\u53ef\u4ee5\u540c\u65f6\u663e\u793a\u4e3b\u523b\u5ea6\u548c\u6b21\u523b\u5ea6\u7684\u7f51\u683c\u7ebf\uff0c\u5e2e\u52a9\u7528\u6237\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u5206\u5e03\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8868\u793a\u5750\u6807\u8f74\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528Matplotlib\u5e93\u3001Plotly\u5e93\u3001Seaborn\u5e93\u7b49\u3002\u5176\u4e2d\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1187677,"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\/1187672"}],"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=1187672"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187672\/revisions"}],"predecessor-version":[{"id":1187678,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187672\/revisions\/1187678"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1187677"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1187672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1187672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1187672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}