{"id":8365,"date":"2021-02-07T14:46:24","date_gmt":"2021-02-07T09:16:24","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=8365"},"modified":"2026-07-13T12:31:49","modified_gmt":"2026-07-13T07:01:49","slug":"matplotlib-colorbar","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/","title":{"rendered":"Matplotlib Colorbar: cmap, norm, Labels, and Layout"},"content":{"rendered":"<p><strong>Quick answer:<\/strong> Create a Matplotlib colorbar from the plot&#8217;s ScalarMappable with fig.colorbar(mappable, ax=ax). The mappable&#8217;s colormap and norm define the data-to-color relationship; use a shared norm when plots must be comparable.<\/p>\n<figure class=\"pythonpool-article-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/matplotlib-colorbar-scale.png\" alt=\"Matplotlib colorbar diagram showing ScalarMappable, colormap and norm, shared scales, location, shrink, aspect, and labels\" width=\"1536\" height=\"1024\" loading=\"lazy\" decoding=\"async\"><figcaption>A colorbar explains the mappable&#8217;s data-to-color scale; share the norm when plots must compare.<\/figcaption><\/figure>\n<p>A Matplotlib colorbar explains how colors map to numeric values. It is essential for heatmaps, images, contour plots, pseudocolor meshes, scatter plots with colored points, and any chart where color carries data instead of decoration.<\/p>\n<p>The usual pattern is to create a mappable object first, such as the result from <code>imshow()<\/code>, <code>scatter()<\/code>, <code>contourf()<\/code>, or <code>pcolormesh()<\/code>. Then pass that object to <code>fig.colorbar()<\/code> or <code>plt.colorbar()<\/code>. The colorbar reads the colormap and numeric normalization from that mappable object.<\/p>\n<p>The official Matplotlib references for <a href=\"https:\/\/matplotlib.org\/stable\/api\/_as_gen\/matplotlib.figure.Figure.colorbar.html\">Figure.colorbar()<\/a>, <a href=\"https:\/\/matplotlib.org\/stable\/api\/_as_gen\/matplotlib.pyplot.colorbar.html\">pyplot.colorbar()<\/a>, <a href=\"https:\/\/matplotlib.org\/stable\/users\/explain\/colors\/colormaps.html\">choosing colormaps<\/a>, and <a href=\"https:\/\/matplotlib.org\/stable\/users\/explain\/colors\/colormapnorms.html\">colormap normalization<\/a> explain the main controls.<\/p>\n<p>Use the figure-oriented API when possible. A colorbar takes space from one or more axes, so tying it to the figure and the intended axes makes layout behavior easier to control. This matters when a figure has multiple subplots, a shared color scale, or a horizontal colorbar below the chart.<\/p>\n<p>A good colorbar needs a label, sensible ticks, and a scale that matches the data. Do not add a colorbar only because a plotting function supports it. Add it when the viewer needs to translate colors back into values or categories.<\/p>\n<p>The examples below close figures after creating them. That keeps scripts, tests, and documentation examples from leaving open windows or accumulating figures in memory.<\/p>\n<p>Before saving a final figure, check that colorbar labels and tick text are not clipped by the canvas edge.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#Add_A_Colorbar_To_imshow\" >Add A Colorbar To imshow<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#Control_Ticks_And_Labels\" >Control Ticks And Labels<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#Use_A_Horizontal_Colorbar\" >Use A Horizontal Colorbar<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#Add_A_Colorbar_To_Scatter\" >Add A Colorbar To Scatter<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#Share_One_Colorbar_Across_Subplots\" >Share One Colorbar Across Subplots<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#Use_Colorbars_With_Contour_Plots\" >Use Colorbars With Contour Plots<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#Connect_The_Colorbar_To_The_Mappable\" >Connect The Colorbar To The Mappable<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#Keep_The_Scale_Honest\" >Keep The Scale Honest<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#Control_Layout_Deliberately\" >Control Layout Deliberately<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#How_do_I_add_a_colorbar_in_Matplotlib\" >How do I add a colorbar in Matplotlib?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#What_do_cmap_and_norm_do\" >What do cmap and norm do?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#How_do_I_make_colorbars_comparable_across_plots\" >How do I make colorbars comparable across plots?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#How_do_I_control_colorbar_size_and_placement\" >How do I control colorbar size and placement?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Add_A_Colorbar_To_imshow\"><\/span>Add A Colorbar To imshow<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>imshow()<\/code> returns an image object. Pass that object to <code>fig.colorbar()<\/code> and attach the colorbar to the same axes.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">try:\n    import numpy as np\n    import matplotlib.pyplot as plt\nexcept ModuleNotFoundError:\n    print(\"Install numpy and matplotlib to run this example.\")\nelse:\n    data = np.arange(16).reshape(4, 4)\n\n    fig, ax = plt.subplots()\n    image = ax.imshow(data, cmap=\"viridis\")\n    colorbar = fig.colorbar(image, ax=ax)\n    colorbar.set_label(\"value\")\n    ax.set_title(\"imshow colorbar\")\n    plt.close(fig)\n<\/code><\/pre>\n<\/div>\n<p>The <code>image<\/code> object stores the colormap and numeric range. The colorbar uses that information to draw matching colors and tick labels.<\/p>\n<p>Always label the colorbar when the plotted values have units, such as temperature, elevation, error, count, or score. The axis label alone rarely explains what the colors mean.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Control_Ticks_And_Labels\"><\/span>Control Ticks And Labels<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use <code>ticks<\/code> when only specific numeric values should appear on the colorbar. This keeps dense color scales readable.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">try:\n    import numpy as np\n    import matplotlib.pyplot as plt\nexcept ModuleNotFoundError:\n    print(\"Install numpy and matplotlib to run this example.\")\nelse:\n    data = np.linspace(0, 1, 25).reshape(5, 5)\n\n    fig, ax = plt.subplots()\n    image = ax.imshow(data, cmap=\"magma\", vmin=0, vmax=1)\n    colorbar = fig.colorbar(image, ax=ax, ticks=[0, 0.5, 1])\n    colorbar.ax.set_yticklabels([\"low\", \"middle\", \"high\"])\n    plt.close(fig)\n<\/code><\/pre>\n<\/div>\n<p>Tick labels should match the plotted values. If the colorbar shows a continuous measurement, numeric ticks are usually clearer than vague words.<\/p>\n<p>Set <code>vmin<\/code> and <code>vmax<\/code> when several plots need a comparable color scale. Otherwise each plot may choose a different range and make comparisons misleading.<\/p>\n<p><!-- Python Pool visual layout repair 2026-07-13 --><\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/colorbar-mappable-b150.png\" alt=\"Python Pool infographic showing Matplotlib data, Normalize, colormap, mappable artist, and colorbar\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Mappable data: Matplotlib data, Normalize, colormap, mappable artist, and colorbar.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Use_A_Horizontal_Colorbar\"><\/span>Use A Horizontal Colorbar<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A horizontal colorbar can fit better below short, wide plots or figures with several columns.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">try:\n    import numpy as np\n    import matplotlib.pyplot as plt\nexcept ModuleNotFoundError:\n    print(\"Install numpy and matplotlib to run this example.\")\nelse:\n    data = np.random.default_rng(4).normal(size=(6, 8))\n\n    fig, ax = plt.subplots()\n    image = ax.imshow(data, cmap=\"coolwarm\")\n    fig.colorbar(image, ax=ax, orientation=\"horizontal\", pad=0.18, label=\"z score\")\n    plt.close(fig)\n<\/code><\/pre>\n<\/div>\n<p>The <code>pad<\/code> value controls spacing between the axes and the colorbar. Use it with layout tools when labels are getting clipped.<\/p>\n<p>Horizontal bars work best when the tick labels are short. Long labels may need more padding or a larger figure.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Add_A_Colorbar_To_Scatter\"><\/span>Add A Colorbar To Scatter<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>For scatter plots, pass numeric values through the <code>c<\/code> argument and keep the returned scatter object.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">try:\n    import numpy as np\n    import matplotlib.pyplot as plt\nexcept ModuleNotFoundError:\n    print(\"Install numpy and matplotlib to run this example.\")\nelse:\n    x = np.array([1, 2, 3, 4, 5])\n    y = np.array([2, 3, 2, 5, 4])\n    score = np.array([10, 20, 15, 35, 30])\n\n    fig, ax = plt.subplots()\n    points = ax.scatter(x, y, c=score, cmap=\"plasma\")\n    fig.colorbar(points, ax=ax, label=\"score\")\n    plt.close(fig)\n<\/code><\/pre>\n<\/div>\n<p>The colorbar explains the <code>score<\/code> values, not the x or y positions. This is useful when point color represents a third measurement.<\/p>\n<p>If color represents categories rather than continuous numbers, a legend may be better than a colorbar. Use a colorbar when the colors form an ordered scale.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/colorbar-label-b150.png\" alt=\"Python Pool infographic mapping a colorbar through label, ticks, formatter, orientation, and units\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Colorbar labels: A colorbar through label, ticks, formatter, orientation, and units.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Share_One_Colorbar_Across_Subplots\"><\/span>Share One Colorbar Across Subplots<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>When subplots use the same numeric range, one shared colorbar can reduce clutter and make comparisons clearer.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">try:\n    import numpy as np\n    import matplotlib.pyplot as plt\nexcept ModuleNotFoundError:\n    print(\"Install numpy and matplotlib to run this example.\")\nelse:\n    left = np.arange(9).reshape(3, 3)\n    right = left + 3\n\n    fig, axes = plt.subplots(1, 2)\n    first = axes[0].imshow(left, vmin=0, vmax=12, cmap=\"viridis\")\n    axes[1].imshow(right, vmin=0, vmax=12, cmap=\"viridis\")\n    fig.colorbar(first, ax=axes, shrink=0.8, label=\"shared value\")\n    plt.close(fig)\n<\/code><\/pre>\n<\/div>\n<p>Passing the full axes collection tells Matplotlib to allocate colorbar space for the group. The <code>shrink<\/code> argument adjusts the colorbar length relative to the subplot area.<\/p>\n<p>Use a shared colorbar only when the subplots truly use the same scale. If each subplot has a different meaning or range, separate labels may be less confusing.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/colorbar-layout-b150.png\" alt=\"Python Pool infographic comparing axes, colorbar axes, constrained layout, shrink, pad, and aspect\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Colorbar layout: Axes, colorbar axes, constrained layout, shrink, pad, and aspect.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Use_Colorbars_With_Contour_Plots\"><\/span>Use Colorbars With Contour Plots<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>contourf()<\/code> creates filled contour levels. A colorbar shows the value range represented by those filled bands.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">try:\n    import numpy as np\n    import matplotlib.pyplot as plt\nexcept ModuleNotFoundError:\n    print(\"Install numpy and matplotlib to run this example.\")\nelse:\n    x = np.linspace(-2, 2, 30)\n    y = np.linspace(-2, 2, 30)\n    xx, yy = np.meshgrid(x, y)\n    z = xx ** 2 + yy ** 2\n\n    fig, ax = plt.subplots()\n    contour = ax.contourf(xx, yy, z, levels=8, cmap=\"cividis\")\n    fig.colorbar(contour, ax=ax, label=\"distance squared\")\n    plt.close(fig)\n<\/code><\/pre>\n<\/div>\n<p>Contour colorbars are especially helpful when the filled regions are smooth and the boundaries are not labeled directly on the plot.<\/p>\n<p>In short, keep the mappable object returned by the plotting call, pass it to <code>fig.colorbar()<\/code>, label the colorbar, choose ticks deliberately, and share colorbars only when subplots use the same value range. A colorbar should make the encoded values easier to read, not simply add visual weight to the figure.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Connect_The_Colorbar_To_The_Mappable\"><\/span>Connect The Colorbar To The Mappable<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A colorbar is not an independent legend. It explains how values in an image, contour set, scatter collection, or other ScalarMappable map to colors. Pass that mappable to <code>fig.colorbar()<\/code>, and give it the relevant axes so Matplotlib can place it without stealing unexpected space from the plot.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\nimport numpy as np\n\nfig, ax = plt.subplots()\nimage = ax.imshow(np.arange(9).reshape(3, 3), cmap=\"viridis\")\nfig.colorbar(image, ax=ax, label=\"Value\")\nplt.show()<\/code><\/pre>\n<\/div>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/colorbar-check-b150.png\" alt=\"Python Pool infographic testing shared colorbars, vmin vmax, LogNorm, resize, and saved output\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Colorbar checks: Shared colorbars, vmin vmax, LogNorm, resize, and saved output.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Keep_The_Scale_Honest\"><\/span>Keep The Scale Honest<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The colormap controls colors and the normalization controls how data values are positioned along that map. When several plots need comparison, reuse the same <code>Normalize<\/code> range instead of letting each plot choose a different scale. A visually similar color does not imply a numerically comparable value unless the normalization is shared.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Control_Layout_Deliberately\"><\/span>Control Layout Deliberately<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use arguments such as <code>location<\/code>, <code>shrink<\/code>, <code>aspect<\/code>, and <code>fraction<\/code> when the colorbar is too large or crowds the axes. For many subplots, pass a list of axes or use a layout strategy that reserves space for the shared colorbar. Label the units and choose a sequential or diverging map that matches the meaning of the data.<\/p>\n<p data-pythonpool-link-set=\"2026-07-13-final\">For color mapping, compare colorbars with imshow() and pcolormesh(). Read <a href=\"https:\/\/www.pythonpool.com\/matplotlib-imshow\/\">matplotlib imshow<\/a> and <a href=\"https:\/\/www.pythonpool.com\/matplotlib-pcolormesh\/\">matplotlib pcolormesh<\/a> for the related workflow.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_add_a_colorbar_in_Matplotlib\"><\/span>How do I add a colorbar in Matplotlib?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Pass an image, contour set, or other ScalarMappable to fig.colorbar(mappable, ax=ax) and label the data units.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_do_cmap_and_norm_do\"><\/span>What do cmap and norm do?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The colormap chooses the colors and the normalization maps data values onto that color range.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_make_colorbars_comparable_across_plots\"><\/span>How do I make colorbars comparable across plots?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Reuse the same normalization range and colormap policy for each mappable instead of allowing each plot to choose a different scale.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_control_colorbar_size_and_placement\"><\/span>How do I control colorbar size and placement?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Use layout arguments such as location, shrink, aspect, and fraction, and give Matplotlib the relevant axes for space allocation.<\/p>\n<p><script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"How do I add a colorbar in Matplotlib?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Pass an image, contour set, or other ScalarMappable to fig.colorbar(mappable, ax=ax) and label the data units.\"}},{\"@type\":\"Question\",\"name\":\"What do cmap and norm do?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"The colormap chooses the colors and the normalization maps data values onto that color range.\"}},{\"@type\":\"Question\",\"name\":\"How do I make colorbars comparable across plots?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Reuse the same normalization range and colormap policy for each mappable instead of allowing each plot to choose a different scale.\"}},{\"@type\":\"Question\",\"name\":\"How do I control colorbar size and placement?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Use layout arguments such as location, shrink, aspect, and fraction, and give Matplotlib the relevant axes for space allocation.\"}}]}<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Create an honest Matplotlib colorbar by connecting it to a mappable, controlling cmap and norm, sharing scales, and managing layout.<\/p>\n","protected":false},"author":11,"featured_media":34242,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2071],"tags":[3524,3522,3519,3523,3268,3520,3517],"class_list":["post-8365","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-matplotlib","tag-colorbar-label-matplotlib","tag-colorbar-matplotlib","tag-matplotlib-colorbar-label","tag-matplotlib-colorbar-limits","tag-matplotlib-colorbar-pcolormesh","tag-matplotlib-colorbar-range","tag-matplotlib-colorbar-ticks","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v28.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Matplotlib Colorbar: cmap, norm, Labels, and Layout<\/title>\n<meta name=\"description\" content=\"Create Matplotlib colorbars with fig.colorbar(), connect mappables, share norms, label units, and control location, shrink, and aspect.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Matplotlib Colorbar: cmap, norm, Labels, and Layout\" \/>\n<meta property=\"og:description\" content=\"Create an honest Matplotlib colorbar by connecting it to a mappable, controlling cmap and norm, sharing scales, and managing layout.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-07T09:16:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-13T07:01:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/matplotlib-colorbar-scale.png\" \/>\n<meta name=\"author\" content=\"Rohit Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Python Pool\" \/>\n<meta name=\"twitter:description\" content=\"Practical Python tutorials, error fixes, code examples, and project guides.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/matplotlib-colorbar-scale.png\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rohit Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-colorbar\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-colorbar\\\/\"},\"author\":{\"name\":\"Rohit Kumar\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/664ced779ebbb8ddfd3c58a60d6f9d66\"},\"headline\":\"Matplotlib Colorbar: cmap, norm, Labels, and Layout\",\"datePublished\":\"2021-02-07T09:16:24+00:00\",\"dateModified\":\"2026-07-13T07:01:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-colorbar\\\/\"},\"wordCount\":1034,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-colorbar\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/matplotlib-colorbar-guide-pythonpool.png\",\"keywords\":[\"colorbar label matplotlib\",\"colorbar matplotlib\",\"matplotlib colorbar label\",\"matplotlib colorbar limits\",\"matplotlib colorbar pcolormesh\",\"matplotlib colorbar range\",\"matplotlib colorbar ticks\"],\"articleSection\":[\"Matplotlib\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-colorbar\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-colorbar\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-colorbar\\\/\",\"name\":\"Matplotlib Colorbar: cmap, norm, Labels, and Layout\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-colorbar\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-colorbar\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/matplotlib-colorbar-guide-pythonpool.png\",\"datePublished\":\"2021-02-07T09:16:24+00:00\",\"dateModified\":\"2026-07-13T07:01:49+00:00\",\"description\":\"Create Matplotlib colorbars with fig.colorbar(), connect mappables, share norms, label units, and control location, shrink, and aspect.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-colorbar\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-colorbar\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-colorbar\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/matplotlib-colorbar-guide-pythonpool.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/matplotlib-colorbar-guide-pythonpool.png\",\"width\":1350,\"height\":650,\"caption\":\"Matplotlib colorbar guide in Python\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/matplotlib-colorbar\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pythonpool.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Matplotlib Colorbar: cmap, norm, Labels, and Layout\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"name\":\"Python Pool\",\"description\":\"Practical Python tutorials, error fixes, code examples, and project guides.\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pythonpool.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/x.com\\\/pythonpool\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/664ced779ebbb8ddfd3c58a60d6f9d66\",\"name\":\"Rohit Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c9a762a12f6bf9093f4c8a7643c0b6a17dd903d9602fae379649d1b050386b1a?s=96&d=wavatar&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c9a762a12f6bf9093f4c8a7643c0b6a17dd903d9602fae379649d1b050386b1a?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c9a762a12f6bf9093f4c8a7643c0b6a17dd903d9602fae379649d1b050386b1a?s=96&d=wavatar&r=g\",\"caption\":\"Rohit Kumar\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Matplotlib Colorbar: cmap, norm, Labels, and Layout","description":"Create Matplotlib colorbars with fig.colorbar(), connect mappables, share norms, label units, and control location, shrink, and aspect.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/","og_locale":"en_US","og_type":"article","og_title":"Matplotlib Colorbar: cmap, norm, Labels, and Layout","og_description":"Create an honest Matplotlib colorbar by connecting it to a mappable, controlling cmap and norm, sharing scales, and managing layout.","og_url":"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/","og_site_name":"Python Pool","article_published_time":"2021-02-07T09:16:24+00:00","article_modified_time":"2026-07-13T07:01:49+00:00","og_image":[{"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/matplotlib-colorbar-scale.png","type":"","width":"","height":""}],"author":"Rohit Kumar","twitter_card":"summary_large_image","twitter_title":"Python Pool","twitter_description":"Practical Python tutorials, error fixes, code examples, and project guides.","twitter_image":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/matplotlib-colorbar-scale.png","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Rohit Kumar","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/"},"author":{"name":"Rohit Kumar","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/664ced779ebbb8ddfd3c58a60d6f9d66"},"headline":"Matplotlib Colorbar: cmap, norm, Labels, and Layout","datePublished":"2021-02-07T09:16:24+00:00","dateModified":"2026-07-13T07:01:49+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/"},"wordCount":1034,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/matplotlib-colorbar-guide-pythonpool.png","keywords":["colorbar label matplotlib","colorbar matplotlib","matplotlib colorbar label","matplotlib colorbar limits","matplotlib colorbar pcolormesh","matplotlib colorbar range","matplotlib colorbar ticks"],"articleSection":["Matplotlib"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/","url":"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/","name":"Matplotlib Colorbar: cmap, norm, Labels, and Layout","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/matplotlib-colorbar-guide-pythonpool.png","datePublished":"2021-02-07T09:16:24+00:00","dateModified":"2026-07-13T07:01:49+00:00","description":"Create Matplotlib colorbars with fig.colorbar(), connect mappables, share norms, label units, and control location, shrink, and aspect.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/matplotlib-colorbar\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/matplotlib-colorbar-guide-pythonpool.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/matplotlib-colorbar-guide-pythonpool.png","width":1350,"height":650,"caption":"Matplotlib colorbar guide in Python"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/matplotlib-colorbar\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Matplotlib Colorbar: cmap, norm, Labels, and Layout"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Practical Python tutorials, error fixes, code examples, and project guides.","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/664ced779ebbb8ddfd3c58a60d6f9d66","name":"Rohit Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c9a762a12f6bf9093f4c8a7643c0b6a17dd903d9602fae379649d1b050386b1a?s=96&d=wavatar&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c9a762a12f6bf9093f4c8a7643c0b6a17dd903d9602fae379649d1b050386b1a?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c9a762a12f6bf9093f4c8a7643c0b6a17dd903d9602fae379649d1b050386b1a?s=96&d=wavatar&r=g","caption":"Rohit Kumar"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/8365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=8365"}],"version-history":[{"count":24,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/8365\/revisions"}],"predecessor-version":[{"id":41569,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/8365\/revisions\/41569"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/34242"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=8365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=8365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=8365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}