{"id":1136675,"date":"2025-01-08T21:38:48","date_gmt":"2025-01-08T13:38:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1136675.html"},"modified":"2025-01-08T21:38:51","modified_gmt":"2025-01-08T13:38:51","slug":"python%e7%94%bb%e6%8a%98%e7%ba%bf%e5%9b%be%e5%a6%82%e4%bd%95%e9%9a%90%e8%97%8f%e7%ba%b5%e5%9d%90%e6%a0%87%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1136675.html","title":{"rendered":"python\u753b\u6298\u7ebf\u56fe\u5982\u4f55\u9690\u85cf\u7eb5\u5750\u6807\u7684\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100726\/a855eb81-daf6-4e85-a235-f95bf11bc606.webp\" alt=\"python\u753b\u6298\u7ebf\u56fe\u5982\u4f55\u9690\u85cf\u7eb5\u5750\u6807\u7684\u503c\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u9690\u85cf\u6298\u7ebf\u56fe\u7684\u7eb5\u5750\u6807\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\uff0c\u901a\u8fc7\u8bbe\u7f6eY\u8f74\u523b\u5ea6\u6807\u7b7e\u4e3a\u7a7a\u5b57\u7b26\u4e32\u6216\u5c06\u5176\u9690\u85cf<\/strong>\u3002\u5176\u4e2d\u4e00\u79cd\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>plt.gca().yaxis.set_visible(False)<\/code>\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u5e76\u521b\u5efa\u6570\u636e<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u521b\u5efa\u56fe\u8868\u7684\u7b2c\u4e00\u6b65\u662f\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff0c\u5e76\u51c6\u5907\u8981\u7ed8\u5236\u7684\u6570\u636e\u3002Matplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165Matplotlib\u5e76\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u96c6\u3002<\/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\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u96c6<\/strong><\/h2>\n<p>x = np.arange(0, 10, 0.1)<\/p>\n<p>y = np.sin(x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u7ed8\u5236\u6298\u7ebf\u56fe<\/p>\n<\/p>\n<p><p>\u6709\u4e86\u6570\u636e\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528Matplotlib\u521b\u5efa\u6298\u7ebf\u56fe\u3002\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>plt.plot()<\/code>\u51fd\u6570\u6765\u7ed8\u5236\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.plot(x, y)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u9690\u85cf\u7eb5\u5750\u6807\u7684\u503c<\/p>\n<\/p>\n<p><p>\u8981\u9690\u85cf\u7eb5\u5750\u6807\u7684\u503c\uff0c\u6709\u51e0\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u65b9\u6cd5\u4e00\uff1a\u8bbe\u7f6e\u523b\u5ea6\u6807\u7b7e\u4e3a\u7a7a\u5b57\u7b26\u4e32<\/strong><\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u5c06\u523b\u5ea6\u6807\u7b7e\u8bbe\u7f6e\u4e3a\u7a7a\u5b57\u7b26\u4e32\u6765\u9690\u85cf\u7eb5\u5750\u6807\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.gca().set_yticklabels([])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528<code>yaxis.set_visible(False)<\/code><\/strong><\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u8bbe\u7f6eY\u8f74\u4e0d\u53ef\u89c1\u6765\u9690\u85cf\u7eb5\u5750\u6807\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.gca().yaxis.set_visible(False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u65b9\u6cd5\u4e09\uff1a\u4f7f\u7528<code>tick_params<\/code>\u8bbe\u7f6e<\/strong><\/p>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u8bbe\u7f6e\u523b\u5ea6\u53c2\u6570\u6765\u9690\u85cf\u7eb5\u5750\u6807\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.tick_params(axis=&#39;y&#39;, which=&#39;both&#39;, left=False, labelleft=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\uff0c\u5c06\u4e0a\u8ff0\u6b65\u9aa4\u7ed3\u5408\u8d77\u6765\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\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u96c6<\/strong><\/h2>\n<p>x = np.arange(0, 10, 0.1)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u65b9\u6cd5\u4e00\uff1a\u8bbe\u7f6e\u523b\u5ea6\u6807\u7b7e\u4e3a\u7a7a\u5b57\u7b26\u4e32<\/strong><\/h2>\n<h2><strong>plt.gca().set_yticklabels([])<\/strong><\/h2>\n<h2><strong>\u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528yaxis.set_visible(False)<\/strong><\/h2>\n<p>plt.gca().yaxis.set_visible(False)<\/p>\n<h2><strong>\u65b9\u6cd5\u4e09\uff1a\u4f7f\u7528tick_params\u8bbe\u7f6e<\/strong><\/h2>\n<h2><strong>plt.tick_params(axis=&#39;y&#39;, which=&#39;both&#39;, left=False, labelleft=False)<\/strong><\/h2>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6df1\u5165\u89e3\u6790\uff1a\u4f7f\u7528<code>tick_params<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>tick_params<\/code>\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u9690\u85cf\u7eb5\u5750\u6807\u7684\u503c\uff0c\u8fd8\u53ef\u4ee5\u5bf9\u523b\u5ea6\u7684\u5176\u4ed6\u5c5e\u6027\u8fdb\u884c\u8be6\u7ec6\u8bbe\u7f6e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u63a7\u5236\u523b\u5ea6\u7ebf\u7684\u663e\u793a\u3001\u523b\u5ea6\u6807\u7b7e\u7684\u65b9\u5411\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.tick_params(axis=&#39;y&#39;, which=&#39;both&#39;, left=False, right=False, labelleft=False, labelright=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u6240\u6709Y\u8f74\u76f8\u5173\u7684\u523b\u5ea6\u7ebf\u548c\u6807\u7b7e\u90fd\u9690\u85cf\u4e86\u3002<\/p>\n<\/p>\n<p><h3>\u6df1\u5165\u89e3\u6790\uff1a\u4f7f\u7528<code>set_yticklabels<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>set_yticklabels<\/code>\u65b9\u6cd5\u53ef\u4ee5\u66f4\u7ec6\u7c92\u5ea6\u5730\u63a7\u5236Y\u8f74\u523b\u5ea6\u6807\u7b7e\u3002\u6bd4\u5982\uff0c\u53ef\u4ee5\u6839\u636e\u7279\u5b9a\u6761\u4ef6\u8bbe\u7f6e\u54ea\u4e9b\u6807\u7b7e\u663e\u793a\uff0c\u54ea\u4e9b\u4e0d\u663e\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ec5\u9690\u85cf\u7279\u5b9a\u523b\u5ea6\u6807\u7b7e<\/p>\n<p>current_labels = plt.gca().get_yticks()<\/p>\n<p>plt.gca().set_yticklabels([&#39;&#39; if label % 1 == 0 else label for label in current_labels])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6df1\u5165\u89e3\u6790\uff1a\u7ed3\u5408\u591a\u4e2a\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6709\u65f6\u9700\u8981\u7ed3\u5408\u591a\u4e2a\u65b9\u6cd5\u6765\u6ee1\u8db3\u7279\u5b9a\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u65e2\u8981\u9690\u85cf\u523b\u5ea6\u7ebf\uff0c\u53c8\u8981\u9690\u85cf\u523b\u5ea6\u6807\u7b7e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.tick_params(axis=&#39;y&#39;, which=&#39;both&#39;, left=False, labelleft=False)<\/p>\n<p>plt.gca().yaxis.set_tick_params(which=&#39;both&#39;, length=0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5176\u4ed6\u5b9e\u7528\u6280\u5de7<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u523b\u5ea6\u683c\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>FuncFormatter<\/code>\u6765\u81ea\u5b9a\u4e49\u523b\u5ea6\u6807\u7b7e\u7684\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from matplotlib.ticker import FuncFormatter<\/p>\n<p>def custom_format(x, pos):<\/p>\n<p>    return &#39;&#39;  # \u8fd9\u91cc\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\u6765\u9690\u85cf\u6807\u7b7e<\/p>\n<p>plt.gca().yaxis.set_major_formatter(FuncFormatter(custom_format))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8c03\u6574\u56fe\u8868\u5e03\u5c40<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9690\u85cf\u7eb5\u5750\u6807\u503c\u540e\uff0c\u53ef\u4ee5\u8c03\u6574\u56fe\u8868\u5e03\u5c40\u4ee5\u66f4\u597d\u5730\u5229\u7528\u7a7a\u95f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fdd\u5b58\u56fe\u8868<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u56fe\u8868\u8bbe\u7f6e\u540e\uff0c\u53ef\u4ee5\u5c06\u5176\u4fdd\u5b58\u4e3a\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.savefig(&#39;line_plot.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u9690\u85cf\u6298\u7ebf\u56fe\u7684\u7eb5\u5750\u6807\u503c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u8bbe\u7f6e\u523b\u5ea6\u6807\u7b7e\u4e3a\u7a7a\u5b57\u7b26\u4e32\u3001\u4f7f\u7528<code>yaxis.set_visible(False)<\/code>\u65b9\u6cd5\u4ee5\u53ca\u4f7f\u7528<code>tick_params<\/code>\u65b9\u6cd5\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u5e94\u7528\u573a\u666f\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u63a7\u5236\u56fe\u8868\u7684\u663e\u793a\u6548\u679c\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u662f\u4e3a\u4e86\u7b80\u5316\u56fe\u8868\u8fd8\u662f\u4e3a\u4e86\u66f4\u597d\u5730\u5c55\u793a\u6570\u636e\uff0c\u9690\u85cf\u4e0d\u5fc5\u8981\u7684\u5750\u6807\u503c\u90fd\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6280\u5de7\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u60a8\u80fd\u591f\u66f4\u597d\u5730\u638c\u63e1\u8fd9\u4e00\u6280\u5de7\uff0c\u5e76\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u7075\u6d3b\u5e94\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u6298\u7ebf\u56fe\u65f6\u9690\u85cf\u7eb5\u5750\u6807\u7684\u503c\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u6298\u7ebf\u56fe\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eY\u8f74\u7684\u523b\u5ea6\u6807\u7b7e\u4e3a\u7a7a\u6765\u5b9e\u73b0\u9690\u85cf\u7eb5\u5750\u6807\u7684\u503c\u3002\u5177\u4f53\u64cd\u4f5c\u662f\u4f7f\u7528<code>plt.yticks([])<\/code>\u51fd\u6570\uff0c\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u9690\u85cf\u7eb5\u5750\u6807\u7684\u6240\u6709\u503c\u3002<\/p>\n<p><strong>\u9690\u85cf\u7eb5\u5750\u6807\u503c\u4f1a\u5f71\u54cd\u56fe\u8868\u7684\u53ef\u8bfb\u6027\u5417\uff1f<\/strong><br \/>\u9690\u85cf\u7eb5\u5750\u6807\u503c\u53ef\u80fd\u4f1a\u4f7f\u56fe\u8868\u7684\u67d0\u4e9b\u4fe1\u606f\u4e0d\u90a3\u4e48\u660e\u663e\uff0c\u7279\u522b\u662f\u5f53\u7528\u6237\u9700\u8981\u4e86\u89e3\u5177\u4f53\u6570\u503c\u65f6\u3002\u5728\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u65f6\uff0c\u5efa\u8bae\u786e\u4fdd\u56fe\u8868\u7684\u5176\u4ed6\u5143\u7d20\uff08\u5982\u6807\u9898\u3001\u6a2a\u5750\u6807\u503c\u548c\u56fe\u4f8b\uff09\u8db3\u591f\u6e05\u6670\uff0c\u4ee5\u4fbf\u89c2\u4f17\u4ecd\u80fd\u83b7\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002<\/p>\n<p><strong>\u9664\u4e86\u9690\u85cf\u7eb5\u5750\u6807\u503c\uff0c\u8fd8\u6709\u54ea\u4e9b\u65b9\u5f0f\u53ef\u4ee5\u7b80\u5316\u56fe\u8868\u7684\u89c6\u89c9\u6548\u679c\uff1f<\/strong><br \/>\u9664\u4e86\u9690\u85cf\u7eb5\u5750\u6807\u503c\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u51cf\u5c11\u7f51\u683c\u7ebf\u3001\u8c03\u6574\u989c\u8272\u548c\u7ebf\u6761\u6837\u5f0f\u3001\u589e\u52a0\u900f\u660e\u5ea6\u7b49\u65b9\u5f0f\u6765\u7b80\u5316\u56fe\u8868\u7684\u89c6\u89c9\u6548\u679c\u3002\u8fd9\u4e9b\u64cd\u4f5c\u53ef\u4ee5\u4f7f\u6298\u7ebf\u56fe\u770b\u8d77\u6765\u66f4\u5e72\u51c0\uff0c\u540c\u65f6\u7a81\u51fa\u663e\u793a\u6570\u636e\u7684\u8d8b\u52bf\u548c\u53d8\u5316\u3002\u4f7f\u7528<code>plt.grid(False)<\/code>\u53ef\u4ee5\u5173\u95ed\u7f51\u683c\u7ebf\u663e\u793a\uff0c\u4ece\u800c\u4f7f\u56fe\u8868\u66f4\u52a0\u7b80\u6d01\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u9690\u85cf\u6298\u7ebf\u56fe\u7684\u7eb5\u5750\u6807\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\uff0c\u901a\u8fc7\u8bbe\u7f6eY\u8f74\u523b\u5ea6\u6807\u7b7e\u4e3a\u7a7a\u5b57\u7b26\u4e32\u6216\u5c06\u5176\u9690\u85cf [&hellip;]","protected":false},"author":3,"featured_media":1136685,"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\/1136675"}],"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=1136675"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136675\/revisions"}],"predecessor-version":[{"id":1136688,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1136675\/revisions\/1136688"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1136685"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1136675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1136675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1136675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}