{"id":1132415,"date":"2025-01-08T20:57:00","date_gmt":"2025-01-08T12:57:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1132415.html"},"modified":"2025-01-08T20:57:03","modified_gmt":"2025-01-08T12:57:03","slug":"python%e4%b8%adplt%e5%a6%82%e4%bd%95%e7%bb%99%e6%89%80%e7%94%bb%e7%9a%84%e7%ba%bf%e6%94%b9%e9%a2%9c%e8%89%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1132415.html","title":{"rendered":"python\u4e2dplt\u5982\u4f55\u7ed9\u6240\u753b\u7684\u7ebf\u6539\u989c\u8272"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101957\/d2cc52a0-a4d8-49bd-8fcd-c89d4e9ca50e.webp\" alt=\"python\u4e2dplt\u5982\u4f55\u7ed9\u6240\u753b\u7684\u7ebf\u6539\u989c\u8272\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u7684plt\u6a21\u5757\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u6539\u53d8\u7ed8\u5236\u7ebf\u6761\u7684\u989c\u8272\u3002<\/strong> \u901a\u8fc7\u8bbe\u7f6e<code>color<\/code>\u53c2\u6570\u3001\u4f7f\u7528\u989c\u8272\u7f29\u5199\u4ee3\u7801\u3001\u4ee5\u53ca\u901a\u8fc7\u81ea\u5b9a\u4e49\u989c\u8272\u4ee3\u7801\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u66f4\u6539\u7ebf\u6761\u989c\u8272\u3002\u8ba9\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e00\u4e0b\u5982\u4f55\u5728Matplotlib\u4e2d\u8bbe\u7f6e\u7ed8\u56fe\u7684\u989c\u8272\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u989c\u8272\u53c2\u6570\u53ca\u5176\u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><p>Matplotlib\u63d0\u4f9b\u591a\u79cd\u65b9\u5f0f\u6765\u6307\u5b9a\u989c\u8272\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u662f\u76f4\u63a5\u4f7f\u7528<code>color<\/code>\u53c2\u6570\u3002\u5728\u8c03\u7528<code>plt.plot()<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e<code>color<\/code>\u53c2\u6570\u6765\u6539\u53d8\u7ebf\u6761\u7684\u989c\u8272\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>x = [0, 1, 2, 3, 4]<\/p>\n<p>y = [0, 1, 4, 9, 16]<\/p>\n<p>plt.plot(x, y, color=&#39;red&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>color=&#39;red&#39;<\/code>\u53c2\u6570\u5c06\u7ebf\u6761\u989c\u8272\u8bbe\u7f6e\u4e3a\u7ea2\u8272\u3002<\/p>\n<\/p>\n<p><h3>\u989c\u8272\u7f29\u5199\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>Matplotlib\u652f\u6301\u591a\u79cd\u989c\u8272\u7f29\u5199\u4ee3\u7801\uff0c\u8fd9\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u3002\u5e38\u89c1\u7684\u7f29\u5199\u4ee3\u7801\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li>&#39;b&#39;\uff1a\u84dd\u8272 (blue)<\/li>\n<li>&#39;g&#39;\uff1a\u7eff\u8272 (green)<\/li>\n<li>&#39;r&#39;\uff1a\u7ea2\u8272 (red)<\/li>\n<li>&#39;c&#39;\uff1a\u9752\u8272 (cyan)<\/li>\n<li>&#39;m&#39;\uff1a\u54c1\u7ea2\u8272 (magenta)<\/li>\n<li>&#39;y&#39;\uff1a\u9ec4\u8272 (yellow)<\/li>\n<li>&#39;k&#39;\uff1a\u9ed1\u8272 (black)<\/li>\n<li>&#39;w&#39;\uff1a\u767d\u8272 (white)<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.plot(x, y, &#39;g&#39;)  # \u4f7f\u7528\u7eff\u8272<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u81ea\u5b9a\u4e49\u989c\u8272\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u9884\u5b9a\u4e49\u7684\u989c\u8272\u540d\u79f0\u548c\u7f29\u5199\u4ee3\u7801\u5916\uff0cMatplotlib\u8fd8\u652f\u6301\u4f7f\u7528\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801\u548cRGB(A)\u989c\u8272\u503c\u3002\u8fd9\u6837\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u81ea\u5b9a\u4e49\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801<\/p>\n<p>plt.plot(x, y, color=&#39;#FF5733&#39;)  # \u6a59\u8272<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u4f7f\u7528RGBA\u989c\u8272\u503c<\/strong><\/h2>\n<p>plt.plot(x, y, color=(0.1, 0.2, 0.5, 0.8))  # \u534a\u900f\u660e\u84dd\u8272<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u6539\u53d8\u591a\u6761\u7ebf\u7684\u989c\u8272<\/h2>\n<\/p>\n<p><p>\u5728\u4e00\u4e2a\u56fe\u4e2d\u7ed8\u5236\u591a\u6761\u7ebf\u65f6\uff0c\u53ef\u4ee5\u5206\u522b\u4e3a\u6bcf\u6761\u7ebf\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\u3002\u53ef\u4ee5\u901a\u8fc7\u591a\u6b21\u8c03\u7528<code>plt.plot()<\/code>\u51fd\u6570\uff0c\u5e76\u4e3a\u6bcf\u6b21\u8c03\u7528\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">x = [0, 1, 2, 3, 4]<\/p>\n<p>y1 = [0, 1, 4, 9, 16]<\/p>\n<p>y2 = [0, 1, 2, 3, 4]<\/p>\n<p>plt.plot(x, y1, color=&#39;blue&#39;, label=&#39;y1&#39;)<\/p>\n<p>plt.plot(x, y2, color=&#39;green&#39;, label=&#39;y2&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e24\u6b21<code>plt.plot()<\/code>\u51fd\u6570\uff0c\u5206\u522b\u4e3a\u6bcf\u6761\u7ebf\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\uff0c\u5e76\u4f7f\u7528<code>plt.legend()<\/code>\u51fd\u6570\u6dfb\u52a0\u56fe\u4f8b\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u989c\u8272\u6620\u5c04<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u7ed8\u56fe\u9700\u6c42\uff0c\u7279\u522b\u662f\u9700\u8981\u6839\u636e\u6570\u636e\u503c\u52a8\u6001\u6539\u53d8\u989c\u8272\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u989c\u8272\u6620\u5c04\uff08colormap\uff09\u3002Matplotlib\u63d0\u4f9b\u4e86\u591a\u79cd\u5185\u7f6e\u989c\u8272\u6620\u5c04\uff0c\u53ef\u4ee5\u901a\u8fc7<code>cmap<\/code>\u53c2\u6570\u6765\u8bbe\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<p>colors = x  # \u6839\u636ex\u503c\u6539\u53d8\u989c\u8272<\/p>\n<p>plt.scatter(x, y, c=colors, cmap=&#39;viridis&#39;)<\/p>\n<p>plt.colorbar()  # \u663e\u793a\u989c\u8272\u6761<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86<code>plt.scatter()<\/code>\u51fd\u6570\uff0c\u5e76\u901a\u8fc7<code>c<\/code>\u53c2\u6570\u548c<code>cmap<\/code>\u53c2\u6570\u6839\u636e<code>x<\/code>\u503c\u52a8\u6001\u6539\u53d8\u70b9\u7684\u989c\u8272\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u81ea\u5b9a\u4e49\u989c\u8272\u6620\u5c04<\/h2>\n<\/p>\n<p><p>\u5982\u679c\u5185\u7f6e\u7684\u989c\u8272\u6620\u5c04\u4e0d\u80fd\u6ee1\u8db3\u9700\u6c42\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49\u989c\u8272\u6620\u5c04\u3002\u81ea\u5b9a\u4e49\u989c\u8272\u6620\u5c04\u9700\u8981\u4f7f\u7528Matplotlib\u7684<code>LinearSegmentedColormap<\/code>\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from matplotlib.colors import LinearSegmentedColormap<\/p>\n<h2><strong>\u5b9a\u4e49\u81ea\u5b9a\u4e49\u989c\u8272\u6620\u5c04<\/strong><\/h2>\n<p>colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]  # \u7ea2 -&gt; \u7eff -&gt; \u84dd<\/p>\n<p>n_bins = 100  # \u989c\u8272\u6620\u5c04\u7684\u5206\u5272\u6570<\/p>\n<p>cmap_name = &#39;my_cmap&#39;<\/p>\n<p>custom_cmap = LinearSegmentedColormap.from_list(cmap_name, colors, N=n_bins)<\/p>\n<h2><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u989c\u8272\u6620\u5c04<\/strong><\/h2>\n<p>plt.scatter(x, y, c=colors, cmap=custom_cmap)<\/p>\n<p>plt.colorbar()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u989c\u8272\u6620\u5c04\uff0c\u5e76\u5728\u7ed8\u56fe\u65f6\u4f7f\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u7684Matplotlib\u5e93\u4e2d\u7075\u6d3b\u5730\u6539\u53d8\u7ebf\u6761\u548c\u70b9\u7684\u989c\u8272\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u989c\u8272\u53c2\u6570\u8bbe\u7f6e\u3001\u4f7f\u7528\u989c\u8272\u7f29\u5199\u4ee3\u7801\u3001\u5341\u516d\u8fdb\u5236\u989c\u8272\u4ee3\u7801\uff0c\u8fd8\u662f\u66f4\u590d\u6742\u7684\u989c\u8272\u6620\u5c04\u548c\u81ea\u5b9a\u4e49\u989c\u8272\u6620\u5c04\uff0cMatplotlib\u90fd\u80fd\u6ee1\u8db3\u5404\u79cd\u9700\u6c42\u3002\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u521b\u5efa\u66f4\u5177\u89c6\u89c9\u5438\u5f15\u529b\u548c\u6570\u636e\u8868\u8fbe\u529b\u7684\u56fe\u8868\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u989c\u8272\u548c\u989c\u8272\u6620\u5c04\u4e0d\u4ec5\u80fd\u589e\u5f3a\u56fe\u8868\u7684\u7f8e\u89c2\u6027\uff0c\u8fd8\u80fd\u63d0\u9ad8\u6570\u636e\u7684\u53ef\u8bfb\u6027\u548c\u6613\u4e8e\u7406\u89e3\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u638c\u63e1Matplotlib\u4e2d\u7684\u989c\u8272\u8bbe\u7f6e\u6280\u5de7\uff0c\u5e76\u5728\u60a8\u7684\u6570\u636e\u53ef\u89c6\u5316\u5de5\u4f5c\u4e2d\u53d1\u6325\u4f5c\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u7684Matplotlib\u4e2d\u9009\u62e9\u4e0d\u540c\u7684\u989c\u8272\u7ed8\u5236\u7ebf\u6761\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u7ed8\u5236\u7ebf\u6761\u65f6\u6307\u5b9a\u989c\u8272\u53c2\u6570\u6765\u66f4\u6539\u7ebf\u6761\u7684\u989c\u8272\u3002\u53ef\u4ee5\u4f7f\u7528\u989c\u8272\u540d\u79f0\uff08\u5982&#39;red&#39;\u6216&#39;blue&#39;\uff09\uff0c\u5341\u516d\u8fdb\u5236\u4ee3\u7801\uff08\u5982&#39;#FF5733&#39;\uff09\u6216\u8005RGB\u5143\u7ec4\uff08\u5982(1, 0, 0)\u8868\u793a\u7ea2\u8272\uff09\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>plt.plot(x, y, color=&#39;green&#39;)<\/code>\u53ef\u4ee5\u5c06\u7ebf\u6761\u989c\u8272\u8bbe\u7f6e\u4e3a\u7eff\u8272\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u989c\u8272\u5417\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01Matplotlib\u652f\u6301\u591a\u79cd\u989c\u8272\u8868\u793a\u65b9\u5f0f\uff0c\u5305\u62ecRGBA\u683c\u5f0f\u3002\u5728RGBA\u4e2d\uff0c\u6700\u540e\u4e00\u4e2a\u503c\u8868\u793a\u900f\u660e\u5ea6\u3002\u4f8b\u5982\uff0c<code>plt.plot(x, y, color=(1, 0, 0, 0.5))<\/code>\u5c06\u7ed8\u5236\u4e00\u4e2a\u534a\u900f\u660e\u7684\u7ea2\u8272\u7ebf\u6761\u3002<\/p>\n<p><strong>\u5982\u4f55\u4e3a\u591a\u4e2a\u7ebf\u6761\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\uff1f<\/strong><br \/>\u5728\u7ed8\u5236\u591a\u4e2a\u7ebf\u6761\u65f6\uff0c\u53ef\u4ee5\u4e3a\u6bcf\u6761\u7ebf\u5206\u522b\u6307\u5b9a\u989c\u8272\u3002\u53ef\u4ee5\u5728<code>plt.plot()<\/code>\u51fd\u6570\u4e2d\u591a\u6b21\u8c03\u7528\u5e76\u4e3a\u6bcf\u6b21\u8c03\u7528\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\u3002\u4f8b\u5982\uff0c<code>plt.plot(x1, y1, color=&#39;blue&#39;)<\/code>\u548c<code>plt.plot(x2, y2, color=&#39;orange&#39;)<\/code>\u5c06\u5206\u522b\u7ed8\u5236\u84dd\u8272\u548c\u6a59\u8272\u7684\u7ebf\u6761\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u56fe\u4f8b\u4e2d\u663e\u793a\u4e0d\u540c\u989c\u8272\u7ebf\u6761\u7684\u542b\u4e49\uff1f<\/strong><br \/>\u5728Matplotlib\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>plt.plot()<\/code>\u65f6\u7684\u6807\u7b7e\u53c2\u6570\u6765\u6dfb\u52a0\u56fe\u4f8b\u3002\u8c03\u7528<code>plt.legend()<\/code>\u53ef\u4ee5\u663e\u793a\u56fe\u4f8b\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">plt.plot(x1, y1, color=&#39;blue&#39;, label=&#39;Line 1&#39;)\nplt.plot(x2, y2, color=&#39;orange&#39;, label=&#39;Line 2&#39;)\nplt.legend()\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u56fe\u4f8b\u4e2d\u4f1a\u663e\u793a\u76f8\u5e94\u989c\u8272\u7684\u7ebf\u6761\u548c\u5b83\u4eec\u7684\u6807\u7b7e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528Matplotlib\u5e93\u7684plt\u6a21\u5757\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u6539\u53d8\u7ed8\u5236\u7ebf\u6761\u7684\u989c\u8272\u3002 \u901a\u8fc7\u8bbe\u7f6ecolor [&hellip;]","protected":false},"author":3,"featured_media":1132424,"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\/1132415"}],"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=1132415"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132415\/revisions"}],"predecessor-version":[{"id":1132426,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1132415\/revisions\/1132426"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1132424"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1132415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1132415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1132415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}