{"id":1144807,"date":"2025-01-08T23:02:54","date_gmt":"2025-01-08T15:02:54","guid":{"rendered":""},"modified":"2025-01-08T23:02:59","modified_gmt":"2025-01-08T15:02:59","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e5%8f%8c%e7%ba%b5%e5%9d%90%e6%a0%87%e8%bd%b4%e7%9a%84%e5%9b%be%e6%8a%98%e7%ba%bf%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1144807.html","title":{"rendered":"python\u5982\u4f55\u753b\u53cc\u7eb5\u5750\u6807\u8f74\u7684\u56fe\u6298\u7ebf\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181720\/daa828af-d220-4523-a1cd-dbd62aa2e4ea.webp\" alt=\"python\u5982\u4f55\u753b\u53cc\u7eb5\u5750\u6807\u8f74\u7684\u56fe\u6298\u7ebf\u56fe\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u53cc\u7eb5\u5750\u6807\u8f74\u7ed8\u5236\u6298\u7ebf\u56fe\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7Matplotlib\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>twinx()<\/code> \u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u5171\u4eab\u540c\u4e00X\u8f74\u7684\u7b2c\u4e8c\u4e2aY\u8f74\u3002<\/strong> \u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u8ba9\u4f60\u5728\u540c\u4e00\u4e2a\u56fe\u8868\u4e0a\u5c55\u793a\u4e24\u4e2a\u4e0d\u540c\u6570\u636e\u96c6\u7684\u53d8\u5316\u8d8b\u52bf\uff0c\u4ece\u800c\u66f4\u5bb9\u6613\u5bf9\u6bd4\u548c\u5206\u6790\u6570\u636e\u3002<strong>\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u521b\u5efa\u6570\u636e\u3001\u521d\u59cb\u5316\u56fe\u8868\u3001\u7ed8\u5236\u7b2c\u4e00\u6761\u6298\u7ebf\u3001\u521b\u5efa\u7b2c\u4e8c\u4e2aY\u8f74\u3001\u7ed8\u5236\u7b2c\u4e8c\u6761\u6298\u7ebf\u3001\u6dfb\u52a0\u56fe\u4f8b\u548c\u6807\u9898\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7ed8\u56fe\u4e4b\u524d\uff0c\u9700\u8981\u5bfc\u5165Matplotlib\u5e93\u3002\u5982\u679c\u8fd8\u6ca1\u6709\u5b89\u88c5\u8fd9\u4e2a\u5e93\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><p>\u7136\u540e\u5728\u4f60\u7684Python\u811a\u672c\u4e2d\u5bfc\u5165\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<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u7ed8\u5236\u53cc\u7eb5\u5750\u6807\u8f74\u7684\u6298\u7ebf\u56fe\uff0c\u4f60\u9700\u8981\u4e24\u4e2a\u4e0d\u540c\u7684\u6570\u636e\u96c6\u3002\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528NumPy\u751f\u6210\u4e00\u4e9b\u793a\u4f8b\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = np.linspace(0, 10, 100)<\/p>\n<p>y1 = np.sin(x)<\/p>\n<p>y2 = np.cos(x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>x<\/code> \u662f\u4ece0\u523010\u7684100\u4e2a\u7b49\u95f4\u8ddd\u70b9\uff0c<code>y1<\/code> \u662f <code>x<\/code> \u5bf9\u5e94\u7684\u6b63\u5f26\u503c\uff0c<code>y2<\/code> \u662f <code>x<\/code> \u5bf9\u5e94\u7684\u4f59\u5f26\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u521d\u59cb\u5316\u56fe\u8868<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>plt.figure()<\/code> \u521d\u59cb\u5316\u4e00\u4e2a\u56fe\u8868\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, ax1 = plt.subplots()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7ed8\u5236\u7b2c\u4e00\u6761\u6298\u7ebf<\/h3>\n<\/p>\n<p><p>\u5728\u4e3bY\u8f74\u4e0a\u7ed8\u5236\u7b2c\u4e00\u6761\u6298\u7ebf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">color = &#39;tab:blue&#39;<\/p>\n<p>ax1.set_xlabel(&#39;X data&#39;)<\/p>\n<p>ax1.set_ylabel(&#39;Sin&#39;, color=color)<\/p>\n<p>ax1.plot(x, y1, color=color)<\/p>\n<p>ax1.tick_params(axis=&#39;y&#39;, labelcolor=color)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u8bbe\u7f6e\u4e86X\u8f74\u6807\u7b7e\u3001\u4e3bY\u8f74\u6807\u7b7e\uff0c\u5e76\u7ed8\u5236\u4e86 <code>y1<\/code> \u5bf9\u5e94\u7684\u6298\u7ebf\u56fe\u3002<code>tick_params<\/code> \u7528\u4e8e\u8bbe\u7f6eY\u8f74\u6807\u7b7e\u7684\u989c\u8272\uff0c\u4ee5\u4fbf\u4e0e\u6298\u7ebf\u989c\u8272\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u521b\u5efa\u7b2c\u4e8c\u4e2aY\u8f74<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528 <code>twinx()<\/code> \u521b\u5efa\u4e00\u4e2a\u65b0\u7684Y\u8f74\u5bf9\u8c61\uff0c\u5e76\u7ed8\u5236\u7b2c\u4e8c\u6761\u6298\u7ebf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax2 = ax1.twinx()  <\/p>\n<p>color = &#39;tab:red&#39;<\/p>\n<p>ax2.set_ylabel(&#39;Cos&#39;, color=color)<\/p>\n<p>ax2.plot(x, y2, color=color)<\/p>\n<p>ax2.tick_params(axis=&#39;y&#39;, labelcolor=color)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528 <code>twinx()<\/code> \u65b9\u6cd5\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684Y\u8f74\u5bf9\u8c61 <code>ax2<\/code>\uff0c\u5e76\u5728\u5176\u4e0a\u7ed8\u5236 <code>y2<\/code> \u5bf9\u5e94\u7684\u6298\u7ebf\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6dfb\u52a0\u56fe\u4f8b\u548c\u6807\u9898<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u4e3a\u56fe\u8868\u6dfb\u52a0\u56fe\u4f8b\u548c\u6807\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig.tight_layout()  <\/p>\n<p>plt.title(&#39;Double Y Axis Line Chart&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u786e\u4fdd\u4e86\u5b50\u56fe\u4e4b\u95f4\u6ca1\u6709\u91cd\u53e0\uff0c\u5e76\u663e\u793a\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u8be6\u7ec6\u63cf\u8ff0<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u53cc\u7eb5\u5750\u6807\u8f74\u7ed8\u5236\u6298\u7ebf\u56fe\u7684\u4e3b\u8981\u4f18\u52bf\u662f\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u56fe\u8868\u4e0a\u5c55\u793a\u4e24\u4e2a\u4e0d\u540c\u6570\u636e\u96c6\u7684\u53d8\u5316\u8d8b\u52bf\uff0c\u4ece\u800c\u66f4\u5bb9\u6613\u5bf9\u6bd4\u548c\u5206\u6790\u6570\u636e\u3002<\/strong> \u4f8b\u5982\uff0c\u5728\u91d1\u878d\u6570\u636e\u5206\u6790\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u540c\u65f6\u5c55\u793a\u80a1\u7968\u4ef7\u683c\u548c\u4ea4\u6613\u91cf\uff0c\u901a\u8fc7\u53cc\u7eb5\u5750\u6807\u8f74\u53ef\u4ee5\u76f4\u89c2\u5730\u770b\u5230\u4e24\u8005\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9664\u4e86\u4e0a\u8ff0\u57fa\u672c\u6b65\u9aa4\uff0c\u8fd8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u66f4\u591a\u7684\u81ea\u5b9a\u4e49\u8bbe\u7f6e\uff0c\u4f8b\u5982\u8c03\u6574\u56fe\u4f8b\u4f4d\u7f6e\u3001\u8bbe\u7f6e\u7f51\u683c\u7ebf\u3001\u6dfb\u52a0\u6ce8\u91ca\u7b49\u3002Matplotlib \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684API\u63a5\u53e3\uff0c\u53ef\u4ee5\u6ee1\u8db3\u5404\u79cd\u590d\u6742\u7684\u7ed8\u56fe\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u5e94\u7528\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u8ba9\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u5b9e\u4f8b\u6765\u8fdb\u4e00\u6b65\u8bf4\u660e\u5982\u4f55\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u4f7f\u7528\u53cc\u7eb5\u5750\u6807\u8f74\u7ed8\u5236\u6298\u7ebf\u56fe\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6e29\u5ea6\u548c\u6e7f\u5ea6\u7684\u65f6\u95f4\u5e8f\u5217\u6570\u636e\uff0c\u9700\u8981\u5728\u540c\u4e00\u4e2a\u56fe\u8868\u4e0a\u5c55\u793a\u5b83\u4eec\u7684\u53d8\u5316\u8d8b\u52bf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>dates = pd.date_range(&#39;20230101&#39;, periods=10)<\/p>\n<p>temperature = [22, 23, 21, 20, 24, 25, 23, 22, 21, 20]<\/p>\n<p>humidity = [30, 35, 37, 32, 30, 29, 28, 27, 33, 34]<\/p>\n<h2><strong>\u521b\u5efaDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;Date&#39;: dates, &#39;Temperature&#39;: temperature, &#39;Humidity&#39;: humidity})<\/p>\n<h2><strong>\u521d\u59cb\u5316\u56fe\u8868<\/strong><\/h2>\n<p>fig, ax1 = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u6e29\u5ea6\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>color = &#39;tab:blue&#39;<\/p>\n<p>ax1.set_xlabel(&#39;Date&#39;)<\/p>\n<p>ax1.set_ylabel(&#39;Temperature (C)&#39;, color=color)<\/p>\n<p>ax1.plot(df[&#39;Date&#39;], df[&#39;Temperature&#39;], color=color)<\/p>\n<p>ax1.tick_params(axis=&#39;y&#39;, labelcolor=color)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2aY\u8f74<\/strong><\/h2>\n<p>ax2 = ax1.twinx()<\/p>\n<p>color = &#39;tab:red&#39;<\/p>\n<p>ax2.set_ylabel(&#39;Humidity (%)&#39;, color=color)<\/p>\n<p>ax2.plot(df[&#39;Date&#39;], df[&#39;Humidity&#39;], color=color)<\/p>\n<p>ax2.tick_params(axis=&#39;y&#39;, labelcolor=color)<\/p>\n<h2><strong>\u6dfb\u52a0\u56fe\u4f8b\u548c\u6807\u9898<\/strong><\/h2>\n<p>fig.tight_layout()<\/p>\n<p>plt.title(&#39;Temperature and Humidity Over Time&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u5b9e\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pandas\u5e93\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542b\u65e5\u671f\u3001\u6e29\u5ea6\u548c\u6e7f\u5ea6\u7684DataFrame\uff0c\u5e76\u5728\u540c\u4e00\u4e2a\u56fe\u8868\u4e0a\u5c55\u793a\u4e86\u6e29\u5ea6\u548c\u6e7f\u5ea6\u7684\u53d8\u5316\u8d8b\u52bf\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u76f4\u89c2\u5730\u5bf9\u6bd4\u548c\u5206\u6790\u4e24\u4e2a\u4e0d\u540c\u6570\u636e\u96c6\u7684\u53d8\u5316\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u5e93\u7684 <code>twinx()<\/code> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u540c\u4e00\u4e2a\u56fe\u8868\u4e0a\u521b\u5efa\u53cc\u7eb5\u5750\u6807\u8f74\u7684\u6298\u7ebf\u56fe\u3002<strong>\u8fd9\u79cd\u56fe\u8868\u5728\u6570\u636e\u5206\u6790\u4e2d\u975e\u5e38\u5b9e\u7528\uff0c\u7279\u522b\u662f\u9700\u8981\u540c\u65f6\u5c55\u793a\u591a\u4e2a\u4e0d\u540c\u6570\u636e\u96c6\u65f6\u3002<\/strong> \u901a\u8fc7\u5408\u7406\u8bbe\u7f6e\u56fe\u8868\u7684\u5404\u9879\u53c2\u6570\uff0c\u53ef\u4ee5\u4f7f\u56fe\u8868\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u7ed8\u5236\u53cc\u7eb5\u5750\u6807\u8f74\u7684\u6298\u7ebf\u56fe\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u53ef\u4ee5\u8f7b\u677e\u7ed8\u5236\u53cc\u7eb5\u5750\u6807\u8f74\u7684\u6298\u7ebf\u56fe\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5Matplotlib\u5e93\uff0c\u7136\u540e\u901a\u8fc7<code>twinx()<\/code>\u65b9\u6cd5\u521b\u5efa\u7b2c\u4e8c\u4e2ay\u8f74\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5e2e\u52a9\u4f60\u5feb\u901f\u5165\u95e8\u3002<\/p>\n<p><strong>\u53cc\u7eb5\u5750\u6807\u8f74\u56fe\u7684\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u53cc\u7eb5\u5750\u6807\u8f74\u56fe\u901a\u5e38\u7528\u4e8e\u5c55\u793a\u4e24\u7ec4\u5177\u6709\u4e0d\u540c\u91cf\u7eb2\u6216\u4e0d\u540c\u5355\u4f4d\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u5c55\u793a\u6c14\u6e29\uff08\u6444\u6c0f\u5ea6\uff09\u4e0e\u964d\u6c34\u91cf\uff08\u6beb\u7c73\uff09\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u6216\u8005\u9500\u552e\u989d\u4e0e\u5e7f\u544a\u652f\u51fa\u4e4b\u95f4\u7684\u8054\u7cfb\u3002\u4f7f\u7528\u53cc\u7eb5\u5750\u6807\u8f74\u53ef\u4ee5\u66f4\u76f4\u89c2\u5730\u6bd4\u8f83\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<p><strong>\u5728\u7ed8\u5236\u53cc\u7eb5\u5750\u6807\u8f74\u56fe\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u53cc\u7eb5\u5750\u6807\u8f74\u7ed8\u56fe\u65f6\uff0c\u786e\u4fdd\u6bcf\u4e2ay\u8f74\u7684\u523b\u5ea6\u548c\u6807\u7b7e\u6e05\u6670\u53ef\u8fa8\u3002\u907f\u514d\u56fe\u8868\u8fc7\u4e8e\u590d\u6742\uff0c\u5bfc\u81f4\u89c2\u4f17\u96be\u4ee5\u7406\u89e3\u3002\u540c\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u989c\u8272\u548c\u7ebf\u578b\uff0c\u4f7f\u5f97\u4e0d\u540c\u6570\u636e\u7cfb\u5217\u4e4b\u95f4\u7684\u5bf9\u6bd4\u66f4\u52a0\u660e\u663e\u3002\u6b64\u5916\uff0c\u9002\u5f53\u5730\u8bbe\u7f6e\u56fe\u4f8b\u4e5f\u662f\u589e\u5f3a\u56fe\u8868\u53ef\u8bfb\u6027\u7684\u5173\u952e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528\u53cc\u7eb5\u5750\u6807\u8f74\u7ed8\u5236\u6298\u7ebf\u56fe\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7Matplotlib\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 twinx() [&hellip;]","protected":false},"author":3,"featured_media":1144820,"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\/1144807"}],"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=1144807"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144807\/revisions"}],"predecessor-version":[{"id":1144823,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144807\/revisions\/1144823"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1144820"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1144807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1144807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1144807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}