{"id":1118446,"date":"2025-01-08T18:35:29","date_gmt":"2025-01-08T10:35:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1118446.html"},"modified":"2025-01-08T18:35:32","modified_gmt":"2025-01-08T10:35:32","slug":"python%e7%94%bb%e5%9b%be%e5%a6%82%e4%bd%95%e6%8a%8a%e5%9d%90%e6%a0%87%e8%bd%b4%e6%94%b9%e6%88%90%e8%99%9a%e7%ba%bf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1118446.html","title":{"rendered":"python\u753b\u56fe\u5982\u4f55\u628a\u5750\u6807\u8f74\u6539\u6210\u865a\u7ebf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081942\/211bb713-34f3-40d2-8b03-7c084df0963e.webp\" alt=\"python\u753b\u56fe\u5982\u4f55\u628a\u5750\u6807\u8f74\u6539\u6210\u865a\u7ebf\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u5c06\u7ed8\u56fe\u7684\u5750\u6807\u8f74\u6539\u4e3a\u865a\u7ebf\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3002\u901a\u8fc7\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6837\u5f0f\u548c\u523b\u5ea6\u7ebf\u6837\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>ax.spines<\/code>\u548c<code>ax.tick_params<\/code>\u51fd\u6570\u5bf9\u5750\u6807\u8f74\u8fdb\u884c\u8be6\u7ec6\u8bbe\u7f6e\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Matplotlib\u5e93\u5c06\u5750\u6807\u8f74\u6539\u4e3a\u865a\u7ebf\uff0c\u5e76\u63a2\u8ba8\u5982\u4f55\u8fdb\u4e00\u6b65\u81ea\u5b9a\u4e49\u56fe\u8868\u4ee5\u6ee1\u8db3\u5404\u79cd\u9700\u6c42\u3002\u5177\u4f53\u5185\u5bb9\u5305\u62ec\u5b89\u88c5\u548c\u5bfc\u5165Matplotlib\u5e93\u3001\u521b\u5efa\u57fa\u672c\u56fe\u8868\u3001\u4fee\u6539\u5750\u6807\u8f74\u4e3a\u865a\u7ebf\u3001\u4ee5\u53ca\u5176\u4ed6\u76f8\u5173\u7684\u81ea\u5b9a\u4e49\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165Matplotlib\u5e93<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u9700\u8981\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u8be5\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>\u4e8c\u3001\u521b\u5efa\u57fa\u672c\u56fe\u8868<\/p>\n<\/p>\n<p><p>\u5728\u4fee\u6539\u5750\u6807\u8f74\u4e3a\u865a\u7ebf\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u56fe\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u6b63\u5f26\u6ce2\u7684\u56fe\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot(x, y)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4fee\u6539\u5750\u6807\u8f74\u4e3a\u865a\u7ebf<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u5c06\u5750\u6807\u8f74\u6539\u4e3a\u865a\u7ebf\uff0c\u6211\u4eec\u9700\u8981\u5bf9<code>ax.spines<\/code>\u8fdb\u884c\u8bbe\u7f6e\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5750\u6807\u8f74\u8bbe\u7f6e\u4e3a\u865a\u7ebf<\/p>\n<p>for spine in ax.spines.values():<\/p>\n<p>    spine.set_linestyle(&#39;--&#39;)<\/p>\n<p>    spine.set_linewidth(0.5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ee3\u7801\u4f1a\u5c06\u6240\u6709\u5750\u6807\u8f74\uff08\u4e0a\u3001\u4e0b\u3001\u5de6\u3001\u53f3\uff09\u90fd\u8bbe\u7f6e\u4e3a\u865a\u7ebf\uff0c\u5e76\u4e14\u7ebf\u5bbd\u4e3a0.5\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u8fdb\u4e00\u6b65\u81ea\u5b9a\u4e49\u5750\u6807\u8f74<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u5c06\u5750\u6807\u8f74\u6539\u4e3a\u865a\u7ebf\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5bf9\u523b\u5ea6\u7ebf\u8fdb\u884c\u81ea\u5b9a\u4e49\u8bbe\u7f6e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u523b\u5ea6\u7ebf\u4e5f\u8bbe\u7f6e\u4e3a\u865a\u7ebf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u523b\u5ea6\u7ebf\u8bbe\u7f6e\u4e3a\u865a\u7ebf<\/p>\n<p>ax.tick_params(axis=&#39;both&#39;, which=&#39;both&#39;, direction=&#39;in&#39;, length=6, width=0.5, colors=&#39;black&#39;, grid_color=&#39;black&#39;, grid_alpha=0.5, linestyle=&#39;--&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u6a2a\u8f74\u548c\u7eb5\u8f74\u7684\u6240\u6709\u523b\u5ea6\u7ebf\u90fd\u8bbe\u7f6e\u4e3a\u865a\u7ebf\uff0c\u5e76\u4e14\u5bf9\u523b\u5ea6\u7ebf\u7684\u65b9\u5411\u3001\u957f\u5ea6\u3001\u5bbd\u5ea6\u3001\u989c\u8272\u7b49\u8fdb\u884c\u4e86\u8fdb\u4e00\u6b65\u7684\u81ea\u5b9a\u4e49\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5305\u542b\u6240\u6709\u4e0a\u8ff0\u6b65\u9aa4\u7684\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot(x, y)<\/p>\n<h2><strong>\u5c06\u5750\u6807\u8f74\u8bbe\u7f6e\u4e3a\u865a\u7ebf<\/strong><\/h2>\n<p>for spine in ax.spines.values():<\/p>\n<p>    spine.set_linestyle(&#39;--&#39;)<\/p>\n<p>    spine.set_linewidth(0.5)<\/p>\n<h2><strong>\u5c06\u523b\u5ea6\u7ebf\u8bbe\u7f6e\u4e3a\u865a\u7ebf<\/strong><\/h2>\n<p>ax.tick_params(axis=&#39;both&#39;, which=&#39;both&#39;, direction=&#39;in&#39;, length=6, width=0.5, colors=&#39;black&#39;, grid_color=&#39;black&#39;, grid_alpha=0.5, linestyle=&#39;--&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u603b\u7ed3\u4e0e\u6269\u5c55<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06Python\u4e2d\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u7684\u56fe\u8868\u7684\u5750\u6807\u8f74\u548c\u523b\u5ea6\u7ebf\u6539\u4e3a\u865a\u7ebf\u3002\u8fd9\u79cd\u81ea\u5b9a\u4e49\u8bbe\u7f6e\u4e0d\u4ec5\u53ef\u4ee5\u589e\u5f3a\u56fe\u8868\u7684\u7f8e\u89c2\u6027\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u56fe\u8868\u66f4\u5bb9\u6613\u9605\u8bfb\u548c\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0cMatplotlib\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u81ea\u5b9a\u4e49\u9009\u9879\uff0c\u4f8b\u5982\u53ef\u4ee5\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u989c\u8272\u3001\u523b\u5ea6\u6807\u7b7e\u7684\u5b57\u4f53\u3001\u56fe\u4f8b\u7684\u4f4d\u7f6e\u7b49\u3002\u901a\u8fc7\u7075\u6d3b\u4f7f\u7528\u8fd9\u4e9b\u9009\u9879\uff0c\u53ef\u4ee5\u521b\u5efa\u6ee1\u8db3\u5404\u79cd\u9700\u6c42\u7684\u4e13\u4e1a\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u66f4\u591a\u590d\u6742\u7684\u9700\u6c42\uff0c\u4f8b\u5982\u5728\u540c\u4e00\u56fe\u8868\u4e2d\u663e\u793a\u591a\u4e2a\u6570\u636e\u96c6\u3001\u6dfb\u52a0\u8bef\u5dee\u6761\u3001\u8bbe\u7f6e\u53cc\u5750\u6807\u8f74\u7b49\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u9700\u6c42\uff0cMatplotlib\u5e93\u540c\u6837\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5efa\u8bae\u8bfb\u8005\u53c2\u8003Matplotlib\u7684\u5b98\u65b9\u6587\u6863\uff0c\u8fdb\u4e00\u6b65\u63a2\u7d22\u5176\u5f3a\u5927\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p><strong>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u4e2d\u7684Matplotlib\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u8bbe\u7f6e\u5c06\u5750\u6807\u8f74\u6539\u4e3a\u865a\u7ebf\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4e30\u5bcc\u7684\u81ea\u5b9a\u4e49\u9009\u9879\u6ee1\u8db3\u5404\u79cd\u56fe\u8868\u7ed8\u5236\u9700\u6c42\u3002<\/strong>\u5e0c\u671b\u672c\u6587\u7684\u4ecb\u7ecd\u80fd\u591f\u5e2e\u52a9\u8bfb\u8005\u638c\u63e1\u8fd9\u4e00\u6280\u5de7\uff0c\u5e76\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7075\u6d3b\u8fd0\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u5750\u6807\u8f74\u4e3a\u865a\u7ebf\u6837\u5f0f\uff1f<\/strong><br \/>\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u5e93\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06\u5750\u6807\u8f74\u8bbe\u7f6e\u4e3a\u865a\u7ebf\u6837\u5f0f\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5bfc\u5165Matplotlib\u5e93\u548c\u76f8\u5e94\u7684\u6a21\u5757\u3002\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>axhline<\/code>\u548c<code>axvline<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u6a2a\u8f74\u548c\u7eb5\u8f74\u7684\u6837\u5f0f\u4e3a\u865a\u7ebf\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>linestyle=&#39;--&#39;<\/code>\u53c2\u6570\u53ef\u4ee5\u5c06\u7ebf\u6761\u8bbe\u7f6e\u4e3a\u865a\u7ebf\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\nplt.plot([1, 2, 3], [1, 4, 9])\nplt.axhline(y=0, color=&#39;k&#39;, linestyle=&#39;--&#39;)\nplt.axvline(x=0, color=&#39;k&#39;, linestyle=&#39;--&#39;)\nplt.show()\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528Seaborn\u7ed8\u56fe\u65f6\u5982\u4f55\u8c03\u6574\u5750\u6807\u8f74\u7ebf\u6761\u6837\u5f0f\uff1f<\/strong><br \/>Seaborn\u662f\u57fa\u4e8eMatplotlib\u6784\u5efa\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u867d\u7136\u5b83\u81ea\u5e26\u7f8e\u89c2\u7684\u6837\u5f0f\uff0c\u4f46\u5982\u679c\u60f3\u8981\u5c06\u5750\u6807\u8f74\u7ebf\u6761\u8bbe\u7f6e\u4e3a\u865a\u7ebf\uff0c\u53ef\u4ee5\u901a\u8fc7Matplotlib\u7684\u8bbe\u7f6e\u6765\u5b9e\u73b0\u3002\u60a8\u53ef\u4ee5\u5728\u521b\u5efaSeaborn\u56fe\u5f62\u540e\uff0c\u8c03\u7528Matplotlib\u7684\u76f8\u5173\u51fd\u6570\uff0c\u5982<code>set_linestyle()<\/code>\uff0c\u6765\u6539\u53d8\u5750\u6807\u8f74\u6837\u5f0f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import seaborn as sns\nimport matplotlib.pyplot as plt\n\nsns.set(style=&quot;whitegrid&quot;)\ndata = sns.load_dataset(&quot;tips&quot;)\nax = sns.scatterplot(x=&quot;total_bill&quot;, y=&quot;tip&quot;, data=data)\nax.axhline(y=0, color=&#39;k&#39;, linestyle=&#39;--&#39;)\nax.axvline(x=0, color=&#39;k&#39;, linestyle=&#39;--&#39;)\nplt.show()\n<\/code><\/pre>\n<p><strong>\u5728Jupyter Notebook\u4e2d\u5982\u4f55\u66f4\u6539\u5750\u6807\u8f74\u7ebf\u7684\u6837\u5f0f\uff1f<\/strong><br \/>\u5728Jupyter Notebook\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u76f4\u63a5\u7ed8\u56fe\u5e76\u8bbe\u7f6e\u5750\u6807\u8f74\u7ebf\u7684\u6837\u5f0f\u3002\u786e\u4fdd\u5728Notebook\u4e2d\u8fd0\u884c\u7684\u5355\u5143\u683c\u5305\u542b\u5fc5\u8981\u7684\u5bfc\u5165\u8bed\u53e5\uff0c\u5e76\u4f7f\u7528<code>%matplotlib inline<\/code>\u547d\u4ee4\u6765\u663e\u793a\u56fe\u5f62\u3002\u901a\u8fc7\u8c03\u7528<code>set()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5750\u6807\u8f74\u7ebf\u6837\u5f0f\u66f4\u6539\u4e3a\u865a\u7ebf\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">%matplotlib inline\nimport matplotlib.pyplot as plt\n\nplt.plot([1, 2, 3], [1, 4, 9])\nplt.gca().spines[&#39;bottom&#39;].set_linestyle(&#39;--&#39;)\nplt.gca().spines[&#39;left&#39;].set_linestyle(&#39;--&#39;)\nplt.show()\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u793a\u4f8b\u5c55\u793a\u4e86\u5728Jupyter Notebook\u4e2d\u5982\u4f55\u7075\u6d3b\u8bbe\u7f6e\u5750\u6807\u8f74\u7ebf\u7684\u6837\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5c06\u7ed8\u56fe\u7684\u5750\u6807\u8f74\u6539\u4e3a\u865a\u7ebf\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3002\u901a\u8fc7\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6837\u5f0f\u548c\u523b\u5ea6\u7ebf\u6837\u5f0f\uff0c\u53ef [&hellip;]","protected":false},"author":3,"featured_media":1118459,"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\/1118446"}],"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=1118446"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1118446\/revisions"}],"predecessor-version":[{"id":1118461,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1118446\/revisions\/1118461"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1118459"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1118446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1118446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1118446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}