{"id":1090751,"date":"2025-01-08T14:05:17","date_gmt":"2025-01-08T06:05:17","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1090751.html"},"modified":"2025-01-08T14:05:19","modified_gmt":"2025-01-08T06:05:19","slug":"%e7%94%a8python%e5%a6%82%e4%bd%95%e5%81%9a%e7%9f%a9%e9%98%b5%e6%b0%94%e6%b3%a1%e5%9b%be-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1090751.html","title":{"rendered":"\u7528python\u5982\u4f55\u505a\u77e9\u9635\u6c14\u6ce1\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24203243\/6bcff10e-6e52-4811-8097-7a3f244af1e8.webp\" alt=\"\u7528python\u5982\u4f55\u505a\u77e9\u9635\u6c14\u6ce1\u56fe\" \/><\/p>\n<p><p> <strong>\u7528Python\u5982\u4f55\u505a\u77e9\u9635\u6c14\u6ce1\u56fe<\/strong><\/p>\n<\/p>\n<p><p><strong>\u7528Python\u7ed8\u5236\u77e9\u9635\u6c14\u6ce1\u56fe\u9700\u8981\u4f7f\u7528\u7684\u5de5\u5177\u3001\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u521b\u5efa\u6570\u636e\u3001\u4f7f\u7528matplotlib\u6216seaborn\u5e93\u3001\u8bbe\u7f6e\u56fe\u5f62\u5c5e\u6027<\/strong>\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528Python\u6765\u7ed8\u5236\u4e00\u4e2a\u77e9\u9635\u6c14\u6ce1\u56fe\uff0c\u5e76\u4ecb\u7ecd\u5982\u4f55\u8c03\u6574\u56fe\u5f62\u7684\u5404\u9879\u5c5e\u6027\u4ee5\u83b7\u5f97\u6700\u4f73\u7684\u53ef\u89c6\u5316\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7ed8\u5236\u77e9\u9635\u6c14\u6ce1\u56fe\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165\u4e00\u4e9b\u5fc5\u9700\u7684\u5e93\u3002\u8fd9\u4e9b\u5e93\u5305\u62ecNumPy\u3001Pandas\u3001Matplotlib\u548cSeaborn\u3002NumPy\u7528\u4e8e\u5904\u7406\u6570\u503c\u6570\u636e\uff0cPandas\u7528\u4e8e\u6570\u636e\u64cd\u4f5c\uff0cMatplotlib\u548cSeaborn\u5219\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>import seaborn as sns<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u96c6\u3002\u8fd9\u4e2a\u6570\u636e\u96c6\u53ef\u4ee5\u662f\u4ece\u6587\u4ef6\u8bfb\u53d6\u7684\u6570\u636e\uff0c\u4e5f\u53ef\u4ee5\u662f\u624b\u52a8\u521b\u5efa\u7684\u6570\u636e\u3002\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u5c06\u4f7f\u7528Pandas\u5e93\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u6846\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u793a\u4f8b\u6570\u636e<\/p>\n<p>data = {<\/p>\n<p>    &#39;A&#39;: [10, 20, 30, 40],<\/p>\n<p>    &#39;B&#39;: [20, 30, 40, 50],<\/p>\n<p>    &#39;C&#39;: [30, 40, 50, 60],<\/p>\n<p>    &#39;D&#39;: [40, 50, 60, 70]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Matplotlib\u7ed8\u5236\u77e9\u9635\u6c14\u6ce1\u56fe<\/h3>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u7ed8\u5236\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\uff0c\u5305\u62ec\u77e9\u9635\u6c14\u6ce1\u56fe\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Matplotlib\u7ed8\u5236\u77e9\u9635\u6c14\u6ce1\u56fe\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u6570\u636e\u7684\u884c\u548c\u5217<\/p>\n<p>rows, cols = df.shape<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u548c\u4e00\u4e2a\u5b50\u56fe<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u636e\u6846\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u5728\u5bf9\u5e94\u7684\u4f4d\u7f6e\u7ed8\u5236\u6c14\u6ce1<\/strong><\/h2>\n<p>for i in range(rows):<\/p>\n<p>    for j in range(cols):<\/p>\n<p>        ax.scatter(j, i, s=df.iloc[i, j] * 10, alpha=0.5)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u6807\u7b7e<\/strong><\/h2>\n<p>ax.set_xticks(range(cols))<\/p>\n<p>ax.set_xticklabels(df.columns)<\/p>\n<p>ax.set_yticks(range(rows))<\/p>\n<p>ax.set_yticklabels(df.index)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Seaborn\u7ed8\u5236\u77e9\u9635\u6c14\u6ce1\u56fe<\/h3>\n<\/p>\n<p><p>Seaborn\u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u548c\u7f8e\u89c2\u7684\u7ed8\u56fe\u63a5\u53e3\u3002\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528Seaborn\u5e93\u6765\u7ed8\u5236\u77e9\u9635\u6c14\u6ce1\u56fe\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Seaborn\u7ed8\u5236\u77e9\u9635\u6c14\u6ce1\u56fe\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u6570\u636e\u6846\u8f6c\u6362\u4e3a\u957f\u683c\u5f0f<\/p>\n<p>df_long = df.reset_index().melt(id_vars=&#39;index&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u548c\u4e00\u4e2a\u5b50\u56fe<\/strong><\/h2>\n<p>fig, ax = plt.subplots()<\/p>\n<h2><strong>\u4f7f\u7528Seaborn\u7684scatterplot\u51fd\u6570\u7ed8\u5236\u6c14\u6ce1\u56fe<\/strong><\/h2>\n<p>sns.scatterplot(<\/p>\n<p>    data=df_long,<\/p>\n<p>    x=&#39;variable&#39;,<\/p>\n<p>    y=&#39;index&#39;,<\/p>\n<p>    size=&#39;value&#39;,<\/p>\n<p>    sizes=(100, 1000),<\/p>\n<p>    alpha=0.5,<\/p>\n<p>    legend=False,<\/p>\n<p>    ax=ax<\/p>\n<p>)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u6807\u7b7e<\/strong><\/h2>\n<p>ax.set_xticks(range(cols))<\/p>\n<p>ax.set_xticklabels(df.columns)<\/p>\n<p>ax.set_yticks(range(rows))<\/p>\n<p>ax.set_yticklabels(df.index)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8bbe\u7f6e\u56fe\u5f62\u5c5e\u6027<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u77e9\u9635\u6c14\u6ce1\u56fe\u66f4\u52a0\u7f8e\u89c2\u548c\u6613\u8bfb\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u6574\u4e00\u4e9b\u56fe\u5f62\u5c5e\u6027\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u6c14\u6ce1\u7684\u989c\u8272\u3001\u900f\u660e\u5ea6\u3001\u5927\u5c0f\u6bd4\u4f8b\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528Seaborn\u7684scatterplot\u51fd\u6570\u7ed8\u5236\u6c14\u6ce1\u56fe\uff0c\u5e76\u8bbe\u7f6e\u6c14\u6ce1\u989c\u8272\u548c\u5927\u5c0f\u6bd4\u4f8b<\/p>\n<p>sns.scatterplot(<\/p>\n<p>    data=df_long,<\/p>\n<p>    x=&#39;variable&#39;,<\/p>\n<p>    y=&#39;index&#39;,<\/p>\n<p>    size=&#39;value&#39;,<\/p>\n<p>    hue=&#39;value&#39;,<\/p>\n<p>    sizes=(100, 1000),<\/p>\n<p>    palette=&#39;coolwarm&#39;,<\/p>\n<p>    alpha=0.5,<\/p>\n<p>    legend=False,<\/p>\n<p>    ax=ax<\/p>\n<p>)<\/p>\n<h2><strong>\u8bbe\u7f6e\u56fe\u5f62\u6807\u9898\u548c\u5750\u6807\u8f74\u6807\u7b7e<\/strong><\/h2>\n<p>ax.set_title(&#39;Matrix Bubble Chart&#39;)<\/p>\n<p>ax.set_xlabel(&#39;Columns&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Rows&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4fdd\u5b58\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u548c\u8c03\u6574\u56fe\u5f62\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u4fdd\u5b58\u4e3a\u6587\u4ef6\uff0c\u4ee5\u4fbf\u5728\u62a5\u544a\u6216\u5176\u4ed6\u5730\u65b9\u4f7f\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684<code>savefig<\/code>\u51fd\u6570\u5c06\u56fe\u5f62\u4fdd\u5b58\u4e3aPNG\u3001JPG\u3001PDF\u7b49\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u56fe\u5f62\u4e3aPNG\u6587\u4ef6<\/p>\n<p>fig.savefig(&#39;matrix_bubble_chart.png&#39;)<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62\u4e3aPDF\u6587\u4ef6<\/strong><\/h2>\n<p>fig.savefig(&#39;matrix_bubble_chart.pdf&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u8ba8\u8bba\u4e86\u5982\u4f55\u4f7f\u7528Python\u6765\u7ed8\u5236\u4e00\u4e2a\u77e9\u9635\u6c14\u6ce1\u56fe\u3002\u6211\u4eec\u4ecb\u7ecd\u4e86\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u521b\u5efa\u6570\u636e\u3001\u4f7f\u7528Matplotlib\u548cSeaborn\u5e93\u7ed8\u5236\u6c14\u6ce1\u56fe\u3001\u8bbe\u7f6e\u56fe\u5f62\u5c5e\u6027\u4ee5\u53ca\u4fdd\u5b58\u56fe\u5f62\u7684\u6b65\u9aa4\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u7f8e\u89c2\u4e14\u4fe1\u606f\u4e30\u5bcc\u7684\u77e9\u9635\u6c14\u6ce1\u56fe\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u5c55\u793a\u548c\u5206\u6790\u6570\u636e\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u5e76\u80fd\u591f\u5728\u60a8\u7684\u6570\u636e\u53ef\u89c6\u5316\u5de5\u4f5c\u4e2d\u63d0\u4f9b\u6709\u7528\u7684\u6307\u5bfc\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u521b\u5efa\u77e9\u9635\u6c14\u6ce1\u56fe\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u521b\u5efa\u77e9\u9635\u6c14\u6ce1\u56fe\u901a\u5e38\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u548c<code>numpy<\/code>\u5e93\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5\u8fd9\u4e24\u4e2a\u5e93\uff0c\u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install matplotlib numpy<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u63a5\u4e0b\u6765\uff0c\u53ef\u4ee5\u901a\u8fc7\u751f\u6210\u968f\u673a\u6570\u636e\u6216\u4f7f\u7528\u5b9e\u9645\u6570\u636e\u6765\u521b\u5efa\u6c14\u6ce1\u56fe\u3002\u4f7f\u7528<code>scatter<\/code>\u51fd\u6570\u6765\u7ed8\u5236\u6c14\u6ce1\uff0c\u786e\u4fdd\u8bbe\u7f6e\u6c14\u6ce1\u7684\u5927\u5c0f\u548c\u989c\u8272\u4ee5\u4f20\u8fbe\u4e0d\u540c\u7684\u4fe1\u606f\u3002<\/p>\n<p><strong>\u6c14\u6ce1\u7684\u5927\u5c0f\u548c\u989c\u8272\u5982\u4f55\u5f71\u54cd\u77e9\u9635\u6c14\u6ce1\u56fe\u7684\u5c55\u793a\u6548\u679c\uff1f<\/strong><br \/>\u6c14\u6ce1\u7684\u5927\u5c0f\u548c\u989c\u8272\u53ef\u4ee5\u4f20\u8fbe\u591a\u7ef4\u6570\u636e\u7684\u4fe1\u606f\u3002\u901a\u5e38\uff0c\u6c14\u6ce1\u7684\u5927\u5c0f\u53ef\u4ee5\u8868\u793a\u67d0\u4e2a\u53d8\u91cf\u7684\u6570\u503c\uff0c\u6bd4\u5982\u9500\u552e\u989d\u6216\u4eba\u53e3\uff0c\u800c\u989c\u8272\u5219\u53ef\u4ee5\u7528\u6765\u5206\u7c7b\uff0c\u5982\u5730\u533a\u6216\u4ea7\u54c1\u7c7b\u578b\u3002\u901a\u8fc7\u9002\u5f53\u5730\u9009\u62e9\u5927\u5c0f\u548c\u989c\u8272\uff0c\u53ef\u4ee5\u4f7f\u56fe\u8868\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u4fe1\u606f\u91cf\uff0c\u5e2e\u52a9\u7528\u6237\u5feb\u901f\u7406\u89e3\u6570\u636e\u7684\u5206\u5e03\u548c\u8d8b\u52bf\u3002<\/p>\n<p><strong>\u5728\u77e9\u9635\u6c14\u6ce1\u56fe\u4e2d\uff0c\u5982\u4f55\u6709\u6548\u5730\u6807\u8bb0\u5750\u6807\u8f74\uff1f<\/strong><br \/>\u6807\u8bb0\u5750\u6807\u8f74\u662f\u786e\u4fdd\u56fe\u8868\u53ef\u8bfb\u6027\u7684\u91cd\u8981\u6b65\u9aa4\u3002\u53ef\u4ee5\u4f7f\u7528<code>plt.xlabel()<\/code>\u548c<code>plt.ylabel()<\/code>\u51fd\u6570\u8bbe\u7f6e\u5750\u6807\u8f74\u7684\u6807\u7b7e\uff0c\u4ee5\u4fbf\u6e05\u6670\u5730\u5c55\u793a\u6570\u636e\u7684\u542b\u4e49\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>plt.xticks()<\/code>\u548c<code>plt.yticks()<\/code>\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5750\u6807\u8f74\u7684\u523b\u5ea6\u548c\u6807\u7b7e\uff0c\u4f7f\u5176\u66f4\u5177\u53ef\u8bfb\u6027\u3002\u786e\u4fdd\u9009\u62e9\u7684\u6807\u7b7e\u7b80\u6d01\u660e\u4e86\uff0c\u6709\u52a9\u4e8e\u89c2\u4f17\u5feb\u901f\u7406\u89e3\u56fe\u8868\u6240\u4f20\u8fbe\u7684\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u7528Python\u5982\u4f55\u505a\u77e9\u9635\u6c14\u6ce1\u56fe \u7528Python\u7ed8\u5236\u77e9\u9635\u6c14\u6ce1\u56fe\u9700\u8981\u4f7f\u7528\u7684\u5de5\u5177\u3001\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u521b\u5efa\u6570\u636e\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1090754,"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\/1090751"}],"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=1090751"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1090751\/revisions"}],"predecessor-version":[{"id":1090755,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1090751\/revisions\/1090755"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1090754"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1090751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1090751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1090751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}