{"id":1128270,"date":"2025-01-08T20:18:42","date_gmt":"2025-01-08T12:18:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1128270.html"},"modified":"2025-01-08T20:18:44","modified_gmt":"2025-01-08T12:18:44","slug":"python%e7%94%bb%e5%9b%be%e8%b5%b7%e5%a7%8b%e7%82%b9%e4%b8%8d%e5%90%8c%e6%97%b6%e5%a6%82%e4%bd%95%e7%94%bb%e5%a4%9a%e5%bc%a0%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1128270.html","title":{"rendered":"python\u753b\u56fe\u8d77\u59cb\u70b9\u4e0d\u540c\u65f6\u5982\u4f55\u753b\u591a\u5f20\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095031\/47771d21-b41f-4b5b-929d-eef1fcd1be68.webp\" alt=\"python\u753b\u56fe\u8d77\u59cb\u70b9\u4e0d\u540c\u65f6\u5982\u4f55\u753b\u591a\u5f20\u56fe\" \/><\/p>\n<p><p> <strong>Python\u753b\u56fe\u8d77\u59cb\u70b9\u4e0d\u540c\u65f6\u5982\u4f55\u753b\u591a\u5f20\u56fe<\/strong>  <\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u7ed8\u5236\u591a\u5f20\u56fe\u65f6\uff0c\u5982\u679c\u8d77\u59cb\u70b9\u4e0d\u540c\uff0c\u53ef\u4ee5\u4f7f\u7528<strong>\u4e0d\u540c\u7ed8\u56fe\u5e93\u3001\u5bf9\u6bcf\u4e2a\u56fe\u8fdb\u884c\u5355\u72ec\u8bbe\u7f6e\u3001\u4f7f\u7528\u5b50\u56fe\u529f\u80fd<\/strong>\u6765\u5b8c\u6210\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u7ed8\u5236\u591a\u5f20\u56fe\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u4e0d\u540c\u7ed8\u56fe\u5e93<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u6709\u591a\u79cd\u7ed8\u56fe\u5e93\u53ef\u4f9b\u9009\u62e9\uff0c\u6700\u5e38\u7528\u7684\u662fMatplotlib\u3001Seaborn\u548cPlotly\u3002\u6bcf\u79cd\u5e93\u90fd\u6709\u81ea\u5df1\u72ec\u7279\u7684\u4f18\u52bf\u548c\u7528\u9014\u3002<\/p>\n<\/p>\n<p><h4>Matplotlib<\/h4>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u57fa\u7840\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\u3002\u5b83\u5141\u8bb8\u7528\u6237\u521b\u5efa\u9759\u6001\u3001\u52a8\u753b\u548c\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u56fe\u8868\u3002<\/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)<\/p>\n<p>y1 = np.sin(x)<\/p>\n<p>y2 = np.cos(x)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e00\u5f20\u56fe<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<p>plt.plot(x, y1, label=&#39;sin(x)&#39;)<\/p>\n<p>plt.title(&#39;Sin Curve&#39;)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.legend()<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u5f20\u56fe<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<p>plt.plot(x, y2, label=&#39;cos(x)&#39;, color=&#39;red&#39;)<\/p>\n<p>plt.title(&#39;Cos Curve&#39;)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>Seaborn<\/h4>\n<\/p>\n<p><p>Seaborn\u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u4e3a\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\u548c\u66f4\u4e3a\u7b80\u5355\u7684\u7ed8\u56fe\u63a5\u53e3\u3002<\/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>tips = sns.load_dataset(&quot;tips&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e00\u5f20\u56fe<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<p>sns.scatterplot(x=&quot;total_bill&quot;, y=&quot;tip&quot;, data=tips)<\/p>\n<p>plt.title(&#39;Total Bill vs Tip&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u5f20\u56fe<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<p>sns.boxplot(x=&quot;day&quot;, y=&quot;total_bill&quot;, data=tips)<\/p>\n<p>plt.title(&#39;Box Plot of Total Bill by Day&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>Plotly<\/h4>\n<\/p>\n<p><p>Plotly\u662f\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u5e93\uff0c\u9002\u7528\u4e8e\u521b\u5efa\u4ea4\u4e92\u5f0f\u548c\u52a8\u6001\u56fe\u8868\u3002<\/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>df = px.data.iris()<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e00\u5f20\u56fe<\/strong><\/h2>\n<p>fig1 = px.scatter(df, x=&#39;sepal_width&#39;, y=&#39;sepal_length&#39;, color=&#39;species&#39;, title=&#39;Sepal Width vs Length&#39;)<\/p>\n<p>fig1.show()<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u5f20\u56fe<\/strong><\/h2>\n<p>fig2 = px.scatter(df, x=&#39;petal_width&#39;, y=&#39;petal_length&#39;, color=&#39;species&#39;, title=&#39;Petal Width vs Length&#39;)<\/p>\n<p>fig2.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5bf9\u6bcf\u4e2a\u56fe\u8fdb\u884c\u5355\u72ec\u8bbe\u7f6e<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u591a\u5f20\u56fe\u65f6\uff0c\u53ef\u4ee5\u5bf9\u6bcf\u4e2a\u56fe\u8fdb\u884c\u5355\u72ec\u8bbe\u7f6e\uff0c\u4ee5\u786e\u4fdd\u56fe\u8868\u4e0d\u4f1a\u76f8\u4e92\u5e72\u6270\u3002\u8fd9\u5728\u4f7f\u7528Matplotlib\u65f6\u5c24\u4e3a\u91cd\u8981\u3002<\/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)<\/p>\n<p>y1 = np.sin(x)<\/p>\n<p>y2 = np.cos(x)<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e00\u5f20\u56fe<\/strong><\/h2>\n<p>fig1, ax1 = plt.subplots()<\/p>\n<p>ax1.plot(x, y1, label=&#39;sin(x)&#39;)<\/p>\n<p>ax1.set_title(&#39;Sin Curve&#39;)<\/p>\n<p>ax1.set_xlabel(&#39;X-axis&#39;)<\/p>\n<p>ax1.set_ylabel(&#39;Y-axis&#39;)<\/p>\n<p>ax1.legend()<\/p>\n<h2><strong>\u521b\u5efa\u7b2c\u4e8c\u5f20\u56fe<\/strong><\/h2>\n<p>fig2, ax2 = plt.subplots()<\/p>\n<p>ax2.plot(x, y2, label=&#39;cos(x)&#39;, color=&#39;red&#39;)<\/p>\n<p>ax2.set_title(&#39;Cos Curve&#39;)<\/p>\n<p>ax2.set_xlabel(&#39;X-axis&#39;)<\/p>\n<p>ax2.set_ylabel(&#39;Y-axis&#39;)<\/p>\n<p>ax2.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5b50\u56fe\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5728\u540c\u4e00\u4e2a\u56fe\u8868\u7a97\u53e3\u4e2d\u663e\u793a\u591a\u5f20\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684\u5b50\u56fe\u529f\u80fd\u3002<\/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)<\/p>\n<p>y1 = np.sin(x)<\/p>\n<p>y2 = np.cos(x)<\/p>\n<h2><strong>\u521b\u5efa\u5b50\u56fe<\/strong><\/h2>\n<p>fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))<\/p>\n<h2><strong>\u5728\u7b2c\u4e00\u4e2a\u5b50\u56fe\u4e2d\u7ed8\u5236sin(x)<\/strong><\/h2>\n<p>ax1.plot(x, y1, label=&#39;sin(x)&#39;)<\/p>\n<p>ax1.set_title(&#39;Sin Curve&#39;)<\/p>\n<p>ax1.set_xlabel(&#39;X-axis&#39;)<\/p>\n<p>ax1.set_ylabel(&#39;Y-axis&#39;)<\/p>\n<p>ax1.legend()<\/p>\n<h2><strong>\u5728\u7b2c\u4e8c\u4e2a\u5b50\u56fe\u4e2d\u7ed8\u5236cos(x)<\/strong><\/h2>\n<p>ax2.plot(x, y2, label=&#39;cos(x)&#39;, color=&#39;red&#39;)<\/p>\n<p>ax2.set_title(&#39;Cos Curve&#39;)<\/p>\n<p>ax2.set_xlabel(&#39;X-axis&#39;)<\/p>\n<p>ax2.set_ylabel(&#39;Y-axis&#39;)<\/p>\n<p>ax2.legend()<\/p>\n<p>plt.tight_layout()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u7ed8\u5236\u591a\u5f20\u56fe\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u4e0d\u540c\u7684\u7ed8\u56fe\u5e93\u3001\u5bf9\u6bcf\u4e2a\u56fe\u8fdb\u884c\u5355\u72ec\u8bbe\u7f6e\u4ee5\u53ca\u5229\u7528\u5b50\u56fe\u529f\u80fd\u6765\u5b9e\u73b0\u3002<strong>\u4e0d\u540c\u7ed8\u56fe\u5e93\u7684\u9009\u62e9\u3001\u5bf9\u6bcf\u4e2a\u56fe\u7684\u5355\u72ec\u8bbe\u7f6e\u3001\u5b50\u56fe\u529f\u80fd\u7684\u5408\u7406\u4f7f\u7528<\/strong>\u662f\u786e\u4fdd\u7ed8\u56fe\u6548\u679c\u548c\u56fe\u8868\u72ec\u7acb\u6027\u7684\u5173\u952e\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u7ba1\u7406\u548c\u5c55\u793a\u6570\u636e\uff0c\u4ece\u800c\u66f4\u597d\u5730\u4f20\u8fbe\u4fe1\u606f\u548c\u53d1\u73b0\u6570\u636e\u4e2d\u7684\u89c4\u5f8b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684\u8d77\u59cb\u70b9\u7ed8\u5236\u591a\u5f20\u56fe\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u7ed8\u5236\u591a\u5f20\u56fe\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e0d\u540c\u7684\u8d77\u59cb\u70b9\uff0c\u60a8\u53ef\u4ee5\u5728\u540c\u4e00\u56fe\u5f62\u7a97\u53e3\u4e2d\u5c55\u793a\u591a\u4e2a\u6570\u636e\u96c6\u3002\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u6bcf\u7ec4\u6570\u636e\u5e76\u8c03\u7528plot\u51fd\u6570\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5728\u540c\u4e00\u56fe\u5f62\u4e2d\u7ed8\u5236\u4e0d\u540c\u7c7b\u578b\u7684\u56fe\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01Matplotlib\u652f\u6301\u591a\u79cd\u56fe\u5f62\u7c7b\u578b\uff0c\u5305\u62ec\u7ebf\u56fe\u3001\u6563\u70b9\u56fe\u548c\u67f1\u72b6\u56fe\u7b49\u3002\u60a8\u53ef\u4ee5\u5728\u540c\u4e00\u56fe\u5f62\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684\u7ed8\u56fe\u547d\u4ee4\u6765\u7ed8\u5236\u4e0d\u540c\u7c7b\u578b\u7684\u56fe\u5f62\uff0c\u53ea\u9700\u786e\u4fdd\u5728\u6bcf\u6b21\u8c03\u7528\u7ed8\u56fe\u51fd\u6570\u65f6\u8bbe\u7f6e\u5408\u9002\u7684\u8d77\u59cb\u70b9\u548c\u6570\u636e\u96c6\u3002<\/p>\n<p><strong>\u5982\u4f55\u4e3a\u6bcf\u5f20\u56fe\u8bbe\u7f6e\u4e0d\u540c\u7684\u6807\u9898\u548c\u6807\u7b7e\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>plt.title()<\/code>\u3001<code>plt.xlabel()<\/code>\u548c<code>plt.ylabel()<\/code>\u51fd\u6570\u4e3a\u6bcf\u5f20\u56fe\u8bbe\u7f6e\u4e0d\u540c\u7684\u6807\u9898\u548c\u6807\u7b7e\u3002\u53ea\u9700\u5728\u7ed8\u5236\u6bcf\u5f20\u56fe\u65f6\u8c03\u7528\u8fd9\u4e9b\u51fd\u6570\uff0c\u5e76\u4f20\u5165\u76f8\u5e94\u7684\u6807\u9898\u548c\u6807\u7b7e\u5b57\u7b26\u4e32\uff0c\u5373\u53ef\u5b9e\u73b0\u4e2a\u6027\u5316\u7684\u56fe\u5f62\u5c55\u793a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u753b\u56fe\u8d77\u59cb\u70b9\u4e0d\u540c\u65f6\u5982\u4f55\u753b\u591a\u5f20\u56fe \u5728Python\u4e2d\u7ed8\u5236\u591a\u5f20\u56fe\u65f6\uff0c\u5982\u679c\u8d77\u59cb\u70b9\u4e0d\u540c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7ed8\u56fe\u5e93\u3001\u5bf9 [&hellip;]","protected":false},"author":3,"featured_media":1128274,"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\/1128270"}],"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=1128270"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128270\/revisions"}],"predecessor-version":[{"id":1128275,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1128270\/revisions\/1128275"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1128274"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1128270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1128270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1128270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}