{"id":1161540,"date":"2025-01-13T19:17:34","date_gmt":"2025-01-13T11:17:34","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1161540.html"},"modified":"2025-01-13T19:17:39","modified_gmt":"2025-01-13T11:17:39","slug":"python%e6%95%b0%e6%8d%ae%e5%a6%82%e4%bd%95%e5%8f%af%e8%a7%86%e5%8c%96","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1161540.html","title":{"rendered":"python\u6570\u636e\u5982\u4f55\u53ef\u89c6\u5316"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202741\/27ba3b98-c6de-4bb6-8ecd-58577fde8be0.webp\" alt=\"python\u6570\u636e\u5982\u4f55\u53ef\u89c6\u5316\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u7684\u6838\u5fc3\u8981\u70b9\u5305\u62ec\uff1a<strong>\u9009\u62e9\u5408\u9002\u7684\u53ef\u89c6\u5316\u5e93\u3001\u7406\u89e3\u6570\u636e\u7684\u7ed3\u6784\u4e0e\u7279\u6027\u3001\u9009\u62e9\u9002\u5f53\u7684\u56fe\u8868\u7c7b\u578b\u3001\u638c\u63e1\u57fa\u672c\u7684\u53ef\u89c6\u5316\u6280\u672f\u3001\u7ed3\u5408\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u5de5\u5177<\/strong>\u3002\u5176\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u53ef\u89c6\u5316\u5e93\u662f\u975e\u5e38\u5173\u952e\u7684\uff0c\u56e0\u4e3a\u4e0d\u540c\u7684\u5e93\u6709\u4e0d\u540c\u7684\u7279\u6027\u548c\u7528\u9014\u3002\u5e38\u89c1\u7684\u53ef\u89c6\u5316\u5e93\u6709Matplotlib\u3001Seaborn\u3001Plotly\u3001Bokeh\u7b49\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684\u53ef\u89c6\u5316\u5e93<\/strong><\/h2>\n<p><p>Python\u4e2d\u6709\u8bb8\u591a\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u7684\u5e93\uff0c\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>1\u3001Matplotlib<\/h3>\n<\/p>\n<p><p><strong>Matplotlib<\/strong> \u662fPython\u4e2d\u6700\u57fa\u7840\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u53ef\u89c6\u5316\u5e93\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u57fa\u672c\u56fe\u8868\u7684\u7ed8\u5236\u3002\u5b83\u7684\u4e3b\u8981\u7279\u70b9\u662f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u7ed8\u5236\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\uff0c\u5305\u62ec\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u6563\u70b9\u56fe\u7b49\u3002<\/li>\n<li><strong>\u5e7f\u6cdb\u652f\u6301<\/strong>\uff1a\u51e0\u4e4e\u6240\u6709\u5176\u4ed6\u53ef\u89c6\u5316\u5e93\u90fd\u57fa\u4e8eMatplotlib\u3002<\/li>\n<li><strong>\u8be6\u7ec6\u63a7\u5236<\/strong>\uff1a\u53ef\u4ee5\u5bf9\u56fe\u8868\u7684\u6bcf\u4e00\u4e2a\u7ec6\u8282\u8fdb\u884c\u5b9a\u5236\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7b80\u5355\u7684\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;\u7b80\u5355\u6298\u7ebf\u56fe&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001Seaborn<\/h3>\n<\/p>\n<p><p><strong>Seaborn<\/strong> \u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u53ef\u89c6\u5316\u5e93\uff0c\u9002\u7528\u4e8e\u7edf\u8ba1\u56fe\u8868\u7684\u7ed8\u5236\u3002\u5b83\u7684\u4e3b\u8981\u7279\u70b9\u662f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u6d01\u7684API<\/strong>\uff1a\u6bd4Matplotlib\u66f4\u7b80\u5355\u7684\u63a5\u53e3\uff0c\u66f4\u9002\u5408\u5feb\u901f\u7ed8\u5236\u7edf\u8ba1\u56fe\u8868\u3002<\/li>\n<li><strong>\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f<\/strong>\uff1a\u56fe\u8868\u7684\u9ed8\u8ba4\u6837\u5f0f\u66f4\u52a0\u7f8e\u89c2\u3002<\/li>\n<li><strong>\u96c6\u6210\u6027\u5f3a<\/strong>\uff1a\u4e0ePandas\u6570\u636e\u6846\u9ad8\u5ea6\u96c6\u6210\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>tips = sns.load_dataset(&#39;tips&#39;)<\/p>\n<p>sns.scatterplot(x=&#39;total_bill&#39;, y=&#39;tip&#39;, data=tips)<\/p>\n<p>plt.title(&#39;\u603b\u8d26\u5355\u4e0e\u5c0f\u8d39\u7684\u5173\u7cfb&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001Plotly<\/h3>\n<\/p>\n<p><p><strong>Plotly<\/strong> \u662f\u4e00\u4e2a\u652f\u6301\u4ea4\u4e92\u5f0f\u56fe\u8868\u7684\u5e93\uff0c\u975e\u5e38\u9002\u5408\u7528\u4e8eWeb\u5e94\u7528\u548c\u4eea\u8868\u76d8\u3002\u5b83\u7684\u4e3b\u8981\u7279\u70b9\u662f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u4ea4\u4e92\u6027\u5f3a<\/strong>\uff1a\u652f\u6301\u7f29\u653e\u3001\u5e73\u79fb\u3001\u60ac\u505c\u7b49\u4ea4\u4e92\u529f\u80fd\u3002<\/li>\n<li><strong>\u652f\u6301\u591a\u79cd\u8f93\u51fa<\/strong>\uff1a\u53ef\u4ee5\u5bfc\u51fa\u4e3aHTML\u3001PNG\u7b49\u591a\u79cd\u683c\u5f0f\u3002<\/li>\n<li><strong>\u9002\u7528\u4e8e\u5927\u6570\u636e\u96c6<\/strong>\uff1a\u80fd\u591f\u5904\u7406\u548c\u5c55\u793a\u8f83\u5927\u89c4\u6a21\u7684\u6570\u636e\u96c6\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<h2><strong>\u7ed8\u5236\u4ea4\u4e92\u5f0f\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>df = px.data.iris()<\/p>\n<p>fig = px.scatter(df, x=&#39;sepal_width&#39;, y=&#39;sepal_length&#39;, color=&#39;species&#39;)<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001Bokeh<\/h3>\n<\/p>\n<p><p><strong>Bokeh<\/strong> \u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u5e93\uff0c\u9002\u5408\u7528\u4e8e\u521b\u5efa\u7f51\u7edc\u5e94\u7528\u4e2d\u7684\u52a8\u6001\u56fe\u8868\u3002\u5b83\u7684\u4e3b\u8981\u7279\u70b9\u662f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u9ad8\u6027\u80fd<\/strong>\uff1a\u80fd\u591f\u5904\u7406\u5927\u91cf\u6570\u636e\u4e14\u4fdd\u6301\u826f\u597d\u7684\u6027\u80fd\u3002<\/li>\n<li><strong>\u4e30\u5bcc\u7684\u4ea4\u4e92\u529f\u80fd<\/strong>\uff1a\u652f\u6301\u591a\u79cd\u4ea4\u4e92\u5f0f\u5de5\u5177\uff0c\u5982\u7f29\u653e\u3001\u9009\u62e9\u7b49\u3002<\/li>\n<li><strong>\u6613\u4e8e\u5d4c\u5165Web\u5e94\u7528<\/strong>\uff1a\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5d4c\u5165\u5230Flask\u3001Django\u7b49Web\u6846\u67b6\u4e2d\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">from bokeh.plotting import figure, show<\/p>\n<p>from bokeh.io import output_notebook<\/p>\n<p>output_notebook()<\/p>\n<h2><strong>\u521b\u5efa\u7b80\u5355\u7684\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>p = figure(title=&quot;\u7b80\u5355\u6298\u7ebf\u56fe&quot;, x_axis_label=&#39;X\u8f74&#39;, y_axis_label=&#39;Y\u8f74&#39;)<\/p>\n<p>p.line([1, 2, 3, 4, 5], [1, 4, 9, 16, 25], legend_label=&quot;\u6298\u7ebf&quot;, line_width=2)<\/p>\n<p>show(p)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u7406\u89e3\u6570\u636e\u7684\u7ed3\u6784\u4e0e\u7279\u6027<\/strong><\/h2>\n<p><p>\u5728\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u4e4b\u524d\uff0c\u7406\u89e3\u6570\u636e\u7684\u7ed3\u6784\u4e0e\u7279\u6027\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u8fd9\u5305\u62ec\u6570\u636e\u7684\u7c7b\u578b\u3001\u5206\u5e03\u3001\u76f8\u5173\u6027\u7b49\u65b9\u9762\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u6570\u636e\u7c7b\u578b<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u7c7b\u578b\u51b3\u5b9a\u4e86\u6211\u4eec\u5e94\u8be5\u5982\u4f55\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u548c\u5c55\u793a\u3002\u5e38\u89c1\u7684\u6570\u636e\u7c7b\u578b\u5305\u62ec\u6570\u503c\u578b\u3001\u5206\u7c7b\u578b\u548c\u65f6\u95f4\u5e8f\u5217\u578b\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u6570\u503c\u578b\u6570\u636e<\/strong>\uff1a\u5982\u6536\u5165\u3001\u5e74\u9f84\u7b49\uff0c\u9002\u5408\u7528\u76f4\u65b9\u56fe\u3001\u7bb1\u7ebf\u56fe\u7b49\u8fdb\u884c\u5c55\u793a\u3002<\/li>\n<li><strong>\u5206\u7c7b\u578b\u6570\u636e<\/strong>\uff1a\u5982\u6027\u522b\u3001\u5730\u533a\u7b49\uff0c\u9002\u5408\u7528\u6761\u5f62\u56fe\u3001\u997c\u56fe\u7b49\u8fdb\u884c\u5c55\u793a\u3002<\/li>\n<li><strong>\u65f6\u95f4\u5e8f\u5217\u578b\u6570\u636e<\/strong>\uff1a\u5982\u80a1\u7968\u4ef7\u683c\u3001\u6e29\u5ea6\u53d8\u5316\u7b49\uff0c\u9002\u5408\u7528\u6298\u7ebf\u56fe\u3001\u9762\u79ef\u56fe\u7b49\u8fdb\u884c\u5c55\u793a\u3002<\/li>\n<\/ul>\n<p><h3>2\u3001\u6570\u636e\u5206\u5e03<\/h3>\n<\/p>\n<p><p>\u4e86\u89e3\u6570\u636e\u7684\u5206\u5e03\u60c5\u51b5\u6709\u52a9\u4e8e\u9009\u62e9\u5408\u9002\u7684\u53ef\u89c6\u5316\u65b9\u6cd5\u3002\u5e38\u89c1\u7684\u5206\u5e03\u7c7b\u578b\u6709\u6b63\u6001\u5206\u5e03\u3001\u5747\u5300\u5206\u5e03\u3001\u6307\u6570\u5206\u5e03\u7b49\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u6b63\u6001\u5206\u5e03<\/strong>\uff1a\u6570\u636e\u96c6\u4e2d\u5728\u5747\u503c\u9644\u8fd1\uff0c\u9002\u5408\u7528\u76f4\u65b9\u56fe\u5c55\u793a\u3002<\/li>\n<li><strong>\u5747\u5300\u5206\u5e03<\/strong>\uff1a\u6570\u636e\u5728\u4e00\u5b9a\u8303\u56f4\u5185\u5747\u5300\u5206\u5e03\uff0c\u9002\u5408\u7528\u76f4\u65b9\u56fe\u5c55\u793a\u3002<\/li>\n<li><strong>\u6307\u6570\u5206\u5e03<\/strong>\uff1a\u6570\u636e\u96c6\u4e2d\u5728\u67d0\u4e2a\u65b9\u5411\u4e0a\uff0c\u9002\u5408\u7528\u76f4\u65b9\u56fe\u6216\u5bc6\u5ea6\u56fe\u5c55\u793a\u3002<\/li>\n<\/ul>\n<p><h3>3\u3001\u6570\u636e\u76f8\u5173\u6027<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u4e4b\u95f4\u7684\u76f8\u5173\u6027\u53ef\u4ee5\u901a\u8fc7\u6563\u70b9\u56fe\u3001\u70ed\u529b\u56fe\u7b49\u8fdb\u884c\u5c55\u793a\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u6563\u70b9\u56fe<\/strong>\uff1a\u9002\u5408\u5c55\u793a\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/li>\n<li><strong>\u70ed\u529b\u56fe<\/strong>\uff1a\u9002\u5408\u5c55\u793a\u591a\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7684\u76f8\u5173\u6027\u3002<\/li>\n<\/ul>\n<h2><strong>\u4e09\u3001\u9009\u62e9\u9002\u5f53\u7684\u56fe\u8868\u7c7b\u578b<\/strong><\/h2>\n<p><p>\u9009\u62e9\u9002\u5f53\u7684\u56fe\u8868\u7c7b\u578b\u662f\u6570\u636e\u53ef\u89c6\u5316\u7684\u5173\u952e\u6b65\u9aa4\u3002\u4e0d\u540c\u7684\u56fe\u8868\u7c7b\u578b\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u548c\u5c55\u793a\u76ee\u7684\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u6298\u7ebf\u56fe<\/h3>\n<\/p>\n<p><p><strong>\u6298\u7ebf\u56fe<\/strong> \u9002\u7528\u4e8e\u5c55\u793a\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u6216\u8fde\u7eed\u53d8\u5316\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u7b80\u5355\u7684\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;\u6298\u7ebf\u56fe&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u67f1\u72b6\u56fe<\/h3>\n<\/p>\n<p><p><strong>\u67f1\u72b6\u56fe<\/strong> \u9002\u7528\u4e8e\u5c55\u793a\u5206\u7c7b\u6570\u636e\u7684\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u7b80\u5355\u7684\u67f1\u72b6\u56fe<\/strong><\/h2>\n<p>categories = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;]<\/p>\n<p>values = [5, 7, 3, 8, 4]<\/p>\n<p>plt.bar(categories, values)<\/p>\n<p>plt.xlabel(&#39;\u7c7b\u522b&#39;)<\/p>\n<p>plt.ylabel(&#39;\u503c&#39;)<\/p>\n<p>plt.title(&#39;\u67f1\u72b6\u56fe&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u6563\u70b9\u56fe<\/h3>\n<\/p>\n<p><p><strong>\u6563\u70b9\u56fe<\/strong> \u9002\u7528\u4e8e\u5c55\u793a\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u7b80\u5355\u7684\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<p>plt.scatter(x, y)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;\u6563\u70b9\u56fe&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4\u3001\u997c\u56fe<\/h3>\n<\/p>\n<p><p><strong>\u997c\u56fe<\/strong> \u9002\u7528\u4e8e\u5c55\u793a\u5206\u7c7b\u6570\u636e\u7684\u6bd4\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236\u7b80\u5355\u7684\u997c\u56fe<\/strong><\/h2>\n<p>labels = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;]<\/p>\n<p>sizes = [15, 30, 45, 10]<\/p>\n<p>plt.pie(sizes, labels=labels, autopct=&#39;%1.1f%%&#39;)<\/p>\n<p>plt.title(&#39;\u997c\u56fe&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u638c\u63e1\u57fa\u672c\u7684\u53ef\u89c6\u5316\u6280\u672f<\/strong><\/h2>\n<p><p>\u638c\u63e1\u57fa\u672c\u7684\u53ef\u89c6\u5316\u6280\u672f\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5c55\u793a\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u8bbe\u7f6e\u56fe\u8868\u6807\u9898\u548c\u6807\u7b7e<\/h3>\n<\/p>\n<p><p>\u4e3a\u56fe\u8868\u8bbe\u7f6e\u6807\u9898\u548c\u6807\u7b7e\u80fd\u591f\u8ba9\u89c2\u4f17\u66f4\u5bb9\u6613\u7406\u89e3\u56fe\u8868\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;\u6298\u7ebf\u56fe&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u6dfb\u52a0\u56fe\u4f8b<\/h3>\n<\/p>\n<p><p>\u56fe\u4f8b\u80fd\u591f\u5e2e\u52a9\u89c2\u4f17\u7406\u89e3\u56fe\u8868\u4e2d\u4e0d\u540c\u5143\u7d20\u7684\u542b\u4e49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y1 = [1, 4, 9, 16, 25]<\/p>\n<p>y2 = [2, 3, 4, 5, 6]<\/p>\n<p>plt.plot(x, y1, label=&#39;\u6570\u636e1&#39;)<\/p>\n<p>plt.plot(x, y2, label=&#39;\u6570\u636e2&#39;)<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;\u6298\u7ebf\u56fe&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u8bbe\u7f6e\u56fe\u8868\u6837\u5f0f<\/h3>\n<\/p>\n<p><p>\u8bbe\u7f6e\u56fe\u8868\u6837\u5f0f\u80fd\u591f\u63d0\u9ad8\u56fe\u8868\u7684\u7f8e\u89c2\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [1, 4, 9, 16, 25]<\/p>\n<p>plt.plot(x, y, &#39;o-&#39;, color=&#39;red&#39;)  # \u4f7f\u7528\u7ea2\u8272\u5706\u70b9\u7ebf<\/p>\n<p>plt.xlabel(&#39;X\u8f74&#39;)<\/p>\n<p>plt.ylabel(&#39;Y\u8f74&#39;)<\/p>\n<p>plt.title(&#39;\u6298\u7ebf\u56fe&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u7ed3\u5408\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u5de5\u5177<\/strong><\/h2>\n<p><p>\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u5de5\u5177\u80fd\u591f\u63d0\u4f9b\u66f4\u4e30\u5bcc\u7684\u7528\u6237\u4f53\u9a8c\uff0c\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u4f7f\u7528Plotly\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868<\/h3>\n<\/p>\n<p><p>Plotly \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u5e93\uff0c\u80fd\u591f\u521b\u5efa\u4e30\u5bcc\u7684\u4ea4\u4e92\u5f0f\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<h2><strong>\u521b\u5efa\u4ea4\u4e92\u5f0f\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>df = px.data.iris()<\/p>\n<p>fig = px.scatter(df, x=&#39;sepal_width&#39;, y=&#39;sepal_length&#39;, color=&#39;species&#39;)<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4f7f\u7528Bokeh\u521b\u5efa\u4ea4\u4e92\u5f0f\u56fe\u8868<\/h3>\n<\/p>\n<p><p>Bokeh \u662f\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u5e93\uff0c\u80fd\u591f\u521b\u5efa\u52a8\u6001\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bokeh.plotting import figure, show<\/p>\n<p>from bokeh.io import output_notebook<\/p>\n<p>output_notebook()<\/p>\n<h2><strong>\u521b\u5efa\u7b80\u5355\u7684\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>p = figure(title=&quot;\u7b80\u5355\u6298\u7ebf\u56fe&quot;, x_axis_label=&#39;X\u8f74&#39;, y_axis_label=&#39;Y\u8f74&#39;)<\/p>\n<p>p.line([1, 2, 3, 4, 5], [1, 4, 9, 16, 25], legend_label=&quot;\u6298\u7ebf&quot;, line_width=2)<\/p>\n<p>show(p)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u4f7f\u7528Dash\u521b\u5efa\u4ea4\u4e92\u5f0f\u4eea\u8868\u76d8<\/h3>\n<\/p>\n<p><p>Dash \u662f\u4e00\u4e2a\u57fa\u4e8ePlotly\u7684Web\u5e94\u7528\u6846\u67b6\uff0c\u80fd\u591f\u521b\u5efa\u4ea4\u4e92\u5f0f\u4eea\u8868\u76d8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import dash<\/p>\n<p>import dash_core_components as dcc<\/p>\n<p>import dash_html_components as html<\/p>\n<p>import plotly.express as px<\/p>\n<h2><strong>\u521b\u5efaDash\u5e94\u7528<\/strong><\/h2>\n<p>app = dash.Dash(__name__)<\/p>\n<h2><strong>\u52a0\u8f7d\u6570\u636e<\/strong><\/h2>\n<p>df = px.data.iris()<\/p>\n<h2><strong>\u5b9a\u4e49\u5e03\u5c40<\/strong><\/h2>\n<p>app.layout = html.Div(children=[<\/p>\n<p>    html.H1(children=&#39;\u4ea4\u4e92\u5f0f\u4eea\u8868\u76d8&#39;),<\/p>\n<p>    dcc.Graph(<\/p>\n<p>        id=&#39;example-graph&#39;,<\/p>\n<p>        figure=px.scatter(df, x=&#39;sepal_width&#39;, y=&#39;sepal_length&#39;, color=&#39;species&#39;)<\/p>\n<p>    )<\/p>\n<p>])<\/p>\n<h2><strong>\u8fd0\u884c\u5e94\u7528<\/strong><\/h2>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    app.run_server(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u6570\u636e\u7684\u53ef\u89c6\u5316\u3002\u9009\u62e9\u5408\u9002\u7684\u53ef\u89c6\u5316\u5e93\u3001\u7406\u89e3\u6570\u636e\u7684\u7ed3\u6784\u4e0e\u7279\u6027\u3001\u9009\u62e9\u9002\u5f53\u7684\u56fe\u8868\u7c7b\u578b\u3001\u638c\u63e1\u57fa\u672c\u7684\u53ef\u89c6\u5316\u6280\u672f\u4ee5\u53ca\u7ed3\u5408\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5c55\u793a\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684Python\u53ef\u89c6\u5316\u5e93\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u53ef\u89c6\u5316\u5e93\u53ef\u4f9b\u9009\u62e9\uff0c\u5305\u62ecMatplotlib\u3001Seaborn\u3001Plotly\u548cBokeh\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u4e3b\u8981\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u3002\u5982\u679c\u9700\u8981\u7b80\u5355\u7684\u9759\u6001\u56fe\uff0cMatplotlib\u662f\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u82e5\u60f3\u521b\u5efa\u7f8e\u89c2\u7684\u7edf\u8ba1\u56fe\u8868\uff0cSeaborn\u5c06\u662f\u4e00\u4e2a\u7406\u60f3\u7684\u9009\u62e9\uff1b\u800c\u5982\u679c\u9700\u8981\u4ea4\u4e92\u5f0f\u56fe\u8868\uff0cPlotly\u548cBokeh\u5219\u66f4\u4e3a\u9002\u5408\u3002\u8003\u8651\u56fe\u8868\u7684\u590d\u6742\u6027\u3001\u4ea4\u4e92\u6027\u4ee5\u53ca\u7f8e\u89c2\u5ea6\uff0c\u90fd\u4f1a\u5f71\u54cd\u4f60\u6700\u7ec8\u7684\u9009\u62e9\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u548c\u6e05\u6d17\u6570\u636e\u4ee5\u4fbf\u4e8e\u53ef\u89c6\u5316\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u4e4b\u524d\uff0c\u6570\u636e\u6e05\u6d17\u81f3\u5173\u91cd\u8981\u3002\u9996\u5148\uff0c\u786e\u4fdd\u6570\u636e\u6ca1\u6709\u7f3a\u5931\u503c\u6216\u5f02\u5e38\u503c\u3002\u5982\u679c\u53d1\u73b0\u8fd9\u4e9b\u95ee\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7\u63d2\u503c\u6cd5\u3001\u5220\u9664\u6216\u66ff\u6362\u6765\u5904\u7406\u3002\u6b64\u5916\uff0c\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\u4e5f\u76f8\u5f53\u91cd\u8981\uff0c\u6bd4\u5982\u5c06\u65e5\u671f\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u65e5\u671f\u683c\u5f0f\u3002\u4f7f\u7528Pandas\u5e93\u53ef\u4ee5\u6709\u6548\u5730\u8fdb\u884c\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u786e\u4fdd\u6570\u636e\u5728\u53ef\u89c6\u5316\u65f6\u80fd\u591f\u51c6\u786e\u53cd\u6620\u771f\u5b9e\u60c5\u51b5\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u9ad8Python\u53ef\u89c6\u5316\u56fe\u8868\u7684\u53ef\u8bfb\u6027\uff1f<\/strong><br \/>\u63d0\u9ad8\u53ef\u89c6\u5316\u56fe\u8868\u7684\u53ef\u8bfb\u6027\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2a\u65b9\u5f0f\u5b9e\u73b0\u3002\u9009\u62e9\u5408\u9002\u7684\u989c\u8272\u642d\u914d\uff0c\u907f\u514d\u4f7f\u7528\u8fc7\u4e8e\u9c9c\u8273\u6216\u76f8\u4f3c\u7684\u989c\u8272\uff0c\u4ee5\u514d\u9020\u6210\u89c6\u89c9\u75b2\u52b3\u3002\u6dfb\u52a0\u56fe\u4f8b\u3001\u6807\u9898\u548c\u8f74\u6807\u7b7e\uff0c\u80fd\u591f\u5e2e\u52a9\u89c2\u4f17\u66f4\u597d\u5730\u7406\u89e3\u56fe\u8868\u5185\u5bb9\u3002\u6b64\u5916\uff0c\u4fdd\u6301\u56fe\u8868\u7b80\u6d01\uff0c\u907f\u514d\u8fc7\u591a\u7684\u4fe1\u606f\u548c\u590d\u6742\u7684\u56fe\u5f62\uff0c\u53ef\u4ee5\u4f7f\u6570\u636e\u7684\u5c55\u793a\u66f4\u52a0\u76f4\u89c2\u548c\u6613\u4e8e\u7406\u89e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u7684\u6838\u5fc3\u8981\u70b9\u5305\u62ec\uff1a\u9009\u62e9\u5408\u9002\u7684\u53ef\u89c6\u5316\u5e93\u3001\u7406\u89e3\u6570\u636e\u7684\u7ed3\u6784\u4e0e\u7279\u6027\u3001\u9009\u62e9\u9002\u5f53\u7684\u56fe\u8868\u7c7b\u578b\u3001\u638c [&hellip;]","protected":false},"author":3,"featured_media":1161541,"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\/1161540"}],"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=1161540"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161540\/revisions"}],"predecessor-version":[{"id":1161543,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161540\/revisions\/1161543"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1161541"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1161540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1161540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1161540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}