{"id":1072794,"date":"2025-01-08T11:19:26","date_gmt":"2025-01-08T03:19:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1072794.html"},"modified":"2025-01-08T11:19:28","modified_gmt":"2025-01-08T03:19:28","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e7%9f%a9%e9%98%b5%e5%86%99%e4%b8%ba%e5%9b%be%e7%89%87-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1072794.html","title":{"rendered":"python\u5982\u4f55\u5c06\u77e9\u9635\u5199\u4e3a\u56fe\u7247"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102915\/121dc975-f3f1-4f8b-a2d4-a7504e349775.webp\" alt=\"python\u5982\u4f55\u5c06\u77e9\u9635\u5199\u4e3a\u56fe\u7247\" \/><\/p>\n<p><p> <strong>Python\u5c06\u77e9\u9635\u5199\u4e3a\u56fe\u7247\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528NumPy\u521b\u5efa\u6216\u52a0\u8f7d\u77e9\u9635\u3001\u4f7f\u7528Matplotlib\u6216Pillow\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3a\u56fe\u7247\u3001\u9009\u62e9\u9002\u5f53\u7684\u989c\u8272\u6620\u5c04\u548c\u683c\u5f0f<\/strong>\u3002\u5176\u4e2d\uff0c\u5229\u7528Matplotlib\u6765\u4fdd\u5b58\u56fe\u7247\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5176\u652f\u6301\u591a\u79cd\u989c\u8272\u6620\u5c04\u548c\u81ea\u5b9a\u4e49\u9009\u9879\u3002\u63a5\u4e0b\u6765\uff0c\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528Matplotlib\u6765\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528NumPy\u521b\u5efa\u6216\u52a0\u8f7d\u77e9\u9635<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u7684\u57fa\u7840\u5e93\u3002\u6211\u4eec\u9996\u5148\u9700\u8981\u521b\u5efa\u6216\u52a0\u8f7d\u4e00\u4e2a\u77e9\u9635\uff0c\u53ef\u4ee5\u662f\u968f\u673a\u751f\u6210\u7684\u77e9\u9635\uff0c\u4e5f\u53ef\u4ee5\u662f\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u968f\u673a\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.random.rand(100, 100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Matplotlib\u4fdd\u5b58\u77e9\u9635\u4e3a\u56fe\u7247<\/h3>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3a\u56fe\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u4f7f\u7528Matplotlib\u7684imshow\u65b9\u6cd5\u663e\u793a\u77e9\u9635<\/strong><\/h2>\n<p>plt.imshow(matrix, cmap=&#39;viridis&#39;)  # \u4f7f\u7528viridis\u989c\u8272\u6620\u5c04<\/p>\n<p>plt.colorbar()  # \u6dfb\u52a0\u989c\u8272\u6761<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u7247<\/strong><\/h2>\n<p>plt.savefig(&#39;matrix_image.png&#39;)<\/p>\n<p>plt.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u9009\u62e9\u989c\u8272\u6620\u5c04\u548c\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>Matplotlib\u652f\u6301\u591a\u79cd\u989c\u8272\u6620\u5c04\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u989c\u8272\u6620\u5c04\u3002\u5e38\u89c1\u7684\u989c\u8272\u6620\u5c04\u5305\u62ec<code>viridis<\/code>, <code>plasma<\/code>, <code>inferno<\/code>, <code>magma<\/code>\u7b49\u3002\u4fdd\u5b58\u56fe\u7247\u65f6\uff0c\u8fd8\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u683c\u5f0f\uff0c\u5982<code>png<\/code>, <code>jpg<\/code>, <code>tiff<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.imshow(matrix, cmap=&#39;plasma&#39;)  # \u4f7f\u7528plasma\u989c\u8272\u6620\u5c04<\/p>\n<p>plt.colorbar()<\/p>\n<p>plt.savefig(&#39;matrix_image.jpg&#39;, format=&#39;jpg&#39;)<\/p>\n<p>plt.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Pillow\u4fdd\u5b58\u77e9\u9635\u4e3a\u56fe\u7247<\/h3>\n<\/p>\n<p><p>Pillow\u662f\u53e6\u4e00\u4e2a\u5904\u7406\u56fe\u50cf\u7684\u5e93\uff0c\u53ef\u4ee5\u76f4\u63a5\u5c06NumPy\u6570\u7ec4\u8f6c\u6362\u4e3a\u56fe\u7247\u5e76\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from PIL import Image<\/p>\n<h2><strong>\u5c06\u77e9\u9635\u5f52\u4e00\u5316\u52300-255\u8303\u56f4\u5185<\/strong><\/h2>\n<p>matrix_normalized = (matrix * 255).astype(np.uint8)<\/p>\n<h2><strong>\u4f7f\u7528Pillow\u4fdd\u5b58\u56fe\u7247<\/strong><\/h2>\n<p>image = Image.fromarray(matrix_normalized)<\/p>\n<p>image.save(&#39;matrix_image_pillow.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5927\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b50\u91c7\u6837\u6216\u5206\u5757\u5904\u7406\uff0c\u4ee5\u907f\u514d\u5185\u5b58\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b50\u91c7\u6837<\/p>\n<p>matrix_subsampled = matrix[::2, ::2]  # \u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\u91c7\u6837<\/p>\n<p>plt.imshow(matrix_subsampled, cmap=&#39;viridis&#39;)<\/p>\n<p>plt.colorbar()<\/p>\n<p>plt.savefig(&#39;matrix_subsampled.png&#39;)<\/p>\n<p>plt.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u6dfb\u52a0\u6807\u9898\u548c\u8f74\u6807\u7b7e<\/h3>\n<\/p>\n<p><p>\u5728\u4fdd\u5b58\u56fe\u7247\u65f6\uff0c\u53ef\u4ee5\u6dfb\u52a0\u6807\u9898\u548c\u8f74\u6807\u7b7e\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u63cf\u8ff0\u56fe\u7247\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.imshow(matrix, cmap=&#39;viridis&#39;)<\/p>\n<p>plt.colorbar()<\/p>\n<p>plt.title(&#39;Random Matrix&#39;)<\/p>\n<p>plt.xlabel(&#39;X-axis&#39;)<\/p>\n<p>plt.ylabel(&#39;Y-axis&#39;)<\/p>\n<p>plt.savefig(&#39;matrix_with_labels.png&#39;)<\/p>\n<p>plt.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u7070\u5ea6\u56fe\u50cf\u548c\u5f69\u8272\u56fe\u50cf<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u5904\u7406\u7070\u5ea6\u56fe\u50cf\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u5f69\u8272\u56fe\u50cf\u3002\u5bf9\u4e8e\u5f69\u8272\u56fe\u50cf\uff0c\u77e9\u9635\u7684\u5f62\u72b6\u5e94\u8be5\u662f<code>(height, width, 3)<\/code>\u6216<code>(height, width, 4)<\/code>\uff0c\u5206\u522b\u8868\u793aRGB\u548cRGBA\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u5f69\u8272\u77e9\u9635<\/p>\n<p>matrix_rgb = np.random.rand(100, 100, 3)<\/p>\n<p>plt.imshow(matrix_rgb)<\/p>\n<p>plt.savefig(&#39;matrix_rgb.png&#39;)<\/p>\n<p>plt.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528\u4e0d\u540c\u7684\u63d2\u503c\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Matplotlib\u652f\u6301\u591a\u79cd\u63d2\u503c\u65b9\u6cd5\uff0c\u5982<code>nearest<\/code>, <code>bilinear<\/code>, <code>bicubic<\/code>\u7b49\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u63d2\u503c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.imshow(matrix, cmap=&#39;viridis&#39;, interpolation=&#39;nearest&#39;)<\/p>\n<p>plt.colorbar()<\/p>\n<p>plt.savefig(&#39;matrix_nearest.png&#39;)<\/p>\n<p>plt.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406\u7279\u6b8a\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e00\u4e9b\u7279\u6b8a\u77e9\u9635\uff0c\u5982\u7a00\u758f\u77e9\u9635\uff0c\u53ef\u4ee5\u5148\u8f6c\u6362\u4e3a\u5bc6\u96c6\u77e9\u9635\u518d\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.sparse import random<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a00\u758f\u77e9\u9635<\/strong><\/h2>\n<p>sparse_matrix = random(100, 100, density=0.1, format=&#39;coo&#39;)<\/p>\n<p>dense_matrix = sparse_matrix.toarray()<\/p>\n<p>plt.imshow(dense_matrix, cmap=&#39;viridis&#39;)<\/p>\n<p>plt.colorbar()<\/p>\n<p>plt.savefig(&#39;sparse_matrix.png&#39;)<\/p>\n<p>plt.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u77e9\u9635\u4fdd\u5b58\u4e3a\u56fe\u7247\u3002\u65e0\u8bba\u662f\u4f7f\u7528Matplotlib\u8fd8\u662fPillow\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u53c2\u6570\uff0c\u4ee5\u83b7\u5f97\u6700\u4f73\u7684\u56fe\u7247\u6548\u679c\u3002<strong>\u5173\u952e\u5728\u4e8e\u5408\u7406\u9009\u62e9\u989c\u8272\u6620\u5c04\u3001\u63d2\u503c\u65b9\u6cd5\u548c\u683c\u5f0f\uff0c\u4ee5\u786e\u4fdd\u56fe\u7247\u7684\u53ef\u8bfb\u6027\u548c\u7f8e\u89c2\u6027<\/strong>\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u7247\u6587\u4ef6\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u591a\u4e2a\u5e93\u6765\u5b9e\u73b0\u77e9\u9635\u5230\u56fe\u7247\u7684\u8f6c\u6362\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecNumPy\u548cMatplotlib\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5c06\u77e9\u9635\u6570\u636e\u51c6\u5907\u597d\uff0c\u7136\u540e\u4f7f\u7528<code>imshow()<\/code>\u51fd\u6570\u5c06\u5176\u53ef\u89c6\u5316\uff0c\u5e76\u4f7f\u7528<code>savefig()<\/code>\u5c06\u5176\u4fdd\u5b58\u4e3a\u56fe\u7247\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nimport matplotlib.pyplot as plt\n\n# \u521b\u5efa\u4e00\u4e2a\u968f\u673a\u77e9\u9635\nmatrix = np.random.rand(10, 10)\n\n# \u663e\u793a\u77e9\u9635\nplt.imshow(matrix, cmap=&#39;gray&#39;)\nplt.colorbar()\n\n# \u4fdd\u5b58\u4e3a\u56fe\u7247\nplt.savefig(&#39;matrix_image.png&#39;)\n<\/code><\/pre>\n<p><strong>\u9700\u8981\u5b89\u88c5\u54ea\u4e9bPython\u5e93\u624d\u80fd\u5c06\u77e9\u9635\u4fdd\u5b58\u4e3a\u56fe\u7247\uff1f<\/strong><br \/>\u60a8\u9700\u8981\u5b89\u88c5NumPy\u548cMatplotlib\u8fd9\u4e24\u4e2a\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<pre><code class=\"language-bash\">pip install numpy matplotlib\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u6765\u5904\u7406\u6570\u7ec4\u548c\u53ef\u89c6\u5316\u6570\u636e\uff0c\u4f7f\u5f97\u5c06\u77e9\u9635\u8f6c\u6362\u4e3a\u56fe\u7247\u53d8\u5f97\u7b80\u5355\u3002<\/p>\n<p><strong>\u5982\u4f55\u63a7\u5236\u751f\u6210\u56fe\u7247\u7684\u5206\u8fa8\u7387\u548c\u5c3a\u5bf8\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Matplotlib\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>figure()<\/code>\u51fd\u6570\u7684<code>figsize<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u56fe\u7247\u7684\u5c3a\u5bf8\uff0c\u4f7f\u7528<code>dpi<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u5206\u8fa8\u7387\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">plt.figure(figsize=(8, 6), dpi=300)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2a8&#215;6\u82f1\u5bf8\u7684\u56fe\u50cf\uff0c\u5206\u8fa8\u7387\u4e3a300 DPI\u3002\u8fd9\u6837\u7684\u8bbe\u7f6e\u53ef\u4ee5\u5e2e\u52a9\u60a8\u83b7\u5f97\u9ad8\u8d28\u91cf\u7684\u56fe\u7247\u8f93\u51fa\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u5c06\u5f69\u8272\u77e9\u9635\u4fdd\u5b58\u4e3a\u56fe\u7247\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u5982\u679c\u60a8\u7684\u77e9\u9635\u5305\u542bRGB\u503c\uff0c\u53ef\u4ee5\u5c06\u5176\u76f4\u63a5\u4f20\u9012\u7ed9<code>imshow()<\/code>\u51fd\u6570\u3002\u786e\u4fdd\u77e9\u9635\u7684\u5f62\u72b6\u4e3a(height, width, 3)\uff0c\u5176\u4e2d3\u8868\u793a\u989c\u8272\u901a\u9053\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a\u5f69\u8272\u77e9\u9635\ncolor_matrix = np.random.rand(10, 10, 3)\n\n# \u663e\u793a\u5f69\u8272\u77e9\u9635\nplt.imshow(color_matrix)\nplt.savefig(&#39;color_matrix_image.png&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u5c31\u80fd\u591f\u5c06\u5f69\u8272\u77e9\u9635\u6210\u529f\u4fdd\u5b58\u4e3a\u56fe\u7247\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u77e9\u9635\u5199\u4e3a\u56fe\u7247\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528NumPy\u521b\u5efa\u6216\u52a0\u8f7d\u77e9\u9635\u3001\u4f7f\u7528Matplotlib\u6216Pillow [&hellip;]","protected":false},"author":3,"featured_media":1072800,"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\/1072794"}],"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=1072794"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072794\/revisions"}],"predecessor-version":[{"id":1072801,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1072794\/revisions\/1072801"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1072800"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1072794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1072794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1072794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}