{"id":1167928,"date":"2025-01-15T15:51:48","date_gmt":"2025-01-15T07:51:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1167928.html"},"modified":"2025-01-15T15:51:50","modified_gmt":"2025-01-15T07:51:50","slug":"python-%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%8f%8cy%e8%bd%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1167928.html","title":{"rendered":"python \u5982\u4f55\u5b9e\u73b0\u53ccy\u8f74"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26065312\/a7ead655-7faf-4c33-8a61-ce0310b02aba.webp\" alt=\"python \u5982\u4f55\u5b9e\u73b0\u53ccy\u8f74\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Matplotlib\u5e93\u3001\u9002\u914d\u4e0d\u540c\u7684\u6570\u636e\u8303\u56f4\u3001\u63d0\u5347\u56fe\u8868\u7684\u53ef\u8bfb\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u5b9e\u73b0\u53ccy\u8f74\u56fe\u8868\u7684\u4e00\u4e2a\u5e38\u89c1\u65b9\u5f0f\u662f\u4f7f\u7528Matplotlib\u5e93\u3002Matplotlib\u662fPython\u4e2d\u6700\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u80fd\u5236\u4f5c\u9ad8\u8d28\u91cf\u7684\u56fe\u8868\u3002\u901a\u8fc7\u4f7f\u7528<code>twinx()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u540c\u4e00\u4e2a\u56fe\u8868\u4e2d\u521b\u5efa\u4e00\u4e2a\u5171\u4eabx\u8f74\u7684\u53ccy\u8f74\u56fe\u8868\u3002\u8fd9\u6837\u53ef\u4ee5\u9002\u914d\u4e0d\u540c\u7684\u6570\u636e\u8303\u56f4\uff0c\u63d0\u5347\u56fe\u8868\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5177\u4f53\u5b9e\u73b0\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [0, 1, 2, 3, 4, 5]<\/p>\n<p>y1 = [0, 1, 4, 9, 16, 25]<\/p>\n<p>y2 = [0, 1, 2, 3, 4, 5]<\/p>\n<p>fig, ax1 = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u7b2c\u4e00\u4e2ay\u8f74\u7684\u56fe\u8868<\/strong><\/h2>\n<p>ax1.plot(x, y1, &#39;g-&#39;)<\/p>\n<p>ax1.set_xlabel(&#39;X data&#39;)<\/p>\n<p>ax1.set_ylabel(&#39;Y1 data&#39;, color=&#39;g&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2ay\u8f74<\/strong><\/h2>\n<p>ax2 = ax1.twinx()<\/p>\n<p>ax2.plot(x, y2, &#39;b-&#39;)<\/p>\n<p>ax2.set_ylabel(&#39;Y2 data&#39;, color=&#39;b&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Matplotlib\u5e93<\/h3>\n<\/p>\n<p><p>Matplotlib\u662fPython\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u80fd\u591f\u751f\u6210\u5404\u7c7b2D\u56fe\u8868\u3002\u901a\u8fc7Matplotlib\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u5305\u62ec\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u6563\u70b9\u56fe\u7b49\u5728\u5185\u7684\u5404\u79cd\u56fe\u8868\u3002\u4f7f\u7528<code>twinx()<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u56fe\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u5171\u4eabx\u8f74\u7684\u53ccy\u8f74\u3002<\/p>\n<\/p>\n<p><h4>\u57fa\u672c\u7ed8\u56fe<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165Matplotlib\u5e93\u5e76\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u56fe\u8868\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>plt.subplots()<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5355\u4e2a\u5b50\u56fe\u7684\u56fe\u8868\u5bf9\u8c61\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>fig, ax1 = plt.subplots()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6dfb\u52a0\u7b2c\u4e00\u4e2ay\u8f74<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u56fe\u8868\u4e0a\u7ed8\u5236\u7b2c\u4e00\u4e2ay\u8f74\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ax1.plot()<\/code>\u65b9\u6cd5\u7ed8\u5236\u6570\u636e\uff0c\u5e76\u8bbe\u7f6ey\u8f74\u7684\u6807\u7b7e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = [0, 1, 2, 3, 4, 5]<\/p>\n<p>y1 = [0, 1, 4, 9, 16, 25]<\/p>\n<p>ax1.plot(x, y1, &#39;g-&#39;)<\/p>\n<p>ax1.set_xlabel(&#39;X data&#39;)<\/p>\n<p>ax1.set_ylabel(&#39;Y1 data&#39;, color=&#39;g&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6dfb\u52a0\u7b2c\u4e8c\u4e2ay\u8f74<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u6dfb\u52a0\u7b2c\u4e8c\u4e2ay\u8f74\uff0c\u6211\u4eec\u4f7f\u7528<code>twinx()<\/code>\u65b9\u6cd5\u3002\u8fd9\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684y\u8f74\u5bf9\u8c61<code>ax2<\/code>\uff0c\u4e0e\u7b2c\u4e00\u4e2ay\u8f74\u5171\u4eab\u76f8\u540c\u7684x\u8f74\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax2 = ax1.twinx()<\/p>\n<p>y2 = [0, 1, 2, 3, 4, 5]<\/p>\n<p>ax2.plot(x, y2, &#39;b-&#39;)<\/p>\n<p>ax2.set_ylabel(&#39;Y2 data&#39;, color=&#39;b&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5b8c\u6574\u793a\u4f8b<\/h4>\n<\/p>\n<p><p>\u7efc\u5408\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u5b8c\u6574\u7684\u53ccy\u8f74\u56fe\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = [0, 1, 2, 3, 4, 5]<\/p>\n<p>y1 = [0, 1, 4, 9, 16, 25]<\/p>\n<p>y2 = [0, 1, 2, 3, 4, 5]<\/p>\n<p>fig, ax1 = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u7b2c\u4e00\u4e2ay\u8f74\u7684\u56fe\u8868<\/strong><\/h2>\n<p>ax1.plot(x, y1, &#39;g-&#39;)<\/p>\n<p>ax1.set_xlabel(&#39;X data&#39;)<\/p>\n<p>ax1.set_ylabel(&#39;Y1 data&#39;, color=&#39;g&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2ay\u8f74<\/strong><\/h2>\n<p>ax2 = ax1.twinx()<\/p>\n<p>ax2.plot(x, y2, &#39;b-&#39;)<\/p>\n<p>ax2.set_ylabel(&#39;Y2 data&#39;, color=&#39;b&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u9002\u914d\u4e0d\u540c\u7684\u6570\u636e\u8303\u56f4<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4e0d\u540c\u7684\u6570\u636e\u96c6\u53ef\u80fd\u5177\u6709\u4e0d\u540c\u7684\u6570\u503c\u8303\u56f4\u3002\u4f7f\u7528\u53ccy\u8f74\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u9002\u914d\u8fd9\u4e9b\u6570\u636e\u8303\u56f4\uff0c\u4f7f\u5f97\u56fe\u8868\u66f4\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b1\uff1a\u4e0d\u540c\u91cf\u7ea7\u7684\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u8003\u8651\u4e00\u4e2a\u5305\u542b\u6e29\u5ea6\u548c\u6e7f\u5ea6\u7684\u6570\u636e\u96c6\u3002\u6e29\u5ea6\u4ee5\u6444\u6c0f\u5ea6\u4e3a\u5355\u4f4d\uff0c\u800c\u6e7f\u5ea6\u4ee5\u767e\u5206\u6bd4\u4e3a\u5355\u4f4d\u3002\u4e24\u4e2a\u6570\u636e\u96c6\u7684\u6570\u503c\u8303\u56f4\u53ef\u80fd\u975e\u5e38\u4e0d\u540c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = range(0, 24)<\/p>\n<p>temperature = [15, 16, 17, 18, 20, 22, 25, 28, 30, 32, 33, 34, 35, 34, 33, 31, 29, 27, 24, 22, 20, 18, 17, 16]<\/p>\n<p>humidity = [80, 82, 85, 88, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 25, 30, 35, 40, 45]<\/p>\n<p>fig, ax1 = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u6e29\u5ea6\u6570\u636e<\/strong><\/h2>\n<p>ax1.plot(x, temperature, &#39;r-&#39;)<\/p>\n<p>ax1.set_xlabel(&#39;Time (hours)&#39;)<\/p>\n<p>ax1.set_ylabel(&#39;Temperature (C)&#39;, color=&#39;r&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2ay\u8f74<\/strong><\/h2>\n<p>ax2 = ax1.twinx()<\/p>\n<p>ax2.plot(x, humidity, &#39;b-&#39;)<\/p>\n<p>ax2.set_ylabel(&#39;Humidity (%)&#39;, color=&#39;b&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u793a\u4f8b2\uff1a\u80a1\u7968\u4ef7\u683c\u4e0e\u6210\u4ea4\u91cf<\/h4>\n<\/p>\n<p><p>\u53e6\u4e00\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\u662f\u80a1\u7968\u4ef7\u683c\u4e0e\u6210\u4ea4\u91cf\u3002\u80a1\u7968\u4ef7\u683c\u548c\u6210\u4ea4\u91cf\u7684\u6570\u636e\u8303\u56f4\u901a\u5e38\u5dee\u5f02\u5f88\u5927\uff0c\u901a\u8fc7\u53ccy\u8f74\u53ef\u4ee5\u66f4\u597d\u5730\u5c55\u793a\u8fd9\u4e24\u7c7b\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>dates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/p>\n<p>prices = [100, 101, 102, 101, 99, 98, 97, 96, 95, 94]<\/p>\n<p>volumes = [200, 220, 210, 230, 240, 250, 260, 270, 280, 290]<\/p>\n<p>fig, ax1 = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u80a1\u7968\u4ef7\u683c<\/strong><\/h2>\n<p>ax1.plot(dates, prices, &#39;g-&#39;)<\/p>\n<p>ax1.set_xlabel(&#39;Date&#39;)<\/p>\n<p>ax1.set_ylabel(&#39;Stock Price&#39;, color=&#39;g&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2ay\u8f74<\/strong><\/h2>\n<p>ax2 = ax1.twinx()<\/p>\n<p>ax2.bar(dates, volumes, alpha=0.6, color=&#39;b&#39;)<\/p>\n<p>ax2.set_ylabel(&#39;Volume&#39;, color=&#39;b&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u63d0\u5347\u56fe\u8868\u7684\u53ef\u8bfb\u6027<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u5347\u56fe\u8868\u7684\u53ef\u8bfb\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u56fe\u8868\u8fdb\u884c\u4e00\u4e9b\u7f8e\u5316\u548c\u8c03\u6574\u3002\u5305\u62ec\u6dfb\u52a0\u6807\u9898\u3001\u56fe\u4f8b\u3001\u7f51\u683c\u7ebf\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u6dfb\u52a0\u6807\u9898\u548c\u56fe\u4f8b<\/h4>\n<\/p>\n<p><p>\u4e3a\u56fe\u8868\u6dfb\u52a0\u6807\u9898\u548c\u56fe\u4f8b\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u56fe\u8868\u7684\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = range(0, 24)<\/p>\n<p>temperature = [15, 16, 17, 18, 20, 22, 25, 28, 30, 32, 33, 34, 35, 34, 33, 31, 29, 27, 24, 22, 20, 18, 17, 16]<\/p>\n<p>humidity = [80, 82, 85, 88, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 25, 30, 35, 40, 45]<\/p>\n<p>fig, ax1 = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u6e29\u5ea6\u6570\u636e<\/strong><\/h2>\n<p>ax1.plot(x, temperature, &#39;r-&#39;, label=&#39;Temperature&#39;)<\/p>\n<p>ax1.set_xlabel(&#39;Time (hours)&#39;)<\/p>\n<p>ax1.set_ylabel(&#39;Temperature (C)&#39;, color=&#39;r&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2ay\u8f74<\/strong><\/h2>\n<p>ax2 = ax1.twinx()<\/p>\n<p>ax2.plot(x, humidity, &#39;b-&#39;, label=&#39;Humidity&#39;)<\/p>\n<p>ax2.set_ylabel(&#39;Humidity (%)&#39;, color=&#39;b&#39;)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u56fe\u4f8b<\/strong><\/h2>\n<p>fig.suptitle(&#39;Temperature and Humidity Over Time&#39;)<\/p>\n<p>ax1.legend(loc=&#39;upper left&#39;)<\/p>\n<p>ax2.legend(loc=&#39;upper right&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6dfb\u52a0\u7f51\u683c\u7ebf<\/h4>\n<\/p>\n<p><p>\u7f51\u683c\u7ebf\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u66f4\u5bb9\u6613\u5730\u8bfb\u53d6\u6570\u636e\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>grid()<\/code>\u65b9\u6cd5\u6dfb\u52a0\u7f51\u683c\u7ebf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = range(0, 24)<\/p>\n<p>temperature = [15, 16, 17, 18, 20, 22, 25, 28, 30, 32, 33, 34, 35, 34, 33, 31, 29, 27, 24, 22, 20, 18, 17, 16]<\/p>\n<p>humidity = [80, 82, 85, 88, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 25, 30, 35, 40, 45]<\/p>\n<p>fig, ax1 = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u6e29\u5ea6\u6570\u636e<\/strong><\/h2>\n<p>ax1.plot(x, temperature, &#39;r-&#39;, label=&#39;Temperature&#39;)<\/p>\n<p>ax1.set_xlabel(&#39;Time (hours)&#39;)<\/p>\n<p>ax1.set_ylabel(&#39;Temperature (C)&#39;, color=&#39;r&#39;)<\/p>\n<p>ax1.grid(True)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2ay\u8f74<\/strong><\/h2>\n<p>ax2 = ax1.twinx()<\/p>\n<p>ax2.plot(x, humidity, &#39;b-&#39;, label=&#39;Humidity&#39;)<\/p>\n<p>ax2.set_ylabel(&#39;Humidity (%)&#39;, color=&#39;b&#39;)<\/p>\n<p>ax2.grid(False)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u56fe\u4f8b<\/strong><\/h2>\n<p>fig.suptitle(&#39;Temperature and Humidity Over Time&#39;)<\/p>\n<p>ax1.legend(loc=&#39;upper left&#39;)<\/p>\n<p>ax2.legend(loc=&#39;upper right&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u81ea\u5b9a\u4e49x\u8f74\u548cy\u8f74\u7684\u523b\u5ea6<\/h4>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u8f74\u7684\u523b\u5ea6\u53ef\u4ee5\u4f7f\u56fe\u8868\u66f4\u6613\u4e8e\u9605\u8bfb\u3002\u53ef\u4ee5\u4f7f\u7528<code>set_xticks()<\/code>\u548c<code>set_yticks()<\/code>\u65b9\u6cd5\u81ea\u5b9a\u4e49\u523b\u5ea6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = range(0, 24)<\/p>\n<p>temperature = [15, 16, 17, 18, 20, 22, 25, 28, 30, 32, 33, 34, 35, 34, 33, 31, 29, 27, 24, 22, 20, 18, 17, 16]<\/p>\n<p>humidity = [80, 82, 85, 88, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 25, 30, 35, 40, 45]<\/p>\n<p>fig, ax1 = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u6e29\u5ea6\u6570\u636e<\/strong><\/h2>\n<p>ax1.plot(x, temperature, &#39;r-&#39;, label=&#39;Temperature&#39;)<\/p>\n<p>ax1.set_xlabel(&#39;Time (hours)&#39;)<\/p>\n<p>ax1.set_ylabel(&#39;Temperature (C)&#39;, color=&#39;r&#39;)<\/p>\n<p>ax1.grid(True)<\/p>\n<p>ax1.set_xticks(range(0, 24, 2))  # \u8bbe\u7f6ex\u8f74\u523b\u5ea6<\/p>\n<p>ax1.set_yticks(range(15, 36, 5))  # \u8bbe\u7f6ey\u8f74\u523b\u5ea6<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2ay\u8f74<\/strong><\/h2>\n<p>ax2 = ax1.twinx()<\/p>\n<p>ax2.plot(x, humidity, &#39;b-&#39;, label=&#39;Humidity&#39;)<\/p>\n<p>ax2.set_ylabel(&#39;Humidity (%)&#39;, color=&#39;b&#39;)<\/p>\n<p>ax2.grid(False)<\/p>\n<p>ax2.set_yticks(range(20, 101, 10))  # \u8bbe\u7f6ey\u8f74\u523b\u5ea6<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u56fe\u4f8b<\/strong><\/h2>\n<p>fig.suptitle(&#39;Temperature and Humidity Over Time&#39;)<\/p>\n<p>ax1.legend(loc=&#39;upper left&#39;)<\/p>\n<p>ax2.legend(loc=&#39;upper right&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u9ad8\u7ea7\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u7840\u7684\u53ccy\u8f74\u7ed8\u56fe\uff0cMatplotlib\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7279\u6027\uff0c\u53ef\u4ee5\u6ee1\u8db3\u66f4\u591a\u590d\u6742\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>\u4e0d\u540c\u7c7b\u578b\u7684\u56fe\u8868\u7ec4\u5408<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u56fe\u8868\u4e2d\u7ec4\u5408\u4e0d\u540c\u7c7b\u578b\u7684\u56fe\u8868\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5c06\u6298\u7ebf\u56fe\u548c\u67f1\u72b6\u56fe\u7ec4\u5408\u5728\u4e00\u8d77\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = range(0, 24)<\/p>\n<p>temperature = [15, 16, 17, 18, 20, 22, 25, 28, 30, 32, 33, 34, 35, 34, 33, 31, 29, 27, 24, 22, 20, 18, 17, 16]<\/p>\n<p>humidity = [80, 82, 85, 88, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 25, 30, 35, 40, 45]<\/p>\n<p>fig, ax1 = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u6e29\u5ea6\u6570\u636e\uff08\u6298\u7ebf\u56fe\uff09<\/strong><\/h2>\n<p>ax1.plot(x, temperature, &#39;r-&#39;, label=&#39;Temperature&#39;)<\/p>\n<p>ax1.set_xlabel(&#39;Time (hours)&#39;)<\/p>\n<p>ax1.set_ylabel(&#39;Temperature (C)&#39;, color=&#39;r&#39;)<\/p>\n<p>ax1.grid(True)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2ay\u8f74<\/strong><\/h2>\n<p>ax2 = ax1.twinx()<\/p>\n<p>ax2.bar(x, humidity, alpha=0.6, color=&#39;b&#39;, label=&#39;Humidity&#39;)<\/p>\n<p>ax2.set_ylabel(&#39;Humidity (%)&#39;, color=&#39;b&#39;)<\/p>\n<p>ax2.grid(False)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u56fe\u4f8b<\/strong><\/h2>\n<p>fig.suptitle(&#39;Temperature and Humidity Over Time&#39;)<\/p>\n<p>ax1.legend(loc=&#39;upper left&#39;)<\/p>\n<p>ax2.legend(loc=&#39;upper right&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u6dfb\u52a0\u6ce8\u91ca<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u8868\u4e2d\u6dfb\u52a0\u6ce8\u91ca\u53ef\u4ee5\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528<code>annotate()<\/code>\u65b9\u6cd5\u6dfb\u52a0\u6ce8\u91ca\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = range(0, 24)<\/p>\n<p>temperature = [15, 16, 17, 18, 20, 22, 25, 28, 30, 32, 33, 34, 35, 34, 33, 31, 29, 27, 24, 22, 20, 18, 17, 16]<\/p>\n<p>humidity = [80, 82, 85, 88, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 25, 30, 35, 40, 45]<\/p>\n<p>fig, ax1 = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u6e29\u5ea6\u6570\u636e\uff08\u6298\u7ebf\u56fe\uff09<\/strong><\/h2>\n<p>ax1.plot(x, temperature, &#39;r-&#39;, label=&#39;Temperature&#39;)<\/p>\n<p>ax1.set_xlabel(&#39;Time (hours)&#39;)<\/p>\n<p>ax1.set_ylabel(&#39;Temperature (C)&#39;, color=&#39;r&#39;)<\/p>\n<p>ax1.grid(True)<\/p>\n<h2><strong>\u6dfb\u52a0\u6ce8\u91ca<\/strong><\/h2>\n<p>ax1.annotate(&#39;Peak Temperature&#39;, xy=(12, 35), xytext=(15, 30),<\/p>\n<p>             arrowprops=dict(facecolor=&#39;black&#39;, shrink=0.05))<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2ay\u8f74<\/strong><\/h2>\n<p>ax2 = ax1.twinx()<\/p>\n<p>ax2.bar(x, humidity, alpha=0.6, color=&#39;b&#39;, label=&#39;Humidity&#39;)<\/p>\n<p>ax2.set_ylabel(&#39;Humidity (%)&#39;, color=&#39;b&#39;)<\/p>\n<p>ax2.grid(False)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u56fe\u4f8b<\/strong><\/h2>\n<p>fig.suptitle(&#39;Temperature and Humidity Over Time&#39;)<\/p>\n<p>ax1.legend(loc=&#39;upper left&#39;)<\/p>\n<p>ax2.legend(loc=&#39;upper right&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u81ea\u5b9a\u4e49\u989c\u8272\u548c\u6837\u5f0f<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u81ea\u5b9a\u4e49\u56fe\u8868\u7684\u989c\u8272\u548c\u6837\u5f0f\uff0c\u4f7f\u5176\u66f4\u7b26\u5408\u6211\u4eec\u7684\u9700\u6c42\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x = range(0, 24)<\/p>\n<p>temperature = [15, 16, 17, 18, 20, 22, 25, 28, 30, 32, 33, 34, 35, 34, 33, 31, 29, 27, 24, 22, 20, 18, 17, 16]<\/p>\n<p>humidity = [80, 82, 85, 88, 90, 85, 80, 75, 70, 65, 60, 55, 50, 45, 40, 35, 30, 25, 20, 25, 30, 35, 40, 45]<\/p>\n<p>fig, ax1 = plt.subplots()<\/p>\n<h2><strong>\u7ed8\u5236\u6e29\u5ea6\u6570\u636e\uff08\u6298\u7ebf\u56fe\uff09<\/strong><\/h2>\n<p>ax1.plot(x, temperature, &#39;r--&#39;, label=&#39;Temperature&#39;)  # \u81ea\u5b9a\u4e49\u7ebf\u6761\u6837\u5f0f<\/p>\n<p>ax1.set_xlabel(&#39;Time (hours)&#39;)<\/p>\n<p>ax1.set_ylabel(&#39;Temperature (C)&#39;, color=&#39;r&#39;)<\/p>\n<p>ax1.grid(True)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u4e2ay\u8f74<\/strong><\/h2>\n<p>ax2 = ax1.twinx()<\/p>\n<p>ax2.bar(x, humidity, alpha=0.6, color=&#39;b&#39;, label=&#39;Humidity&#39;)<\/p>\n<p>ax2.set_ylabel(&#39;Humidity (%)&#39;, color=&#39;b&#39;)<\/p>\n<p>ax2.grid(False)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u56fe\u4f8b<\/strong><\/h2>\n<p>fig.suptitle(&#39;Temperature and Humidity Over Time&#39;)<\/p>\n<p>ax1.legend(loc=&#39;upper left&#39;)<\/p>\n<p>ax2.legend(loc=&#39;upper right&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528Matplotlib\u5e93\u5b9e\u73b0\u529f\u80fd\u4e30\u5bcc\u7684\u53ccy\u8f74\u56fe\u8868\uff0c\u9002\u914d\u4e0d\u540c\u7684\u6570\u636e\u8303\u56f4\uff0c\u63d0\u5347\u56fe\u8868<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u53ccY\u8f74\u56fe\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u521b\u5efa\u53ccY\u8f74\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u3002\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u5bf9\u8c61\u5e76\u6dfb\u52a0\u4e24\u4e2aY\u8f74\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5Matplotlib\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>twinx()<\/code>\u65b9\u6cd5\u521b\u5efa\u7b2c\u4e8c\u4e2aY\u8f74\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\n# \u793a\u4f8b\u6570\u636e\nx = [1, 2, 3, 4, 5]\ny1 = [10, 20, 25, 30, 35]\ny2 = [5, 15, 20, 25, 30]\n\n# \u521b\u5efa\u56fe\u5f62\u548c\u7b2c\u4e00\u4e2aY\u8f74\nfig, ax1 = plt.subplots()\n\n# \u7ed8\u5236\u7b2c\u4e00\u4e2aY\u8f74\u7684\u6570\u636e\nax1.plot(x, y1, &#39;g-&#39;, label=&#39;Y1 Data&#39;)\nax1.set_ylabel(&#39;Y1 Axis&#39;, color=&#39;g&#39;)\n\n# \u521b\u5efa\u7b2c\u4e8c\u4e2aY\u8f74\nax2 = ax1.twinx()\nax2.plot(x, y2, &#39;b-&#39;, label=&#39;Y2 Data&#39;)\nax2.set_ylabel(&#39;Y2 Axis&#39;, color=&#39;b&#39;)\n\n# \u663e\u793a\u56fe\u4f8b\nax1.legend(loc=&#39;upper left&#39;)\nax2.legend(loc=&#39;upper right&#39;)\n\nplt.show()\n<\/code><\/pre>\n<p><strong>\u4f7f\u7528\u53ccY\u8f74\u65f6\u9700\u8981\u6ce8\u610f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u53ccY\u8f74\u65f6\uff0c\u786e\u4fdd\u4e24\u4e2aY\u8f74\u7684\u91cf\u7eb2\u548c\u6570\u636e\u8303\u56f4\u6709\u6240\u4e0d\u540c\uff0c\u4ee5\u907f\u514d\u6df7\u6dc6\u8bfb\u8005\u3002\u5408\u7406\u9009\u62e9\u989c\u8272\u548c\u7ebf\u578b\uff0c\u786e\u4fdd\u56fe\u5f62\u6e05\u6670\u6613\u8bfb\u3002\u6b64\u5916\uff0c\u56fe\u4f8b\u7684\u4f4d\u7f6e\u4e5f\u8981\u9002\u5f53\uff0c\u4ee5\u4fbf\u89c2\u4f17\u80fd\u591f\u8f7b\u677e\u7406\u89e3\u6bcf\u4e2a\u6570\u636e\u7cfb\u5217\u7684\u542b\u4e49\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u7528\u7684Python\u5e93\u652f\u6301\u53ccY\u8f74\u7ed8\u56fe\uff1f<\/strong><br \/>\u9664\u4e86Matplotlib\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9bPython\u5e93\u53ef\u4ee5\u652f\u6301\u53ccY\u8f74\u7ed8\u56fe\uff0c\u4f8b\u5982Seaborn\u548cPlotly\u3002Seaborn\u662f\u4e00\u4e2a\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u56fe\u5f62\u5e93\uff0c\u5177\u6709\u66f4\u7b80\u6d01\u7684API\uff0c\u800cPlotly\u5219\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u521b\u5efa\u52a8\u6001\u548c\u54cd\u5e94\u5f0f\u7684\u56fe\u5f62\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u4f7f\u7528\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Matplotlib\u5e93\u3001\u9002\u914d\u4e0d\u540c\u7684\u6570\u636e\u8303\u56f4\u3001\u63d0\u5347\u56fe\u8868\u7684\u53ef\u8bfb\u6027 \u5b9e\u73b0\u53ccy\u8f74\u56fe\u8868\u7684\u4e00\u4e2a\u5e38\u89c1\u65b9\u5f0f\u662f\u4f7f\u7528Matpl [&hellip;]","protected":false},"author":3,"featured_media":1167934,"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\/1167928"}],"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=1167928"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167928\/revisions"}],"predecessor-version":[{"id":1167937,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167928\/revisions\/1167937"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1167934"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1167928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1167928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1167928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}