{"id":1076254,"date":"2025-01-08T11:51:44","date_gmt":"2025-01-08T03:51:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1076254.html"},"modified":"2025-01-08T11:51:46","modified_gmt":"2025-01-08T03:51:46","slug":"python%e6%a8%aa%e5%9d%90%e6%a0%87%e5%a6%82%e4%bd%95%e6%98%be%e7%a4%ba%e6%b5%ae%e7%82%b9%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1076254.html","title":{"rendered":"python\u6a2a\u5750\u6807\u5982\u4f55\u663e\u793a\u6d6e\u70b9\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180924\/ca3b128e-aa32-4dbb-8f76-29a7ffa27b87.webp\" alt=\"python\u6a2a\u5750\u6807\u5982\u4f55\u663e\u793a\u6d6e\u70b9\u6570\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u5982\u679c\u4f60\u60f3\u5728\u7ed8\u5236\u56fe\u8868\u65f6\u663e\u793a\u6d6e\u70b9\u6570\u4f5c\u4e3a\u6a2a\u5750\u6807\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3002<strong>Matplotlib\u5e93\u975e\u5e38\u5f3a\u5927\u3001\u7075\u6d3b\uff0c\u652f\u6301\u6d6e\u70b9\u6570\u663e\u793a\u3001\u53ef\u4ee5\u901a\u8fc7\u683c\u5f0f\u5316\u51fd\u6570\u81ea\u5b9a\u4e49\u663e\u793a\u683c\u5f0f\u3001\u53ef\u4ee5\u8bbe\u7f6e\u523b\u5ea6\u548c\u6807\u7b7e\u683c\u5f0f<\/strong>\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u5b83\u6765\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5Matplotlib\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\u4f60\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86Matplotlib\u5e93\uff0c\u5982\u679c\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><h3>\u4e8c\u3001\u7ed8\u5236\u57fa\u7840\u56fe\u8868<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u7ed8\u5236\u4e00\u4e2a\u5305\u542b\u6d6e\u70b9\u6570\u6a2a\u5750\u6807\u7684\u56fe\u8868\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>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)  # \u751f\u6210\u4ece0\u523010\u7684100\u4e2a\u7b49\u95f4\u8ddd\u70b9<\/p>\n<p>y = np.sin(x)  # \u8ba1\u7b97y\u503c<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u8868<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6a2a\u5750\u6807\u548c\u7eb5\u5750\u6807\u6807\u7b7e<\/strong><\/h2>\n<p>plt.xlabel(&#39;X-axis (Float)&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8be6\u7ec6\u8bbe\u7f6e\u6a2a\u5750\u6807\u663e\u793a\u6d6e\u70b9\u6570<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u4e0a\u9762\u7684\u4f8b\u5b50\u5df2\u7ecf\u663e\u793a\u4e86\u6d6e\u70b9\u6570\u6a2a\u5750\u6807\uff0c\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u8fdb\u884c\u66f4\u591a\u7684\u5b9a\u5236\u6765\u63d0\u9ad8\u663e\u793a\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bbe\u7f6e\u523b\u5ea6\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7<code>plt.gca()<\/code>\u83b7\u53d6\u5f53\u524d\u7684\u8f74\uff0c\u5e76\u4f7f\u7528<code>xaxis.set_major_formatter<\/code>\u6765\u8bbe\u7f6e\u523b\u5ea6\u683c\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.ticker as ticker<\/p>\n<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u8868<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6a2a\u5750\u6807\u548c\u7eb5\u5750\u6807\u6807\u7b7e<\/strong><\/h2>\n<p>plt.xlabel(&#39;X-axis (Float)&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u7684\u8f74<\/strong><\/h2>\n<p>ax = plt.gca()<\/p>\n<h2><strong>\u8bbe\u7f6e\u6a2a\u5750\u6807\u663e\u793a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>ax.xaxis.set_major_formatter(ticker.FormatStrFormatter(&#39;%.2f&#39;))<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u81ea\u5b9a\u4e49\u523b\u5ea6<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7<code>set_xticks<\/code>\u6765\u81ea\u5b9a\u4e49\u523b\u5ea6\u7684\u4f4d\u7f6e\u548c\u6807\u7b7e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u6570\u636e<\/p>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u8868<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6a2a\u5750\u6807\u548c\u7eb5\u5750\u6807\u6807\u7b7e<\/strong><\/h2>\n<p>plt.xlabel(&#39;X-axis (Float)&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49\u523b\u5ea6\u4f4d\u7f6e\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.xticks(np.arange(0, 11, 1), [f&#39;{i:.1f}&#39; for i in np.arange(0, 11, 1)])<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u66f4\u591a\u9ad8\u7ea7\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><h4>1. \u8bbe\u7f6e\u523b\u5ea6\u5bc6\u5ea6<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u8bbe\u7f6e\u66f4\u5bc6\u96c6\u7684\u523b\u5ea6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>MultipleLocator<\/code>\u6765\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.ticker as ticker<\/p>\n<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u8868<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6a2a\u5750\u6807\u548c\u7eb5\u5750\u6807\u6807\u7b7e<\/strong><\/h2>\n<p>plt.xlabel(&#39;X-axis (Float)&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u7684\u8f74<\/strong><\/h2>\n<p>ax = plt.gca()<\/p>\n<h2><strong>\u8bbe\u7f6e\u6a2a\u5750\u6807\u5bc6\u96c6\u523b\u5ea6<\/strong><\/h2>\n<p>ax.xaxis.set_major_locator(ticker.MultipleLocator(0.5))<\/p>\n<p>ax.xaxis.set_major_formatter(ticker.FormatStrFormatter(&#39;%.2f&#39;))<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u65cb\u8f6c\u523b\u5ea6\u6807\u7b7e<\/h4>\n<\/p>\n<p><p>\u5f53\u523b\u5ea6\u6807\u7b7e\u8fc7\u4e8e\u5bc6\u96c6\u65f6\uff0c\u53ef\u4ee5\u65cb\u8f6c\u523b\u5ea6\u6807\u7b7e\u6765\u907f\u514d\u91cd\u53e0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u6570\u636e<\/p>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u7ed8\u5236\u56fe\u8868<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u8bbe\u7f6e\u6a2a\u5750\u6807\u548c\u7eb5\u5750\u6807\u6807\u7b7e<\/strong><\/h2>\n<p>plt.xlabel(&#39;X-axis (Float)&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49\u523b\u5ea6\u4f4d\u7f6e\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.xticks(np.arange(0, 11, 1), [f&#39;{i:.1f}&#39; for i in np.arange(0, 11, 1)], rotation=45)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c<strong>\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u5e93\u6765\u663e\u793a\u6d6e\u70b9\u6570\u4f5c\u4e3a\u6a2a\u5750\u6807<\/strong>\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u683c\u5f0f\u5316\u51fd\u6570\u3001\u81ea\u5b9a\u4e49\u523b\u5ea6\u3001\u8bbe\u7f6e\u523b\u5ea6\u5bc6\u5ea6\u548c\u65cb\u8f6c\u523b\u5ea6\u6807\u7b7e\u7b49\u65b9\u5f0f\u8fdb\u884c\u9ad8\u7ea7\u8bbe\u7f6e\u3002<strong>\u8fd9\u4e9b\u529f\u80fd\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u7075\u6d3b\u5730\u63a7\u5236\u56fe\u8868\u7684\u663e\u793a\u6548\u679c<\/strong>\uff0c\u4ece\u800c\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u4f60\u80fd\u591f\u638c\u63e1\u5728Python\u4e2d\u5982\u4f55\u8bbe\u7f6e\u6a2a\u5750\u6807\u663e\u793a\u6d6e\u70b9\u6570\uff0c\u5e76\u4e14\u80fd\u591f\u6839\u636e\u9700\u8981\u8fdb\u884c\u76f8\u5e94\u7684\u5b9a\u5236\u548c\u8c03\u6574\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u6a2a\u5750\u6807\u4e3a\u6d6e\u70b9\u6570\u683c\u5f0f\uff1f<\/strong><br \/>\u5728Python\u7684\u7ed8\u56fe\u5e93\u4e2d\uff0c\u4f8b\u5982Matplotlib\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>plt.xticks()<\/code>\u51fd\u6570\u6765\u8bbe\u7f6e\u6a2a\u5750\u6807\u663e\u793a\u4e3a\u6d6e\u70b9\u6570\u683c\u5f0f\u3002\u53ef\u4ee5\u901a\u8fc7<code>format<\/code>\u53c2\u6570\u6307\u5b9a\u663e\u793a\u7684\u683c\u5f0f\uff0c\u4f8b\u5982<code>&#39;%.2f&#39;<\/code>\u6765\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u5e93\u751f\u6210\u6d6e\u70b9\u6570\u7684\u6570\u7ec4\u4f5c\u4e3a\u6a2a\u5750\u6807\u3002<\/p>\n<p><strong>\u5728\u7ed8\u56fe\u65f6\uff0c\u5982\u4f55\u81ea\u5b9a\u4e49\u6d6e\u70b9\u6570\u7684\u663e\u793a\u8303\u56f4\u548c\u95f4\u9694\uff1f<\/strong><br \/>\u81ea\u5b9a\u4e49\u6d6e\u70b9\u6570\u7684\u663e\u793a\u8303\u56f4\u548c\u95f4\u9694\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>plt.xlim()<\/code>\u548c<code>plt.xticks()<\/code>\u6765\u5b9e\u73b0\u3002<code>plt.xlim()<\/code>\u53ef\u4ee5\u7528\u6765\u6307\u5b9a\u6a2a\u5750\u6807\u7684\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\uff0c\u800c<code>plt.xticks()<\/code>\u5141\u8bb8\u60a8\u6307\u5b9a\u81ea\u5b9a\u4e49\u7684\u6d6e\u70b9\u6570\u95f4\u9694\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u6d6e\u70b9\u6570\u7684\u5217\u8868\u4f5c\u4e3a\u95f4\u9694\uff0c\u7136\u540e\u5c06\u5176\u4f20\u9012\u7ed9<code>plt.xticks()<\/code>\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u6211\u7684\u6a2a\u5750\u6807\u6d6e\u70b9\u6570\u663e\u793a\u4e0d\u6b63\u5e38\uff1f<\/strong><br \/>\u5982\u679c\u6a2a\u5750\u6807\u7684\u6d6e\u70b9\u6570\u663e\u793a\u4e0d\u6b63\u5e38\uff0c\u53ef\u80fd\u662f\u7531\u4e8e\u6570\u636e\u7684\u8303\u56f4\u8fc7\u5927\u6216\u8fc7\u5c0f\uff0c\u5bfc\u81f4\u9ed8\u8ba4\u7684\u523b\u5ea6\u8bbe\u7f6e\u4e0d\u9002\u7528\u3002\u60a8\u53ef\u4ee5\u5c1d\u8bd5\u624b\u52a8\u8bbe\u7f6e\u6a2a\u5750\u6807\u7684\u8303\u56f4\u548c\u523b\u5ea6\uff0c\u6216\u68c0\u67e5\u6570\u636e\u7c7b\u578b\u662f\u5426\u4e3a\u6d6e\u70b9\u6570\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u4f7f\u7528\u9002\u5f53\u7684\u683c\u5f0f\u5316\u51fd\u6570\u4ee5\u907f\u514d\u79d1\u5b66\u8ba1\u6570\u6cd5\u663e\u793a\u4e0d\u4fbf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5982\u679c\u4f60\u60f3\u5728\u7ed8\u5236\u56fe\u8868\u65f6\u663e\u793a\u6d6e\u70b9\u6570\u4f5c\u4e3a\u6a2a\u5750\u6807\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3002Matplotli [&hellip;]","protected":false},"author":3,"featured_media":1076264,"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\/1076254"}],"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=1076254"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076254\/revisions"}],"predecessor-version":[{"id":1076267,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1076254\/revisions\/1076267"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1076264"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1076254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1076254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1076254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}