{"id":986118,"date":"2024-12-27T07:42:10","date_gmt":"2024-12-26T23:42:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/986118.html"},"modified":"2024-12-27T07:42:12","modified_gmt":"2024-12-26T23:42:12","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e7%83%ad%e5%8a%9b%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/986118.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u70ed\u529b\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25062842\/ee0e0ca0-659d-4fab-93aa-77c4a615235e.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u70ed\u529b\u56fe\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9e\u73b0\u70ed\u529b\u56fe\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u4f8b\u5982Matplotlib\u3001Seaborn\u6216Plotly\u3002\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u51c6\u5907\u6570\u636e\u3001\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u51fd\u6570\u3001\u914d\u7f6e\u56fe\u50cf\u5c5e\u6027\u3001\u5c55\u793a\u56fe\u50cf\u3002\u4ee5Seaborn\u4e3a\u4f8b\uff0c\u901a\u8fc7\u52a0\u8f7d\u6570\u636e\u96c6\u3001\u4f7f\u7528<code>heatmap<\/code>\u51fd\u6570\u7ed8\u5236\u70ed\u529b\u56fe\uff0c\u5e76\u8fdb\u884c\u989c\u8272\u3001\u6807\u7b7e\u7b49\u65b9\u9762\u7684\u81ea\u5b9a\u4e49\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u51c6\u5907\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u70ed\u529b\u56fe\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u51c6\u5907\u597d\u6570\u636e\u3002\u901a\u5e38\u70ed\u529b\u56fe\u7528\u4e8e\u8868\u793a\u4e8c\u7ef4\u6570\u636e\u7684\u5f3a\u5ea6\u6216\u5bc6\u5ea6\uff0c\u56e0\u6b64\u6570\u636e\u9700\u8981\u4ee5\u77e9\u9635\u6216\u4e8c\u7ef4\u6570\u7ec4\u7684\u5f62\u5f0f\u5b58\u5728\u3002\u53ef\u4ee5\u4eceCSV\u6587\u4ef6\u3001\u6570\u636e\u5e93\u6216API\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Pandas\u65f6\uff0c\u901a\u5e38\u4f1a\u5c06\u6570\u636e\u8bfb\u53d6\u5230DataFrame\u4e2d\u5e76\u8fdb\u884c\u5fc5\u8981\u7684\u6e05\u6d17\u548c\u8f6c\u6362\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4eceCSV\u6587\u4ef6\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u6e05\u6d17\u548c\u8f6c\u6362\u6570\u636e<\/strong><\/h2>\n<p>data_pivot = data.pivot(&#39;row_feature&#39;, &#39;column_feature&#39;, &#39;value_feature&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u9009\u62e9\u9002\u5408\u7684\u5e93\u548c\u51fd\u6570<\/p>\n<\/p>\n<p><p>Python\u6709\u591a\u4e2a\u53ef\u7528\u7684\u5e93\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u70ed\u529b\u56fe\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fSeaborn\u548cMatplotlib\u3002Seaborn\u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u63a5\u53e3\uff0c\u63d0\u4f9b\u4e86\u66f4\u7b80\u6d01\u548c\u7f8e\u89c2\u7684\u7ed8\u56fe\u51fd\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li>Seaborn<\/li>\n<\/ol>\n<p><p>Seaborn\u7b80\u5316\u4e86\u70ed\u529b\u56fe\u7684\u521b\u5efa\u8fc7\u7a0b\uff0c\u63d0\u4f9b\u4e86<code>heatmap<\/code>\u51fd\u6570\u3002\u4f7f\u7528Seaborn\u7ed8\u5236\u70ed\u529b\u56fe\u7684\u6b65\u9aa4\u5982\u4e0b\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>\u8bbe\u7f6e\u7ed8\u56fe\u98ce\u683c<\/strong><\/h2>\n<p>sns.set_theme()<\/p>\n<h2><strong>\u521b\u5efa\u70ed\u529b\u56fe<\/strong><\/h2>\n<p>sns.heatmap(data_pivot, annot=True, fmt=&quot;d&quot;, cmap=&quot;YlGnBu&quot;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u50cf<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>annot=True<\/code>\u8868\u793a\u5728\u6bcf\u4e2a\u5355\u5143\u683c\u4e2d\u663e\u793a\u6570\u503c\uff0c<code>fmt=&quot;d&quot;<\/code>\u8868\u793a\u6570\u503c\u683c\u5f0f\u4e3a\u6574\u6570\uff0c<code>cmap=&quot;YlGnBu&quot;<\/code>\u6307\u5b9a\u4e86\u989c\u8272\u6620\u5c04\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>Matplotlib<\/li>\n<\/ol>\n<p><p>\u867d\u7136Seaborn\u57fa\u4e8eMatplotlib\uff0c\u4f46\u6709\u65f6\u76f4\u63a5\u4f7f\u7528Matplotlib\u80fd\u63d0\u4f9b\u66f4\u9ad8\u7684\u81ea\u5b9a\u4e49\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>plt.imshow(data_pivot, cmap=&#39;hot&#39;, interpolation=&#39;nearest&#39;)<\/p>\n<p>plt.colorbar()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u63d0\u4f9b\u4e86\u5bf9\u56fe\u50cf\u66f4\u4f4e\u5c42\u7684\u63a7\u5236\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u7ec6\u8282\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u914d\u7f6e\u56fe\u50cf\u5c5e\u6027<\/p>\n<\/p>\n<p><p>\u7ed8\u5236\u70ed\u529b\u56fe\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u81ea\u5b9a\u4e49\u56fe\u50cf\u7684\u5916\u89c2\u3002\u4f8b\u5982\uff0c\u8c03\u6574\u989c\u8272\u6620\u5c04\u3001\u8bbe\u7f6e\u6807\u7b7e\u548c\u6807\u9898\u3001\u4fee\u6539\u5750\u6807\u8f74\u7b49\u3002<\/p>\n<\/p>\n<ol>\n<li>\u989c\u8272\u6620\u5c04<\/li>\n<\/ol>\n<p><p>\u989c\u8272\u6620\u5c04\u7528\u4e8e\u8868\u793a\u6570\u636e\u7684\u4e0d\u540c\u503c\uff0c\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7279\u70b9\u9009\u62e9\u5408\u9002\u7684\u989c\u8272\u65b9\u6848\u3002Seaborn\u548cMatplotlib\u90fd\u63d0\u4f9b\u4e86\u591a\u79cd\u989c\u8272\u65b9\u6848\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sns.heatmap(data_pivot, cmap=&quot;coolwarm&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/li>\n<\/ol>\n<p><p>\u4e3a\u56fe\u50cf\u6dfb\u52a0\u6807\u9898\u548c\u8f74\u6807\u7b7e\u6709\u52a9\u4e8e\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.title(&#39;Heatmap Title&#39;)<\/p>\n<p>plt.xlabel(&#39;X-axis Label&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis Label&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u8c03\u6574\u5750\u6807\u8f74<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u5750\u6807\u8f74\u7684\u523b\u5ea6\u548c\u6807\u7b7e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.xticks(rotation=45)<\/p>\n<p>plt.yticks(rotation=0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5c55\u793a\u548c\u4fdd\u5b58\u56fe\u50cf<\/p>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u70ed\u529b\u56fe\u7684\u7ed8\u5236\u548c\u81ea\u5b9a\u4e49\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7Matplotlib\u7684<code>show<\/code>\u51fd\u6570\u5c06\u56fe\u50cf\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\uff0c\u6216\u8005\u4f7f\u7528<code>savefig<\/code>\u51fd\u6570\u5c06\u56fe\u50cf\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.show()  # \u663e\u793a\u56fe\u50cf<\/p>\n<p>plt.savefig(&#39;heatmap.png&#39;)  # \u4fdd\u5b58\u56fe\u50cf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u8fdb\u9636\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5728\u638c\u63e1\u57fa\u672c\u70ed\u529b\u56fe\u7ed8\u5236\u540e\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u66f4\u590d\u6742\u7684\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u7ed3\u5408Pandas\u548cSeaborn\u8fdb\u884c\u6570\u636e\u5206\u6790\uff0c\u4f7f\u7528Plotly\u521b\u5efa\u4ea4\u4e92\u5f0f\u70ed\u529b\u56fe\uff0c\u6216\u5728Jupyter Notebook\u4e2d\u8fdb\u884c\u52a8\u6001\u6f14\u793a\u3002<\/p>\n<\/p>\n<ol>\n<li>\u52a8\u6001\u70ed\u529b\u56fe<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528Plotly\u5e93\uff0c\u53ef\u4ee5\u521b\u5efa\u4ea4\u4e92\u5f0f\u70ed\u529b\u56fe\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u9f20\u6807\u60ac\u505c\u67e5\u770b\u8be6\u7ec6\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.express as px<\/p>\n<p>fig = px.imshow(data_pivot, color_continuous_scale=&#39;Viridis&#39;)<\/p>\n<p>fig.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u591a\u56fe\u7ec4\u5408<\/li>\n<\/ol>\n<p><p>\u6709\u65f6\u9700\u8981\u5c06\u591a\u4e2a\u70ed\u529b\u56fe\u7ec4\u5408\u5728\u4e00\u8d77\u4ee5\u5c55\u793a\u66f4\u4e30\u5bcc\u7684\u4fe1\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7684<code>subplot<\/code>\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">fig, axs = plt.subplots(1, 2, figsize=(12, 6))<\/p>\n<p>sns.heatmap(data_pivot1, ax=axs[0], cmap=&quot;Blues&quot;)<\/p>\n<p>sns.heatmap(data_pivot2, ax=axs[1], cmap=&quot;Reds&quot;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u7075\u6d3b\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u70ed\u529b\u56fe\uff0c\u4e3a\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5e93\u751f\u6210\u70ed\u529b\u56fe\uff1f<\/strong><br \/>\u751f\u6210\u70ed\u529b\u56fe\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684<code>matplotlib<\/code>\u548c<code>seaborn<\/code>\u5e93\u3002<code>seaborn<\/code>\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u4fbf\u7684\u65b9\u6cd5\u6765\u521b\u5efa\u70ed\u529b\u56fe\uff0c\u4f60\u53ea\u9700\u51c6\u5907\u4e00\u4e2a\u4e8c\u7ef4\u6570\u636e\u96c6\uff08\u5982DataFrame\uff09\uff0c\u7136\u540e\u8c03\u7528<code>seaborn.heatmap()<\/code>\u51fd\u6570\uff0c\u4f20\u5165\u6570\u636e\u548c\u4e00\u4e9b\u53c2\u6570\u5373\u53ef\u3002\u6b64\u5916\uff0c\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8272\u5f69\u6620\u5c04\u53c2\u6570\u6765\u8c03\u6574\u70ed\u529b\u56fe\u7684\u989c\u8272\u98ce\u683c\uff0c\u4f7f\u5176\u66f4\u5177\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u81ea\u5b9a\u4e49\u70ed\u529b\u56fe\u7684\u989c\u8272\u548c\u6807\u7b7e\uff1f<\/strong><br \/>\u5728\u521b\u5efa\u70ed\u529b\u56fe\u65f6\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7<code>cmap<\/code>\u53c2\u6570\u81ea\u5b9a\u4e49\u989c\u8272\u6620\u5c04\uff0c\u5982<code>cmap=&#39;YlGnBu&#39;<\/code>\u6765\u9009\u62e9\u6e10\u53d8\u8272\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>xticklabels<\/code>\u548c<code>yticklabels<\/code>\u53c2\u6570\u6765\u6dfb\u52a0\u81ea\u5b9a\u4e49\u7684\u5750\u6807\u8f74\u6807\u7b7e\uff0c\u8fd9\u6837\u53ef\u4ee5\u8ba9\u70ed\u529b\u56fe\u66f4\u5177\u4fe1\u606f\u6027\u548c\u6613\u8bfb\u6027\u3002\u901a\u8fc7\u8fd9\u4e9b\u81ea\u5b9a\u4e49\u9009\u9879\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8c03\u6574\u70ed\u529b\u56fe\u7684\u5c55\u793a\u6548\u679c\u3002<\/p>\n<p><strong>\u70ed\u529b\u56fe\u9002\u5408\u7528\u4e8e\u54ea\u4e9b\u7c7b\u578b\u7684\u6570\u636e\u5206\u6790\uff1f<\/strong><br \/>\u70ed\u529b\u56fe\u975e\u5e38\u9002\u5408\u53ef\u89c6\u5316\u5177\u6709\u5bc6\u5ea6\u6216\u5f3a\u5ea6\u7684\u4e8c\u7ef4\u6570\u636e\uff0c\u4f8b\u5982\u76f8\u5173\u6027\u77e9\u9635\u3001\u5730\u7406\u6570\u636e\u7684\u5206\u5e03\u6216\u7528\u6237\u884c\u4e3a\u6570\u636e\u3002\u5b83\u80fd\u591f\u76f4\u89c2\u5730\u5c55\u793a\u6570\u636e\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u4f7f\u5f97\u6570\u636e\u5206\u6790\u4eba\u5458\u53ef\u4ee5\u5feb\u901f\u8bc6\u522b\u51fa\u6a21\u5f0f\u548c\u5f02\u5e38\u503c\u3002\u5c24\u5176\u5728\u6570\u636e\u91cf\u8f83\u5927\u65f6\uff0c\u70ed\u529b\u56fe\u80fd\u591f\u6709\u6548\u7b80\u5316\u4fe1\u606f\uff0c\u4f7f\u5f97\u590d\u6742\u7684\u6570\u636e\u66f4\u6613\u4e8e\u7406\u89e3\u548c\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9e\u73b0\u70ed\u529b\u56fe\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u4f8b\u5982Matplotlib\u3001Seaborn\u6216Plotly\u3002 [&hellip;]","protected":false},"author":3,"featured_media":986121,"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\/986118"}],"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=986118"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986118\/revisions"}],"predecessor-version":[{"id":986124,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/986118\/revisions\/986124"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/986121"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=986118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=986118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=986118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}