{"id":174563,"date":"2024-05-08T18:33:41","date_gmt":"2024-05-08T10:33:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/174563.html"},"modified":"2024-05-08T18:33:44","modified_gmt":"2024-05-08T10:33:44","slug":"python%e4%b8%ad%e7%94%bb%e7%83%ad%e5%9b%be%ef%bc%8c%e5%a6%82%e4%bd%95%e5%8a%a0%e4%b8%8a%e6%b3%a8%e9%87%8a%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/174563.html","title":{"rendered":"python\u4e2d\u753b\u70ed\u56fe\uff0c\u5982\u4f55\u52a0\u4e0a\u6ce8\u91ca\u4fe1\u606f"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27052142\/e072a40d-844b-4bd1-9c82-9dd1305c1a65.webp\" alt=\"python\u4e2d\u753b\u70ed\u56fe\uff0c\u5982\u4f55\u52a0\u4e0a\u6ce8\u91ca\u4fe1\u606f\" \/><\/p>\n<p><p><strong>\u5728Python\u4e2d\u753b\u70ed\u56fe\u901a\u5e38\u4f7f\u7528Seaborn\u5e93\u4e2d\u7684heatmap\u51fd\u6570\u3001\u5229\u7528matplotlib\u8fdb\u884c\u81ea\u5b9a\u4e49\u8bbe\u7f6e\u3002\u8981\u5728\u70ed\u56fe\u4e0a\u6dfb\u52a0\u6ce8\u91ca\u4fe1\u606f\uff0c\u53ef\u4ee5\u5728\u8c03\u7528heatmap\u51fd\u6570\u65f6\u4f7f\u7528\u53c2\u6570<code>annot=True<\/code>\uff0c\u5e76\u901a\u8fc7<code>fmt<\/code>\u53c2\u6570\u8c03\u6574\u6ce8\u91ca\u7684\u6570\u5b57\u683c\u5f0f\u3002<\/strong> \u4f8b\u5982\uff0c\u5982\u679c\u6570\u636e\u96c6\u4e2d\u7684\u6570\u503c\u90fd\u662f\u6574\u6570\uff0c\u53ef\u5c06<code>fmt<\/code>\u8bbe\u7f6e\u4e3a<code>&#039;d&#039;<\/code>\u8fdb\u884c\u683c\u5f0f\u5316\u3002\u82e5\u6570\u636e\u9700\u8981\u5c55\u793a\u5c0f\u6570\u70b9\u540e\u7279\u5b9a\u4f4d\u6570\uff0c\u53ef\u4ee5\u7528<code>&#039;0.2f&#039;<\/code>\u8fdb\u884c\u683c\u5f0f\u5316\uff0c\u8868\u793a\u4fdd\u7559\u4e24\u4f4d\u5c0f\u6570\u3002\u6b64\u5916\uff0cSeaborn\u7684heatmap\u8fd8\u5141\u8bb8\u901a\u8fc7<code>annot_kws<\/code>\u53c2\u6570\u6765\u81ea\u5b9a\u4e49\u6ce8\u91ca\u6587\u672c\u7684\u5927\u5c0f\u548c\u989c\u8272\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u878d\u5408\u4e8e\u6574\u4e2a\u70ed\u56fe\u8bbe\u8ba1\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u4e0e\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u70ed\u56fe\u7684\u7ed8\u5236\u9700\u8981\u501f\u52a9Python\u4e2d\u7684\u51e0\u4e2a\u5e38\u7528\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86matplotlib\u548cseaborn\u8fd9\u4e24\u4e2a\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install matplotlib seaborn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5728\u4f60\u7684\u4ee3\u7801\u6587\u4ef6\u4e2d\u5bfc\u5165\u5b83\u4eec\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<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u51c6\u5907\u9700\u8981\u7ed8\u5236\u70ed\u56fe\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u96c6\uff0c\u6216\u8005\u4ece\u5916\u90e8\u8d44\u6e90\u5bfc\u5165\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u77e9\u9635\u4f5c\u4e3a\u793a\u4f8b<\/strong><\/h2>\n<p>data = np.array([[5, 4, 6], [3, 8, 5], [6, 2, 9]])<\/p>\n<p>labels = [[&#039;A1&#039;, &#039;A2&#039;, &#039;A3&#039;], [&#039;B1&#039;, &#039;B2&#039;, &#039;B3&#039;], [&#039;C1&#039;, &#039;C2&#039;, &#039;C3&#039;]]<\/p>\n<h2><strong>\u5982\u679c\u6570\u636e\u5b58\u50a8\u4e8eCSV\u6216Excel\u6587\u4ef6\uff0c\u4f7f\u7528pandas\u8bfb\u53d6<\/strong><\/h2>\n<h2><strong>data = pd.read_csv(&#039;path_to_your_data.csv&#039;)<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8bbe\u7f6eSeaborn\u7684heatmap\u53c2\u6570\u7ed8\u5236\u70ed\u56fe\u5e76\u6dfb\u52a0\u6ce8\u91ca<\/h3>\n<\/p>\n<p><p>\u7ed8\u5236\u57fa\u7840\u70ed\u56fe\u5e76\u52a0\u4e0a\u9ed8\u8ba4\u6ce8\u91ca\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u70ed\u56fe\u9ed8\u8ba4\u53c2\u6570<\/p>\n<p>sns.heatmap(data, annot=True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b9a\u5236\u5316\u6ce8\u91ca\u98ce\u683c\uff0c\u6bd4\u5982\u6539\u53d8\u5b57\u4f53\u5927\u5c0f\u6216\u989c\u8272\uff0c\u53ef\u4ee5\u4f7f\u7528<code>annot_kws<\/code>\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u81ea\u5b9a\u4e49\u6ce8\u91ca\u6837\u5f0f<\/p>\n<p>sns.heatmap(data, annot=True, fmt=&#039;d&#039;, annot_kws={&quot;size&quot;: 10, &quot;color&quot;: &#039;blue&#039;})<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u8fdb\u4e00\u6b65\u8c03\u6574\u70ed\u56fe\u6837\u5f0f<\/h3>\n<\/p>\n<p><p>\u60a8\u4e5f\u53ef\u4ee5\u6dfb\u52a0\u5982\u6807\u9898\u3001\u8f74\u6807\u7b7e\u3001\u6539\u53d8\u989c\u8272\u6761\u7b49\u8bbe\u7f6e\u8fdb\u4e00\u6b65\u7f8e\u5316\u70ed\u56fe\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u6807\u9898\u548c\u8f74\u6807\u7b7e\uff0c\u5e76\u8c03\u6574\u989c\u8272\u6761<\/p>\n<p>ax = sns.heatmap(data, annot=True, fmt=&#039;d&#039;, annot_kws={&quot;size&quot;: 12})<\/p>\n<p>ax.set_title(&#039;Heatmap with Annotations&#039;, fontsize=16)<\/p>\n<p>ax.set_xlabel(&#039;X Axis Label&#039;, fontsize=12)<\/p>\n<p>ax.set_ylabel(&#039;Y Axis Label&#039;, fontsize=12)<\/p>\n<p>plt.colorbar(ax.collections[0])<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528Pandas DataFrame\u4f5c\u4e3a\u6570\u636e\u6e90<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u60a8\u5728\u4f7f\u7528pandas\u7684DataFrame\u4f5c\u4e3a\u6570\u636e\u6e90\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u7ba1\u7406\u884c\u548c\u5217\u7684\u6807\u7b7e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u636e\u8f7d\u5165DataFrame\u5e76\u6dfb\u52a0\u884c\u5217\u6807\u7b7e<\/p>\n<p>df = pd.DataFrame(data, index=[&#039;row1&#039;, &#039;row2&#039;, &#039;row3&#039;], columns=[&#039;col1&#039;, &#039;col2&#039;, &#039;col3&#039;])<\/p>\n<h2><strong>\u7ed8\u5236DataFrame\u7684\u70ed\u56fe<\/strong><\/h2>\n<p>sns.heatmap(df, annot=True, fmt=&#039;d&#039;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4fdd\u5b58\u70ed\u56fe<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u60a8\u53ef\u80fd\u60f3\u5c06\u751f\u6210\u7684\u70ed\u56fe\u4fdd\u5b58\u4e3a\u56fe\u7247\u6587\u4ef6\uff0c\u8fd9\u5728matplotlib\u4e2d\u975e\u5e38\u7b80\u5355\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u70ed\u56fe\u4e3a\u56fe\u7247\u6587\u4ef6<\/p>\n<p>plt.savefig(&#039;heatmap.png&#039;, dpi=300, bbox_inches=&#039;tight&#039;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\u60a8\u53ef\u4ee5\u5c06\u70ed\u56fe\u89c6\u4e3a\u6570\u636e\u7684\u76f4\u89c2\u5c55\u793a\uff0c\u540c\u65f6\u52a0\u4e0a\u6ce8\u91ca\u4fe1\u606f\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u7406\u89e3\u70ed\u56fe\u4e2d\u6bcf\u4e2a\u5355\u5143\u683c\u7684\u5177\u4f53\u6570\u503c\u3002\u8fd9\u4e9b\u81ea\u5b9a\u4e49\u9009\u9879\u53ef\u4ee5\u8ba9\u70ed\u56fe\u66f4\u7b26\u5408\u60a8\u7684\u5177\u4f53\u9700\u6c42\uff0c\u4f7f\u5176\u65e2\u7f8e\u89c2\u53c8\u5bcc\u6709\u4fe1\u606f\u91cf\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: \u5728Python\u4e2d\u4f7f\u7528Matplotlib\u7ed8\u5236\u70ed\u56fe\u65f6\uff0c\u5982\u4f55\u6dfb\u52a0\u6ce8\u91ca\u4fe1\u606f\uff1f<\/strong><br \/>\n<strong>Q: Python\u4e2d\u753b\u70ed\u56fe\u65f6\uff0c\u5982\u4f55\u5728\u56fe\u4e0a\u6807\u6ce8\u989d\u5916\u7684\u4fe1\u606f\uff1f<\/strong><br \/>\n<strong>Q: \u7ed8\u5236\u70ed\u56fe\u65f6\uff0c\u5982\u4f55\u5728Python\u4e2d\u6dfb\u52a0\u6ce8\u89e3\u4fe1\u606f\uff1f<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u753b\u70ed\u56fe\u901a\u5e38\u4f7f\u7528Seaborn\u5e93\u4e2d\u7684heatmap\u51fd\u6570\u3001\u5229\u7528matplotlib\u8fdb\u884c\u81ea\u5b9a\u4e49\u8bbe\u7f6e\u3002 [&hellip;]","protected":false},"author":3,"featured_media":174567,"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\/174563"}],"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=174563"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/174563\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/174567"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=174563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=174563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=174563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}