{"id":1035319,"date":"2024-12-31T11:57:47","date_gmt":"2024-12-31T03:57:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1035319.html"},"modified":"2024-12-31T11:57:49","modified_gmt":"2024-12-31T03:57:49","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e5%87%ba%e7%9f%a9%e9%98%b5%e7%82%b9%e5%9d%90%e6%a0%87%e7%b3%bb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1035319.html","title":{"rendered":"python\u5982\u4f55\u753b\u51fa\u77e9\u9635\u70b9\u5750\u6807\u7cfb"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/94f88548-d9f0-4b87-8bfb-df2129ae724c.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u753b\u51fa\u77e9\u9635\u70b9\u5750\u6807\u7cfb\" \/><\/p>\n<p><p> <strong>Python \u753b\u51fa\u77e9\u9635\u70b9\u5750\u6807\u7cfb<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u7ed8\u5236\u77e9\u9635\u70b9\u5750\u6807\u7cfb\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u5e93\u662fMatplotlib\u3002<strong>\u4f7f\u7528Matplotlib\u7ed8\u5236\u77e9\u9635\u70b9\u5750\u6807\u7cfb\u3001\u5229\u7528scatter\u51fd\u6570\u7ed8\u5236\u70b9\u3001\u8bbe\u7f6e\u5750\u6807\u8f74\u548c\u7f51\u683c\u3001\u4f7f\u7528\u4e0d\u540c\u989c\u8272\u548c\u5f62\u72b6\u7684\u6807\u8bb0\u533a\u5206\u4e0d\u540c\u7684\u70b9<\/strong>\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u8be6\u7ec6\u4f7f\u7528Matplotlib\u5e93\u6765\u7ed8\u5236\u77e9\u9635\u70b9\u5750\u6807\u7cfb\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efa\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u7ed8\u5236\u77e9\u9635\u70b9\u5750\u6807\u7cfb\u4e4b\u524d\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e9b\u6570\u636e\u3002\u8fd9\u4e9b\u6570\u636e\u53ef\u4ee5\u662f\u968f\u673a\u7684\uff0c\u4e5f\u53ef\u4ee5\u662f\u7279\u5b9a\u7684\u77e9\u9635\u6570\u636e\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u751f\u6210\u4e00\u4e9b\u968f\u673a\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u968f\u673a\u77e9\u9635\u6570\u636e<\/p>\n<p>matrix = np.random.rand(10, 10)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed8\u5236\u77e9\u9635\u70b9\u5750\u6807\u7cfb<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528scatter\u51fd\u6570\u7ed8\u5236\u70b9<\/h4>\n<\/p>\n<p><p>Matplotlib\u7684scatter\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u7ed8\u5236\u6563\u70b9\u56fe\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528scatter\u51fd\u6570\u7ed8\u5236\u77e9\u9635\u70b9\u5750\u6807\u7cfb\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u77e9\u9635\u7684\u884c\u5217\u6570<\/p>\n<p>rows, cols = matrix.shape<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<h2><strong>\u4f7f\u7528scatter\u51fd\u6570\u7ed8\u5236\u77e9\u9635\u70b9\u5750\u6807\u7cfb<\/strong><\/h2>\n<p>for i in range(rows):<\/p>\n<p>    for j in range(cols):<\/p>\n<p>        plt.scatter(j, i, c=&#39;blue&#39;, s=matrix[i, j]*100)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u77e9\u9635\u7684\u6bcf\u4e00\u884c\u548c\u6bcf\u4e00\u5217\uff0c\u5e76\u4f7f\u7528scatter\u51fd\u6570\u7ed8\u5236\u6bcf\u4e2a\u70b9\u3002<strong>\u989c\u8272\uff08c\u53c2\u6570\uff09\u548c\u5927\u5c0f\uff08s\u53c2\u6570\uff09\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u8bbe\u7f6e<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u8bbe\u7f6e\u5750\u6807\u8f74\u548c\u7f51\u683c<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u56fe\u5f62\u66f4\u52a0\u6e05\u6670\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u5750\u6807\u8f74\u548c\u7f51\u683c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4<\/p>\n<p>plt.xlim(-1, cols)<\/p>\n<p>plt.ylim(-1, rows)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u6807\u7b7e<\/strong><\/h2>\n<p>plt.xlabel(&#39;Column Index&#39;)<\/p>\n<p>plt.ylabel(&#39;Row Index&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7f51\u683c<\/strong><\/h2>\n<p>plt.grid(True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528\u4e0d\u540c\u989c\u8272\u548c\u5f62\u72b6\u7684\u6807\u8bb0\u533a\u5206\u4e0d\u540c\u7684\u70b9<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u533a\u5206\u4e0d\u540c\u7c7b\u578b\u7684\u70b9\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u989c\u8272\u548c\u5f62\u72b6\u7684\u6807\u8bb0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u4e0d\u540c\u989c\u8272\u548c\u5f62\u72b6\u7684\u6807\u8bb0\u533a\u5206\u4e0d\u540c\u7684\u70b9<\/p>\n<p>for i in range(rows):<\/p>\n<p>    for j in range(cols):<\/p>\n<p>        if matrix[i, j] &gt; 0.5:<\/p>\n<p>            plt.scatter(j, i, c=&#39;red&#39;, marker=&#39;o&#39;, s=matrix[i, j]*100)<\/p>\n<p>        else:<\/p>\n<p>            plt.scatter(j, i, c=&#39;blue&#39;, marker=&#39;x&#39;, s=matrix[i, j]*100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4fdd\u5b58\u56fe\u5f62<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u4fdd\u5b58\u56fe\u5f62\uff0c\u53ef\u4ee5\u4f7f\u7528savefig\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u56fe\u5f62<\/p>\n<p>plt.savefig(&#39;matrix_plot.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u5305\u62ec\u6240\u6709\u6b65\u9aa4\uff1a<\/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\u968f\u673a\u77e9\u9635\u6570\u636e<\/strong><\/h2>\n<p>matrix = np.random.rand(10, 10)<\/p>\n<h2><strong>\u83b7\u53d6\u77e9\u9635\u7684\u884c\u5217\u6570<\/strong><\/h2>\n<p>rows, cols = matrix.shape<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u5f62<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<h2><strong>\u4f7f\u7528scatter\u51fd\u6570\u7ed8\u5236\u77e9\u9635\u70b9\u5750\u6807\u7cfb<\/strong><\/h2>\n<p>for i in range(rows):<\/p>\n<p>    for j in range(cols):<\/p>\n<p>        if matrix[i, j] &gt; 0.5:<\/p>\n<p>            plt.scatter(j, i, c=&#39;red&#39;, marker=&#39;o&#39;, s=matrix[i, j]*100)<\/p>\n<p>        else:<\/p>\n<p>            plt.scatter(j, i, c=&#39;blue&#39;, marker=&#39;x&#39;, s=matrix[i, j]*100)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u8303\u56f4<\/strong><\/h2>\n<p>plt.xlim(-1, cols)<\/p>\n<p>plt.ylim(-1, rows)<\/p>\n<h2><strong>\u8bbe\u7f6e\u5750\u6807\u8f74\u6807\u7b7e<\/strong><\/h2>\n<p>plt.xlabel(&#39;Column Index&#39;)<\/p>\n<p>plt.ylabel(&#39;Row Index&#39;)<\/p>\n<h2><strong>\u8bbe\u7f6e\u7f51\u683c<\/strong><\/h2>\n<p>plt.grid(True)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u5f62<\/strong><\/h2>\n<p>plt.show()<\/p>\n<h2><strong>\u4fdd\u5b58\u56fe\u5f62<\/strong><\/h2>\n<p>plt.savefig(&#39;matrix_plot.png&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236\u77e9\u9635\u70b9\u5750\u6807\u7cfb\u3002<strong>\u4f7f\u7528scatter\u51fd\u6570\u7ed8\u5236\u70b9\u3001\u8bbe\u7f6e\u5750\u6807\u8f74\u548c\u7f51\u683c\u3001\u4f7f\u7528\u4e0d\u540c\u989c\u8272\u548c\u5f62\u72b6\u7684\u6807\u8bb0\u533a\u5206\u4e0d\u540c\u7684\u70b9<\/strong>\uff0c\u4f7f\u5f97\u56fe\u5f62\u66f4\u52a0\u76f4\u89c2\u548c\u7f8e\u89c2\u3002\u5e0c\u671b\u8fd9\u4e9b\u6b65\u9aa4\u548c\u793a\u4f8b\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\u70b9\u5750\u6807\u7cfb\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u6765\u7ed8\u5236\u77e9\u9635\u70b9\u5750\u6807\u7cfb\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7<code>pip install matplotlib<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>plt.scatter()<\/code>\u51fd\u6570\u7ed8\u5236\u70b9\uff0c\u5e76\u901a\u8fc7<code>plt.grid()<\/code>\u6dfb\u52a0\u7f51\u683c\u7ebf\u4ee5\u5f62\u6210\u5750\u6807\u7cfb\u3002\u5b8c\u6574\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport numpy as np\n\n# \u521b\u5efa\u77e9\u9635\u5750\u6807\nx = np.array([1, 2, 3, 4, 5])\ny = np.array([1, 4, 9, 16, 25])\n\nplt.scatter(x, y)\nplt.title(&#39;Matrix Point Coordinate System&#39;)\nplt.xlabel(&#39;X-axis&#39;)\nplt.ylabel(&#39;Y-axis&#39;)\nplt.grid(True)\nplt.show()\n<\/code><\/pre>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u5176\u4ed6\u5e93\u6765\u7ed8\u5236\u77e9\u9635\u70b9\u5750\u6807\u7cfb\uff1f<\/strong><br \/>\u9664\u4e86Matplotlib\uff0cSeaborn\u548cPlotly\u4e5f\u662f\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u3002Seaborn\u5728\u6570\u636e\u53ef\u89c6\u5316\u65b9\u9762\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\uff0c\u800cPlotly\u5219\u652f\u6301\u4ea4\u4e92\u5f0f\u56fe\u5f62\u3002\u4f7f\u7528\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u4f7f\u5f97\u7ed8\u56fe\u66f4\u5177\u7f8e\u89c2\u6027\u548c\u4e92\u52a8\u6027\uff0c\u9002\u5408\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6237\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49\u77e9\u9635\u70b9\u5750\u6807\u7cfb\u7684\u5916\u89c2\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u81ea\u5b9a\u4e49\u70b9\u7684\u989c\u8272\u3001\u5927\u5c0f\u548c\u5f62\u72b6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>c<\/code>\u53c2\u6570\u53ef\u4ee5\u8bbe\u7f6e\u989c\u8272\uff0c<code>s<\/code>\u53c2\u6570\u53ef\u4ee5\u8bbe\u7f6e\u70b9\u7684\u5927\u5c0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">plt.scatter(x, y, c=&#39;red&#39;, s=100, marker=&#39;o&#39;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u4e0d\u540c\u7684\u53c2\u6570\u914d\u7f6e\uff0c\u53ef\u4ee5\u521b\u5efa\u51fa\u7b26\u5408\u4e2a\u6027\u5316\u9700\u6c42\u7684\u5750\u6807\u7cfb\u5916\u89c2\uff0c\u4f7f\u5f97\u6570\u636e\u5c55\u793a\u66f4\u5177\u5438\u5f15\u529b\u3002<\/p>\n<p><strong>\u5728\u7ed8\u5236\u77e9\u9635\u70b9\u5750\u6807\u7cfb\u65f6\uff0c\u5982\u4f55\u6807\u8bb0\u7279\u5b9a\u7684\u6570\u636e\u70b9\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528<code>plt.annotate()<\/code>\u51fd\u6570\u4e3a\u7279\u5b9a\u6570\u636e\u70b9\u6dfb\u52a0\u6807\u8bb0\u3002\u901a\u8fc7\u8bbe\u7f6e\u6587\u672c\u3001\u4f4d\u7f6e\u548c\u7bad\u5934\u7b49\u53c2\u6570\uff0c\u53ef\u4ee5\u6e05\u6670\u5730\u6307\u51fa\u7279\u5b9a\u6570\u636e\u70b9\u7684\u542b\u4e49\u6216\u7279\u5f81\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">plt.annotate(&#39;Point (3,9)&#39;, xy=(3, 9), xytext=(4, 15),\n             arrowprops=dict(facecolor=&#39;black&#39;, shrink=0.05))\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u6807\u8bb0\u65b9\u5f0f\u80fd\u591f\u5e2e\u52a9\u89c2\u4f17\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u7684\u5206\u5e03\u53ca\u5176\u542b\u4e49\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python \u753b\u51fa\u77e9\u9635\u70b9\u5750\u6807\u7cfb \u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u7ed8\u5236\u77e9\u9635\u70b9\u5750\u6807\u7cfb\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u5e93\u662fMatpl [&hellip;]","protected":false},"author":3,"featured_media":1035328,"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\/1035319"}],"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=1035319"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035319\/revisions"}],"predecessor-version":[{"id":1035329,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1035319\/revisions\/1035329"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1035328"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1035319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1035319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1035319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}