{"id":1092015,"date":"2025-01-08T14:17:09","date_gmt":"2025-01-08T06:17:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1092015.html"},"modified":"2025-01-08T14:17:11","modified_gmt":"2025-01-08T06:17:11","slug":"%e6%95%a3%e7%82%b9%e5%9b%be%e7%ba%b5%e5%9d%90%e6%a0%87%e5%a6%82%e4%bd%95%e9%97%b4%e9%9a%9410python-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1092015.html","title":{"rendered":"\u6563\u70b9\u56fe\u7eb5\u5750\u6807\u5982\u4f55\u95f4\u969410python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24205246\/dfe511fe-d59d-491d-9124-94706968168b.webp\" alt=\"\u6563\u70b9\u56fe\u7eb5\u5750\u6807\u5982\u4f55\u95f4\u969410python\" \/><\/p>\n<p><p> <strong>\u6563\u70b9\u56fe\u7eb5\u5750\u6807\u95f4\u969410\u7684Python\u5b9e\u73b0\u65b9\u6cd5<\/strong>\uff1a\u901a\u8fc7\u8bbe\u7f6e\u6563\u70b9\u56fe\u7684\u7eb5\u5750\u6807\u95f4\u9694\uff0c\u53ef\u4ee5\u4f7f\u6570\u636e\u7684\u53ef\u8bfb\u6027\u66f4\u5f3a\u3002\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u5177\u4f53\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>plt.yticks()<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u7eb5\u5750\u6807\u7684\u523b\u5ea6\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u70b9\u7684\u8be6\u7ec6\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/p>\n<p>\u5728Python\u4e2d\uff0c\u7ed8\u5236\u6563\u70b9\u56fe\u7684\u5e38\u7528\u5e93\u5305\u62ecMatplotlib\u548cSeaborn\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165\u8fd9\u4e9b\u5e93\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<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u751f\u6210\u6570\u636e<\/p>\n<p>\u4e3a\u4e86\u6f14\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u751f\u6210\u4e00\u4e9b\u968f\u673a\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u968f\u673a\u6570\u636e<\/p>\n<p>np.random.seed(0)<\/p>\n<p>x = np.random.rand(50) * 100<\/p>\n<p>y = np.random.rand(50) * 100<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u7ed8\u5236\u6563\u70b9\u56fe<\/p>\n<p>\u4f7f\u7528Matplotlib\u7ed8\u5236\u57fa\u672c\u7684\u6563\u70b9\u56fe\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.scatter(x, y)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u8bbe\u7f6e\u7eb5\u5750\u6807\u95f4\u9694<\/p>\n<p>\u4f7f\u7528<code>plt.yticks()<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u7eb5\u5750\u6807\u7684\u523b\u5ea6\u95f4\u9694\u4e3a10\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u5f53\u524d\u7684\u7eb5\u5750\u6807\u8303\u56f4<\/p>\n<p>y_min, y_max = plt.ylim()<\/p>\n<h2><strong>\u8bbe\u7f6e\u7eb5\u5750\u6807\u7684\u523b\u5ea6\u95f4\u9694\u4e3a10<\/strong><\/h2>\n<p>plt.yticks(np.arange(y_min, y_max, 10))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u663e\u793a\u56fe\u5f62<\/p>\n<p>\u6700\u540e\uff0c\u4f7f\u7528<code>plt.show()<\/code>\u6765\u663e\u793a\u56fe\u5f62\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;\u6563\u70b9\u56fe\u793a\u4f8b&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u6210\u529f\u5730\u8bbe\u7f6e\u6563\u70b9\u56fe\u7684\u7eb5\u5750\u6807\u95f4\u9694\u4e3a10\u3002<\/p>\n<\/p>\n<p><p><strong>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u8bbe\u7f6e\u7eb5\u5750\u6807\u95f4\u9694<\/strong><\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u8bbe\u7f6e\u7eb5\u5750\u6807\u95f4\u9694\uff0c\u6211\u4eec\u9700\u8981\u6df1\u5165\u4e86\u89e3<code>plt.yticks()<\/code>\u51fd\u6570\u3002\u8be5\u51fd\u6570\u5141\u8bb8\u6211\u4eec\u624b\u52a8\u8bbe\u7f6e\u523b\u5ea6\u7684\u4f4d\u7f6e\u548c\u6807\u7b7e\uff0c\u4ece\u800c\u53ef\u4ee5\u7cbe\u786e\u63a7\u5236\u523b\u5ea6\u7684\u95f4\u9694\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u83b7\u53d6\u5f53\u524d\u7684\u7eb5\u5750\u6807\u8303\u56f4<\/p>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u56fe\u5f62\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.ylim()<\/code>\u51fd\u6570\u83b7\u53d6\u5f53\u524d\u7684\u7eb5\u5750\u6807\u8303\u56f4\u3002\u8fd9\u662f\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u503c\u7684\u5143\u7ec4\uff0c\u5206\u522b\u8868\u793a\u7eb5\u5750\u6807\u7684\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">y_min, y_max = plt.ylim()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u751f\u6210\u523b\u5ea6\u4f4d\u7f6e<\/p>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u7684<code>np.arange()<\/code>\u51fd\u6570\u751f\u6210\u4ece<code>y_min<\/code>\u5230<code>y_max<\/code>\uff0c\u6b65\u957f\u4e3a10\u7684\u6570\u7ec4\u3002\u8fd9\u4e9b\u503c\u5c06\u4f5c\u4e3a\u7eb5\u5750\u6807\u7684\u523b\u5ea6\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">yticks = np.arange(y_min, y_max, 10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3\u3001\u8bbe\u7f6e\u523b\u5ea6\u4f4d\u7f6e<\/p>\n<\/p>\n<p><p>\u901a\u8fc7<code>plt.yticks()<\/code>\u51fd\u6570\u5c06\u751f\u6210\u7684\u6570\u7ec4\u8bbe\u7f6e\u4e3a\u523b\u5ea6\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.yticks(yticks)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6269\u5c55\u5185\u5bb9\uff1a\u63d0\u9ad8\u56fe\u5f62\u7684\u53ef\u8bfb\u6027<\/strong><\/p>\n<\/p>\n<p><p>1\u3001\u6dfb\u52a0\u7f51\u683c\u7ebf<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u56fe\u5f62\u7684\u53ef\u8bfb\u6027\uff0c\u53ef\u4ee5\u6dfb\u52a0\u7f51\u683c\u7ebf\u3002\u4f7f\u7528<code>plt.grid()<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.grid(True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u8bbe\u7f6e\u523b\u5ea6\u5b57\u4f53\u5927\u5c0f<\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>plt.tick_params()<\/code>\u51fd\u6570\u53ef\u4ee5\u8bbe\u7f6e\u523b\u5ea6\u7684\u5b57\u4f53\u5927\u5c0f\uff0c\u4ece\u800c\u4f7f\u56fe\u5f62\u66f4\u52a0\u7f8e\u89c2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.tick_params(axis=&#39;both&#39;, which=&#39;major&#39;, labelsize=10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3\u3001\u6dfb\u52a0\u56fe\u4f8b<\/p>\n<\/p>\n<p><p>\u5982\u679c\u56fe\u5f62\u5305\u542b\u591a\u4e2a\u6570\u636e\u96c6\uff0c\u6dfb\u52a0\u56fe\u4f8b\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u56fe\u5f62\u3002\u4f7f\u7528<code>plt.legend()<\/code>\u51fd\u6570\u53ef\u4ee5\u6dfb\u52a0\u56fe\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.scatter(x, y, label=&#39;\u6570\u636e\u96c61&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u6269\u5c55\u5185\u5bb9\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u56fe\u5f62\u7684\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u5ea6\u3002<\/p>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u8be6\u7ec6\u63cf\u8ff0\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u4e00\u4e2a\u7eb5\u5750\u6807\u95f4\u9694\u4e3a10\u7684\u6563\u70b9\u56fe\u3002\u5177\u4f53\u5305\u62ec\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u751f\u6210\u6570\u636e\u3001\u7ed8\u5236\u6563\u70b9\u56fe\u3001\u8bbe\u7f6e\u7eb5\u5750\u6807\u95f4\u9694\u4ee5\u53ca\u663e\u793a\u56fe\u5f62\u3002\u4e3a\u4e86\u63d0\u9ad8\u56fe\u5f62\u7684\u53ef\u8bfb\u6027\uff0c\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u7f51\u683c\u7ebf\u3001\u8bbe\u7f6e\u523b\u5ea6\u5b57\u4f53\u5927\u5c0f\u548c\u6dfb\u52a0\u56fe\u4f8b\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u95f4\u9694\u4e3a10\u7684\u6563\u70b9\u56fe\u7eb5\u5750\u6807\uff1f<\/strong><br \/>\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u6563\u70b9\u56fe\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>yticks()<\/code>\u51fd\u6570\u8bbe\u7f6e\u7eb5\u5750\u6807\u7684\u523b\u5ea6\u95f4\u9694\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.yticks(range(min_value, max_value + 1, 10))<\/code>\u6765\u5b9a\u4e49\u7eb5\u5750\u6807\u7684\u523b\u5ea6\uff0c\u4ece\u800c\u8fbe\u5230\u6bcf10\u4e2a\u5355\u4f4d\u4e00\u95f4\u9694\u7684\u6548\u679c\u3002<\/p>\n<p><strong>\u4f7f\u7528Seaborn\u7ed8\u5236\u6563\u70b9\u56fe\u65f6\uff0c\u5982\u4f55\u8bbe\u7f6e\u7eb5\u5750\u6807\u95f4\u9694\uff1f<\/strong><br \/>Seaborn\u662f\u57fa\u4e8eMatplotlib\u6784\u5efa\u7684\u9ad8\u7ea7\u6570\u636e\u53ef\u89c6\u5316\u5e93\u3002\u5728Seaborn\u4e2d\uff0c\u53ef\u4ee5\u5728\u7ed8\u5236\u6563\u70b9\u56fe\u540e\u4f7f\u7528Matplotlib\u7684<code>plt.yticks()<\/code>\u65b9\u6cd5\u6765\u8c03\u6574\u7eb5\u5750\u6807\u7684\u95f4\u9694\u3002\u4f8b\u5982\uff0c\u8c03\u7528<code>plt.yticks(range(min_value, max_value + 1, 10))<\/code>\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u95f4\u9694\u4e3a10\u7684\u7eb5\u5750\u6807\u3002<\/p>\n<p><strong>\u5728\u6563\u70b9\u56fe\u4e2d\u5982\u4f55\u81ea\u5b9a\u4e49\u7eb5\u5750\u6807\u6807\u7b7e\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5728\u6563\u70b9\u56fe\u4e2d\u81ea\u5b9a\u4e49\u7eb5\u5750\u6807\u6807\u7b7e\uff0c\u9664\u4e86\u8bbe\u7f6e\u95f4\u9694\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>plt.yticks()<\/code>\u51fd\u6570\u6765\u63d0\u4f9b\u81ea\u5b9a\u4e49\u6807\u7b7e\u5217\u8868\u3002\u901a\u8fc7\u4f20\u5165\u4e00\u4e2a\u6807\u7b7e\u5217\u8868\uff0c\u53ef\u4ee5\u8ba9\u7eb5\u5750\u6807\u663e\u793a\u7279\u5b9a\u7684\u6587\u672c\u6216\u683c\u5f0f\uff0c\u589e\u5f3a\u56fe\u8868\u7684\u53ef\u8bfb\u6027\u548c\u4fe1\u606f\u4f20\u8fbe\u3002<\/p>\n<p><strong>\u5982\u4f55\u786e\u4fdd\u6563\u70b9\u56fe\u7684\u7eb5\u5750\u6807\u95f4\u9694\u8bbe\u7f6e\u4e0d\u4f1a\u5f71\u54cd\u6570\u636e\u5c55\u793a\uff1f<\/strong><br \/>\u5728\u8bbe\u7f6e\u7eb5\u5750\u6807\u95f4\u9694\u65f6\uff0c\u5efa\u8bae\u5148\u4e86\u89e3\u6570\u636e\u7684\u8303\u56f4\u548c\u5206\u5e03\u60c5\u51b5\uff0c\u4ee5\u786e\u4fdd\u95f4\u9694\u8bbe\u7f6e\u4e0d\u4f1a\u5bfc\u81f4\u91cd\u8981\u6570\u636e\u88ab\u5ffd\u89c6\u3002\u4f7f\u7528<code>plt.ylim()<\/code>\u51fd\u6570\u53ef\u4ee5\u9650\u5236\u7eb5\u5750\u6807\u7684\u663e\u793a\u8303\u56f4\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u5c55\u73b0\u6570\u636e\u7684\u7ec6\u8282\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u6563\u70b9\u56fe\u7eb5\u5750\u6807\u95f4\u969410\u7684Python\u5b9e\u73b0\u65b9\u6cd5\uff1a\u901a\u8fc7\u8bbe\u7f6e\u6563\u70b9\u56fe\u7684\u7eb5\u5750\u6807\u95f4\u9694\uff0c\u53ef\u4ee5\u4f7f\u6570\u636e\u7684\u53ef\u8bfb\u6027\u66f4\u5f3a\u3002\u53ef\u4ee5\u4f7f\u7528Mat [&hellip;]","protected":false},"author":3,"featured_media":1092023,"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\/1092015"}],"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=1092015"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1092015\/revisions"}],"predecessor-version":[{"id":1092028,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1092015\/revisions\/1092028"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1092023"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1092015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1092015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1092015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}