{"id":1082124,"date":"2025-01-08T12:45:56","date_gmt":"2025-01-08T04:45:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1082124.html"},"modified":"2025-01-08T12:45:59","modified_gmt":"2025-01-08T04:45:59","slug":"python%e5%a6%82%e4%bd%95%e5%8f%aa%e7%bb%98%e5%88%b6%e7%82%b9%e4%b8%8d%e7%bb%98%e5%88%b6%e7%ba%bf-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1082124.html","title":{"rendered":"python\u5982\u4f55\u53ea\u7ed8\u5236\u70b9\u4e0d\u7ed8\u5236\u7ebf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183811\/47804ff3-12cd-4046-80c6-06125fe1b07e.webp\" alt=\"python\u5982\u4f55\u53ea\u7ed8\u5236\u70b9\u4e0d\u7ed8\u5236\u7ebf\" \/><\/p>\n<p><p> <strong>Python\u7ed8\u5236\u70b9\u800c\u4e0d\u7ed8\u5236\u7ebf\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528matplotlib\u5e93\u3001\u4f7f\u7528scatter\u51fd\u6570\u3001\u8bbe\u7f6e\u7ed8\u56fe\u5c5e\u6027<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528scatter\u51fd\u6570\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u65f6\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u7ed8\u5236\u6570\u636e\u70b9\u800c\u4e0d\u662f\u8fde\u63a5\u8fd9\u4e9b\u70b9\u7684\u7ebf\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5f3a\u5927\u7684matplotlib\u5e93\uff0c\u7279\u522b\u662f\u5176\u4e2d\u7684scatter\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u76f4\u89c2\uff0c\u800c\u4e14\u6781\u5176\u7075\u6d3b\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u6570\u636e\u53ef\u89c6\u5316\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528matplotlib\u5e93\u7ed8\u5236\u70b9\u800c\u4e0d\u7ed8\u5236\u7ebf\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u5b89\u88c5\u548c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/strong><\/h2>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5matplotlib\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\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\uff0c\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><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528scatter\u51fd\u6570\u7ed8\u5236\u70b9<\/strong><\/h2>\n<p><p>matplotlib\u5e93\u4e2d\u7684scatter\u51fd\u6570\u4e13\u95e8\u7528\u4e8e\u7ed8\u5236\u70b9\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<h2><strong>\u7ed8\u5236\u70b9<\/strong><\/h2>\n<p>plt.scatter(x, y)<\/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\uff0cx\u548cy\u662f\u6570\u636e\u70b9\u7684\u5750\u6807\u3002\u4f7f\u7528plt.scatter()\u51fd\u6570\u7ed8\u5236\u8fd9\u4e9b\u70b9\uff0cplt.show()\u51fd\u6570\u663e\u793a\u56fe\u5f62\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u8bbe\u7f6e\u7ed8\u56fe\u5c5e\u6027<\/strong><\/h2>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6escatter\u51fd\u6570\u7684\u53c2\u6570\u6765\u66f4\u6539\u70b9\u7684\u989c\u8272\u3001\u5927\u5c0f\u548c\u5f62\u72b6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<h2><strong>\u7ed8\u5236\u70b9\uff0c\u8bbe\u7f6e\u989c\u8272\u3001\u5927\u5c0f\u548c\u5f62\u72b6<\/strong><\/h2>\n<p>plt.scatter(x, y, c=&#39;red&#39;, s=100, marker=&#39;o&#39;)<\/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\u53c2\u6570c\u8bbe\u7f6e\u70b9\u7684\u989c\u8272\uff0cs\u8bbe\u7f6e\u70b9\u7684\u5927\u5c0f\uff0cmarker\u8bbe\u7f6e\u70b9\u7684\u5f62\u72b6\u3002\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u8fd9\u4e9b\u53c2\u6570\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u7ed3\u5408\u5176\u4ed6\u56fe\u8868\u7c7b\u578b<\/strong><\/h2>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u5728\u540c\u4e00\u56fe\u8868\u4e2d\u7ed3\u5408\u5176\u4ed6\u56fe\u8868\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u7ed8\u5236\u70b9\u548c\u7ebf\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<h2><strong>\u7ed8\u5236\u7ebf<\/strong><\/h2>\n<p>plt.plot(x, y, linestyle=&#39;--&#39;, color=&#39;blue&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u70b9<\/strong><\/h2>\n<p>plt.scatter(x, y, c=&#39;red&#39;, s=100, marker=&#39;o&#39;)<\/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\uff0cplt.plot()\u51fd\u6570\u7528\u4e8e\u7ed8\u5236\u7ebf\uff0cplt.scatter()\u51fd\u6570\u7528\u4e8e\u7ed8\u5236\u70b9\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e0d\u540c\u7684\u7ebf\u578b\u548c\u989c\u8272\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e30\u5bcc\u7684\u56fe\u8868\u7ec4\u5408\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u57283D\u56fe\u4e2d\u7ed8\u5236\u70b9<\/strong><\/h2>\n<p><p>matplotlib\u5e93\u4e5f\u652f\u6301\u7ed8\u52363D\u56fe\u3002\u53ef\u4ee5\u4f7f\u7528mpl_toolkits.mplot3d\u5e93\u7684scatter\u51fd\u6570\u7ed8\u52363D\u70b9\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>from mpl_toolkits.mplot3d import Axes3D<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<p>z = [1, 4, 9, 16, 25]<\/p>\n<h2><strong>\u521b\u5efa3D\u56fe\u5f62<\/strong><\/h2>\n<p>fig = plt.figure()<\/p>\n<p>ax = fig.add_subplot(111, projection=&#39;3d&#39;)<\/p>\n<h2><strong>\u7ed8\u52363D\u70b9<\/strong><\/h2>\n<p>ax.scatter(x, y, z, c=&#39;red&#39;, s=100, marker=&#39;o&#39;)<\/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\u4f7f\u7528mpl_toolkits.mplot3d\u5e93\u521b\u5efa3D\u56fe\u5f62\uff0c\u4f7f\u7528scatter\u51fd\u6570\u7ed8\u52363D\u70b9\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u53c2\u6570\u8c03\u6574\u70b9\u7684\u989c\u8272\u3001\u5927\u5c0f\u548c\u5f62\u72b6\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u7ed3\u5408\u5176\u4ed6\u5e93<\/strong><\/h2>\n<p><p>\u9664\u4e86matplotlib\uff0c\u8fd8\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6\u5e93\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u6570\u636e\u53ef\u89c6\u5316\u3002\u4f8b\u5982\uff0c\u4f7f\u7528seaborn\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import seaborn as sns<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 3, 5, 7, 11]<\/p>\n<h2><strong>\u4f7f\u7528seaborn\u7ed8\u5236\u70b9<\/strong><\/h2>\n<p>sns.scatterplot(x=x, y=y, color=&#39;red&#39;, s=100, marker=&#39;o&#39;)<\/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\u4f7f\u7528seaborn\u5e93\u7684scatterplot\u51fd\u6570\u7ed8\u5236\u70b9\u3002seaborn\u5e93\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u7ed8\u56fe\u529f\u80fd\u548c\u66f4\u7f8e\u89c2\u7684\u9ed8\u8ba4\u6837\u5f0f\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Python\u4e2d\u7684matplotlib\u5e93\u7ed8\u5236\u70b9\u800c\u4e0d\u7ed8\u5236\u7ebf\u7684\u65b9\u6cd5\u3002\u5305\u62ec\u4f7f\u7528scatter\u51fd\u6570\u3001\u8bbe\u7f6e\u7ed8\u56fe\u5c5e\u6027\u3001\u7ed3\u5408\u5176\u4ed6\u56fe\u8868\u7c7b\u578b\u3001\u57283D\u56fe\u4e2d\u7ed8\u5236\u70b9\u548c\u7ed3\u5408\u5176\u4ed6\u5e93\u7b49\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u5730\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><p>\u7ed8\u5236\u70b9\u800c\u4e0d\u7ed8\u5236\u7ebf\u7684\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5355\u76f4\u89c2\uff0c\u800c\u4e14\u7075\u6d3b\u591a\u6837\u3002\u901a\u8fc7\u8c03\u6574\u53c2\u6570\u548c\u7ed3\u5408\u5176\u4ed6\u56fe\u8868\u7c7b\u578b\uff0c\u53ef\u4ee5\u5b9e\u73b0\u4e30\u5bcc\u7684\u56fe\u8868\u7ec4\u5408\uff0c\u6ee1\u8db3\u5404\u79cd\u6570\u636e\u53ef\u89c6\u5316\u9700\u6c42\u3002\u5e0c\u671b\u8bfb\u8005\u80fd\u591f\u6839\u636e\u9700\u8981\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u63d0\u5347\u6570\u636e\u53ef\u89c6\u5316\u7684\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528Matplotlib\u4ec5\u7ed8\u5236\u6563\u70b9\u56fe\u800c\u4e0d\u8fde\u63a5\u7ebf\u6761\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7scatter\u51fd\u6570\u6765\u7ed8\u5236\u6563\u70b9\u56fe\u3002\u53ea\u9700\u5c06\u6570\u636e\u4f20\u9012\u7ed9scatter\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u4f7f\u7528plot\u51fd\u6570\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u53ea\u663e\u793a\u70b9\u800c\u4e0d\u8fde\u63a5\u7ebf\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\nx = [1, 2, 3, 4, 5]\ny = [2, 3, 5, 1, 4]\n\nplt.scatter(x, y)  # \u4f7f\u7528scatter\u51fd\u6570\u7ed8\u5236\u70b9\nplt.show()\n<\/code><\/pre>\n<p><strong>\u5728\u7ed8\u5236\u6563\u70b9\u56fe\u65f6\uff0c\u5982\u4f55\u81ea\u5b9a\u4e49\u70b9\u7684\u989c\u8272\u548c\u5927\u5c0f\uff1f<\/strong><br \/>\u5728scatter\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7c\u53c2\u6570\u6307\u5b9a\u70b9\u7684\u989c\u8272\uff0c\u901a\u8fc7s\u53c2\u6570\u8c03\u6574\u70b9\u7684\u5927\u5c0f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">plt.scatter(x, y, c=&#39;red&#39;, s=100)  # \u7ea2\u8272\u70b9\uff0c\u5927\u5c0f\u4e3a100\nplt.show()\n<\/code><\/pre>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u989c\u8272\u6620\u5c04\u548c\u6570\u7ec4\u6765\u52a8\u6001\u8bbe\u7f6e\u70b9\u7684\u989c\u8272\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u6765\u7ed8\u5236\u4ec5\u663e\u793a\u70b9\u7684\u56fe\u5f62\uff1f<\/strong><br \/>\u9664\u4e86Matplotlib\uff0cSeaborn\u548cPlotly\u7b49\u5e93\u4e5f\u53ef\u4ee5\u7ed8\u5236\u6563\u70b9\u56fe\u3002Seaborn\u63d0\u4f9b\u4e86\u66f4\u9ad8\u5c42\u6b21\u7684\u63a5\u53e3\uff0c\u9002\u5408\u8fdb\u884c\u7edf\u8ba1\u6570\u636e\u53ef\u89c6\u5316\uff0c\u800cPlotly\u652f\u6301\u4ea4\u4e92\u5f0f\u56fe\u5f62\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Seaborn\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import seaborn as sns\n\nsns.scatterplot(x=x, y=y)\nplt.show()\n<\/code><\/pre>\n<p>\u9009\u62e9\u9002\u5408\u81ea\u5df1\u9700\u6c42\u7684\u5e93\uff0c\u53ef\u4ee5\u5e2e\u52a9\u66f4\u597d\u5730\u5c55\u793a\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7ed8\u5236\u70b9\u800c\u4e0d\u7ed8\u5236\u7ebf\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528matplotlib\u5e93\u3001\u4f7f\u7528scatter\u51fd\u6570\u3001\u8bbe\u7f6e\u7ed8\u56fe\u5c5e\u6027\u3002\u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1082131,"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\/1082124"}],"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=1082124"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082124\/revisions"}],"predecessor-version":[{"id":1082133,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1082124\/revisions\/1082133"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1082131"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1082124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1082124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1082124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}