{"id":1153085,"date":"2025-01-13T17:34:26","date_gmt":"2025-01-13T09:34:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1153085.html"},"modified":"2025-01-13T17:34:29","modified_gmt":"2025-01-13T09:34:29","slug":"python-%e5%a6%82%e4%bd%95%e8%a7%84%e5%ae%9ax%e8%8c%83%e5%9b%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1153085.html","title":{"rendered":"python \u5982\u4f55\u89c4\u5b9ax\u8303\u56f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25182916\/e6ed34f0-23dd-448b-88b9-b2f3c0d0e47d.webp\" alt=\"python \u5982\u4f55\u89c4\u5b9ax\u8303\u56f4\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u89c4\u5b9ax\u8303\u56f4\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4f60\u4f7f\u7528\u7684\u5e93\u548c\u4f60\u60f3\u8981\u8fbe\u5230\u7684\u6548\u679c\u3002<strong>\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u3001Matplotlib\u3001Pandas\u3001SciPy\u7b49\u5e93<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>\u4f7f\u7528Matplotlib\u8bbe\u7f6e\u7ed8\u56fe\u7684x\u8f74\u8303\u56f4<\/strong>\u662f\u6bd4\u8f83\u5e38\u89c1\u7684\u65b9\u5f0f\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7<code>plt.xlim()<\/code>\u51fd\u6570\u6765\u8bbe\u7f6ex\u8f74\u7684\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u3002\u4f8b\u5982\uff0c<code>plt.xlim(0, 10)<\/code>\u5c06x\u8f74\u8303\u56f4\u8bbe\u7f6e\u4e3a\u4ece0\u523010\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u79cd\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u5176\u4ed6\u65b9\u6cd5\u8fdb\u884c\u5bf9\u6bd4\u548c\u8865\u5145\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Matplotlib\u8bbe\u7f6ex\u8303\u56f4<\/h3>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u79d1\u5b66\u548c\u6570\u636e\u53ef\u89c6\u5316\u9886\u57df\u3002\u901a\u8fc7Matplotlib\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u8bbe\u7f6ex\u8f74\u8303\u56f4\uff0c\u5e76\u521b\u5efa\u5404\u79cd\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u8bbe\u7f6ex\u8f74\u8303\u56f4<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>plt.xlim()<\/code>\u51fd\u6570\u6765\u8bbe\u7f6ex\u8f74\u7684\u8303\u56f4\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u8bbe\u7f6ex\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>plt.xlim(0, 6)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7<code>plt.xlim(0, 6)<\/code>\u8bbe\u7f6ex\u8f74\u7684\u8303\u56f4\u4e3a0\u52306\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u8bbe\u7f6ex\u8f74\u8303\u56f4\u7684\u7ec6\u7c92\u5ea6\u63a7\u5236<\/h4>\n<\/p>\n<p><p>\u9664\u4e86<code>plt.xlim()<\/code>\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>set_xlim()<\/code>\u65b9\u6cd5\u5bf9x\u8f74\u8303\u56f4\u8fdb\u884c\u66f4\u7cbe\u7ec6\u7684\u63a7\u5236\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<p>ax.plot(x, y)<\/p>\n<h2><strong>\u8bbe\u7f6ex\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>ax.set_xlim([0, 6])<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528<code>ax.set_xlim([0, 6])<\/code>\u6765\u8bbe\u7f6ex\u8f74\u8303\u56f4\uff0c\u4e0e<code>plt.xlim()<\/code>\u529f\u80fd\u76f8\u540c\uff0c\u4f46\u8fd9\u79cd\u65b9\u6cd5\u66f4\u7075\u6d3b\uff0c\u53ef\u4ee5\u4e0e\u5176\u4ed6\u8f74\u8bbe\u7f6e\u65b9\u6cd5\u7ed3\u5408\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NumPy\u751f\u6210\u7279\u5b9a\u8303\u56f4\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u5e7f\u6cdb\u7528\u4e8e\u6570\u7ec4\u64cd\u4f5c\u548c\u77e9\u9635\u8ba1\u7b97\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528NumPy\u751f\u6210\u7279\u5b9a\u8303\u56f4\u7684\u6570\u636e\uff0c\u7136\u540e\u7ed3\u5408Matplotlib\u8fdb\u884c\u7ed8\u56fe\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u751f\u6210\u7279\u5b9a\u8303\u56f4\u7684\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>numpy.linspace()<\/code>\u6216<code>numpy.arange()<\/code>\u751f\u6210\u7279\u5b9a\u8303\u56f4\u7684\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/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>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u8bbe\u7f6ex\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>plt.xlim(0, 10)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>np.linspace(0, 10, 100)<\/code>\u751f\u6210\u4ece0\u523010\u4e4b\u95f4\u7684100\u4e2a\u7b49\u95f4\u8ddd\u70b9\uff0c\u7136\u540e\u7ed8\u5236\u8fd9\u4e9b\u70b9\u7684\u6b63\u5f26\u51fd\u6570\u56fe\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u4f7f\u7528arange\u751f\u6210\u6570\u636e<\/h4>\n<\/p>\n<p><p><code>numpy.arange()<\/code>\u4e5f\u53ef\u4ee5\u7528\u6765\u751f\u6210\u7279\u5b9a\u8303\u56f4\u7684\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.arange(0, 10, 0.1)<\/p>\n<p>y = np.cos(x)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u8bbe\u7f6ex\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>plt.xlim(0, 10)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>np.arange(0, 10, 0.1)<\/code>\u751f\u6210\u4ece0\u523010\u4e4b\u95f4\u7684\u95f4\u9694\u4e3a0.1\u7684\u70b9\uff0c\u7136\u540e\u7ed8\u5236\u8fd9\u4e9b\u70b9\u7684\u4f59\u5f26\u51fd\u6570\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pandas\u5904\u7406\u548c\u7ed8\u5236\u6570\u636e<\/h3>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u5904\u7406\u7684\u5f3a\u5927\u5e93\uff0c\u7ed3\u5408Matplotlib\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406\u548c\u7ed8\u5236\u6570\u636e\uff0c\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h4>3.1 \u4f7f\u7528Pandas\u521b\u5efa\u6570\u636e\u6846<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528Pandas\u521b\u5efa\u6570\u636e\u6846\uff0c\u7136\u540e\u7ed8\u5236\u6570\u636e\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;x&#39;: [1, 2, 3, 4, 5], &#39;y&#39;: [1, 4, 9, 16, 25]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.plot(df[&#39;x&#39;], df[&#39;y&#39;])<\/p>\n<h2><strong>\u8bbe\u7f6ex\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>plt.xlim(0, 6)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5305\u542bx\u548cy\u6570\u636e\u7684\u6570\u636e\u6846\uff0c\u7136\u540e\u4f7f\u7528Matplotlib\u7ed8\u5236\u6570\u636e\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u4f7f\u7528Pandas\u7ed8\u56fe\u529f\u80fd<\/h4>\n<\/p>\n<p><p>Pandas\u81ea\u5e26\u7684\u7ed8\u56fe\u529f\u80fd\u4e5f\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u6570\u636e\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;x&#39;: [1, 2, 3, 4, 5], &#39;y&#39;: [1, 4, 9, 16, 25]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>ax = df.plot(x=&#39;x&#39;, y=&#39;y&#39;)<\/p>\n<p>ax.set_xlim([0, 6])<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pandas\u7684<code>plot()<\/code>\u65b9\u6cd5\u7ed8\u5236\u6570\u636e\uff0c\u5e76\u901a\u8fc7<code>ax.set_xlim([0, 6])<\/code>\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528SciPy\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u7ed8\u5236<\/h3>\n<\/p>\n<p><p>SciPy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684Python\u5e93\uff0c\u5305\u542b\u8bb8\u591a\u9ad8\u7ea7\u6570\u5b66\u3001\u79d1\u5b66\u548c\u5de5\u7a0b\u8ba1\u7b97\u529f\u80fd\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528SciPy\u751f\u6210\u548c\u5904\u7406\u6570\u636e\uff0c\u7136\u540e\u7ed3\u5408Matplotlib\u8fdb\u884c\u7ed8\u56fe\u3002<\/p>\n<\/p>\n<p><h4>4.1 \u4f7f\u7528SciPy\u751f\u6210\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528SciPy\u751f\u6210\u7279\u5b9a\u8303\u56f4\u7684\u6570\u636e\u3002\u4f8b\u5982\uff0c\u4f7f\u7528SciPy\u7684<code>linspace()<\/code>\u51fd\u6570\u751f\u6210\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy import linspace<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = linspace(0, 10, 100)<\/p>\n<p>y = np.exp(x)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u8bbe\u7f6ex\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>plt.xlim(0, 10)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528SciPy\u7684<code>linspace(0, 10, 100)<\/code>\u751f\u6210\u4ece0\u523010\u4e4b\u95f4\u7684100\u4e2a\u7b49\u95f4\u8ddd\u70b9\uff0c\u7136\u540e\u7ed8\u5236\u8fd9\u4e9b\u70b9\u7684\u6307\u6570\u51fd\u6570\u56fe\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u7ed3\u5408SciPy\u548cNumPy<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u7ed3\u5408SciPy\u548cNumPy\u751f\u6210\u548c\u5904\u7406\u6570\u636e\uff0c\u7136\u540e\u4f7f\u7528Matplotlib\u7ed8\u56fe\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy import sin<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = sin(x)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<h2><strong>\u8bbe\u7f6ex\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>plt.xlim(0, 10)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528NumPy\u751f\u6210\u6570\u636e\uff0c\u5e76\u4f7f\u7528SciPy\u7684<code>sin()<\/code>\u51fd\u6570\u8ba1\u7b97\u6b63\u5f26\u503c\uff0c\u7136\u540e\u4f7f\u7528Matplotlib\u7ed8\u56fe\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Seaborn\u8fdb\u884c\u9ad8\u7ea7\u6570\u636e\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>Seaborn\u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u7f8e\u89c2\u4e14\u6613\u4e8e\u4f7f\u7528\u7684\u7ed8\u56fe\u51fd\u6570\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Seaborn\u751f\u6210\u9ad8\u7ea7\u56fe\u8868\uff0c\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h4>5.1 \u4f7f\u7528Seaborn\u7ed8\u5236\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528Seaborn\u7ed8\u5236\u6570\u636e\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/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>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>sns.lineplot(x=x, y=y)<\/p>\n<h2><strong>\u8bbe\u7f6ex\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>plt.xlim(0, 10)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Seaborn\u7684<code>lineplot()<\/code>\u51fd\u6570\u7ed8\u5236\u6570\u636e\uff0c\u5e76\u901a\u8fc7<code>plt.xlim(0, 10)<\/code>\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h4>5.2 \u4f7f\u7528Seaborn\u548cPandas\u7ed3\u5408\u7ed8\u56fe<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u7ed3\u5408Seaborn\u548cPandas\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u7ed8\u56fe\uff0c\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;x&#39;: np.linspace(0, 10, 100), &#39;y&#39;: np.cos(np.linspace(0, 10, 100))}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>sns.lineplot(data=df, x=&#39;x&#39;, y=&#39;y&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6ex\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>plt.xlim(0, 10)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pandas\u521b\u5efa\u6570\u636e\u6846\uff0c\u5e76\u4f7f\u7528Seaborn\u7684<code>lineplot()<\/code>\u51fd\u6570\u7ed8\u5236\u6570\u636e\uff0c\u7136\u540e\u901a\u8fc7<code>plt.xlim(0, 10)<\/code>\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Plotly\u8fdb\u884c\u4ea4\u4e92\u5f0f\u7ed8\u56fe<\/h3>\n<\/p>\n<p><p>Plotly\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868\u7684Python\u5e93\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Plotly\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868\uff0c\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h4>6.1 \u4f7f\u7528Plotly\u7ed8\u5236\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528Plotly\u7ed8\u5236\u6570\u636e\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.graph_objects as go<\/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>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig = go.Figure(data=go.Scatter(x=x, y=y))<\/p>\n<h2><strong>\u8bbe\u7f6ex\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>fig.update_layout(xaxis=dict(range=[0, 10]))<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Plotly\u7684<code>go.Scatter()<\/code>\u51fd\u6570\u521b\u5efa\u6563\u70b9\u56fe\uff0c\u5e76\u901a\u8fc7<code>fig.update_layout(xaxis=dict(range=[0, 10]))<\/code>\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h4>6.2 \u4f7f\u7528Plotly Express\u7b80\u5316\u7ed8\u56fe<\/h4>\n<\/p>\n<p><p>Plotly Express\u662fPlotly\u7684\u9ad8\u7ea7\u63a5\u53e3\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u7b80\u6d01\u7684\u7ed8\u56fe\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Plotly Express\u7ed8\u5236\u6570\u636e\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<h2><strong>\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.cos(x)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>fig = px.line(x=x, y=y)<\/p>\n<h2><strong>\u8bbe\u7f6ex\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>fig.update_layout(xaxis=dict(range=[0, 10]))<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Plotly Express\u7684<code>px.line()<\/code>\u51fd\u6570\u521b\u5efa\u6298\u7ebf\u56fe\uff0c\u5e76\u901a\u8fc7<code>fig.update_layout(xaxis=dict(range=[0, 10]))<\/code>\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528Bokeh\u8fdb\u884c\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>Bokeh\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868\u7684Python\u5e93\uff0c\u7279\u522b\u9002\u5408\u7528\u4e8e\u5927\u6570\u636e\u96c6\u7684\u53ef\u89c6\u5316\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Bokeh\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868\uff0c\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h4>7.1 \u4f7f\u7528Bokeh\u7ed8\u5236\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528Bokeh\u7ed8\u5236\u6570\u636e\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bokeh.plotting import figure, show<\/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>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>p = figure(x_range=(0, 10))<\/p>\n<p>p.line(x, y)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>show(p)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Bokeh\u7684<code>figure()<\/code>\u51fd\u6570\u521b\u5efa\u56fe\u8868\uff0c\u5e76\u901a\u8fc7<code>x_range=(0, 10)<\/code>\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h4>7.2 \u4f7f\u7528Bokeh\u548cPandas\u7ed3\u5408\u7ed8\u56fe<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u7ed3\u5408Bokeh\u548cPandas\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u7ed8\u56fe\uff0c\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>from bokeh.plotting import figure, show<\/p>\n<p>from bokeh.io import output_notebook<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;x&#39;: np.linspace(0, 10, 100), &#39;y&#39;: np.cos(np.linspace(0, 10, 100))}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>p = figure(x_range=(0, 10))<\/p>\n<p>p.line(df[&#39;x&#39;], df[&#39;y&#39;])<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>output_notebook()  # \u5728Jupyter Notebook\u4e2d\u4f7f\u7528<\/p>\n<p>show(p)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pandas\u521b\u5efa\u6570\u636e\u6846\uff0c\u5e76\u4f7f\u7528Bokeh\u7684<code>figure()<\/code>\u51fd\u6570\u521b\u5efa\u56fe\u8868\uff0c\u7136\u540e\u901a\u8fc7<code>x_range=(0, 10)<\/code>\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528Alt<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>r\u8fdb\u884c\u58f0\u660e\u5f0f\u7ed8\u56fe<\/h3>\n<\/p>\n<p><p>Altair\u662f\u4e00\u4e2a\u57fa\u4e8eVega\u548cVega-Lite\u7684\u58f0\u660e\u5f0f\u53ef\u89c6\u5316\u5e93\uff0c\u9002\u5408\u7528\u4e8e\u5feb\u901f\u521b\u5efa\u590d\u6742\u7684\u56fe\u8868\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Altair\u521b\u5efa\u56fe\u8868\uff0c\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h4>8.1 \u4f7f\u7528Altair\u7ed8\u5236\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528Altair\u7ed8\u5236\u6570\u636e\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import altair as alt<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = pd.DataFrame({&#39;x&#39;: np.linspace(0, 10, 100), &#39;y&#39;: np.sin(np.linspace(0, 10, 100))})<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>chart = alt.Chart(data).mark_line().encode(<\/p>\n<p>    x=alt.X(&#39;x&#39;, scale=alt.Scale(domain=[0, 10])),<\/p>\n<p>    y=&#39;y&#39;<\/p>\n<p>)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>chart.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Altair\u7684<code>alt.Chart()<\/code>\u51fd\u6570\u521b\u5efa\u56fe\u8868\uff0c\u5e76\u901a\u8fc7<code>alt.X(&#39;x&#39;, scale=alt.Scale(domain=[0, 10]))<\/code>\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h4>8.2 \u4f7f\u7528Altair\u548cPandas\u7ed3\u5408\u7ed8\u56fe<\/h4>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u7ed3\u5408Altair\u548cPandas\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u7ed8\u56fe\uff0c\u5e76\u8bbe\u7f6ex\u8f74\u8303\u56f4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import altair as alt<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = pd.DataFrame({&#39;x&#39;: np.linspace(0, 10, 100), &#39;y&#39;: np.cos(np.linspace(0, 10, 100))})<\/p>\n<h2><strong>\u521b\u5efa\u56fe\u8868<\/strong><\/h2>\n<p>chart = alt.Chart(data).mark_line().encode(<\/p>\n<p>    x=alt.X(&#39;x&#39;, scale=alt.Scale(domain=[0, 10])),<\/p>\n<p>    y=&#39;y&#39;<\/p>\n<p>)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>chart.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Pandas\u521b\u5efa\u6570\u636e\u6846\uff0c\u5e76\u4f7f\u7528Altair\u7684<code>alt.Chart()<\/code>\u51fd\u6570\u521b\u5efa\u56fe\u8868\uff0c\u7136\u540e\u901a\u8fc7<code>alt.X(&#39;x&#39;, scale=alt.Scale(domain=[0, 10]))<\/code>\u8bbe\u7f6ex\u8f74\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Python\u4e2d\u89c4\u5b9ax\u8303\u56f4\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528Matplotlib\u3001NumPy\u3001Pandas\u3001SciPy\u3001Seaborn\u3001Plotly\u3001Bokeh\u548cAltair\u7b49\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u6570\u636e\u5904\u7406\u548c\u53ef\u89c6\u5316\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u63d0\u9ad8\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u7684\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u53d8\u91cf\u7684\u8303\u56f4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6761\u4ef6\u8bed\u53e5\u6765\u9650\u5236\u53d8\u91cf\u7684\u8303\u56f4\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>if<\/code>\u8bed\u53e5\u68c0\u67e5\u53d8\u91cf\u503c\u662f\u5426\u5728\u6307\u5b9a\u8303\u56f4\u5185\uff0c\u5982\u679c\u4e0d\u5728\u8303\u56f4\u5185\uff0c\u53ef\u4ee5\u629b\u51fa\u5f02\u5e38\u6216\u8d4b\u4e88\u9ed8\u8ba4\u503c\u3002\u4f7f\u7528\u8fd9\u6837\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u53d8\u91cf\u59cb\u7ec8\u6ee1\u8db3\u7279\u5b9a\u7684\u7ea6\u675f\u6761\u4ef6\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u9650\u5236\u6570\u503c\u8303\u56f4\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>min()<\/code>\u548c<code>max()<\/code>\u51fd\u6570\u6765\u9650\u5236\u6570\u503c\u8303\u56f4\u3002\u4f8b\u5982\uff0c<code>x = max(min_value, min(x, max_value))<\/code>\u53ef\u4ee5\u786e\u4fdd\u53d8\u91cfx\u7684\u503c\u59cb\u7ec8\u5728min_value\u548cmax_value\u4e4b\u95f4\u3002\u6b64\u5916\uff0cNumPy\u5e93\u7684<code>clip()<\/code>\u51fd\u6570\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5feb\u901f\u5c06\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u9650\u5236\u5728\u7ed9\u5b9a\u7684\u8303\u56f4\u5185\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u8d85\u51fa\u8303\u56f4\u7684\u503c\uff1f<\/strong><br \/>\u5f53\u53d8\u91cf\u7684\u503c\u8d85\u51fa\u9884\u5b9a\u8303\u56f4\u65f6\uff0c\u901a\u5e38\u6709\u51e0\u79cd\u5904\u7406\u65b9\u5f0f\u3002\u53ef\u4ee5\u9009\u62e9\u629b\u51fa\u5f02\u5e38\uff0c\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u673a\u5236\u901a\u77e5\u7528\u6237\uff0c\u6216\u8005\u5c06\u8d85\u51fa\u8303\u56f4\u7684\u503c\u66ff\u6362\u4e3a\u8303\u56f4\u7684\u8fb9\u754c\u503c\uff08\u4f8b\u5982\uff0c\u5c06\u5c0f\u4e8e\u6700\u5c0f\u503c\u7684\u503c\u8bbe\u4e3a\u6700\u5c0f\u503c\uff0c\u5c06\u5927\u4e8e\u6700\u5927\u503c\u7684\u503c\u8bbe\u4e3a\u6700\u5927\u503c\uff09\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u5e76\u4fdd\u6301\u6570\u636e\u7684\u6709\u6548\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u89c4\u5b9ax\u8303\u56f4\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4f60\u4f7f\u7528\u7684\u5e93\u548c\u4f60\u60f3\u8981\u8fbe\u5230\u7684\u6548\u679c\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumP [&hellip;]","protected":false},"author":3,"featured_media":1153088,"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\/1153085"}],"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=1153085"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153085\/revisions"}],"predecessor-version":[{"id":1153092,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153085\/revisions\/1153092"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1153088"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1153085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1153085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1153085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}