Colorbars

A colorbar is the labeled strip beside a plot that acts as the legend for a colormap, translating each color back into the numeric value it represents.

Learn Colorbars in our free Matplotlib course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Matplotlib course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll attach a colorbar with fig.colorbar() , give it a label and custom ticks, change its orientation and size, and share a single bar across several subplots.

Color-carrying plots like imshow and scatter return a mappable — an object that knows its colormap and value range. Capture it in a variable and hand it to fig.colorbar(mappable, ax=ax) . Without that mappable, Matplotlib has nothing to build the legend from.

What you'll see: a 10×10 viridis heatmap with a vertical colorbar on its right edge, labeled "Value", running from dark purple at the bottom to bright yellow at the top.

A colorbar is highly tunable. Use orientation="horizontal" to drop it below the plot, ticks=[...] to mark exactly the values you care about, shrink to scale its length, and pad to set the gap between plot and bar. Combine them to fit any layout.

What you'll see: a plasma heatmap with a shorter, horizontal colorbar sitting below it, marked only at 0, 25, 50, 75, and 100, and labeled "Percent".

When several panels share a scale, a single colorbar is cleaner than one per panel. The key is to give every panel the same vmin and vmax so a color means the same thing everywhere, then pass a list of Axes to the ax argument.

What you'll see: two cividis heatmaps side by side titled "Sensor A" and "Sensor B", with a single colorbar on the far right serving both — because both panels use the identical 0–1 scale.

Replace each ___ to add a labeled colorbar to a colored scatter plot.

Hint: the figure method is colorbar , and the label method on the returned object is set_label .

You called fig.colorbar() without passing the plot object. Capture it first: im = ax.imshow(...) , then fig.colorbar(im, ax=ax) .

Use constrained_layout=True and tune shrink / pad , instead of letting Matplotlib carve a large default slice out of the Axes.

The panels auto-scaled differently. Give every panel the same vmin and vmax before sharing one bar.

Draw a temperature heatmap and give it a shrunk, padded vertical colorbar with custom ticks, a label, and smaller tick text.

Lesson complete — your color scales now explain themselves!

You attached colorbars with fig.colorbar() , labeled and re-ticked them, changed orientation and size with shrink and pad , and shared a single bar across multiple panels.

🚀 Up next: Contour & Filled Contour Plots — visualize a surface as nested level curves.

Practice quiz

What is a 'mappable' that fig.colorbar() needs?

  • A plot object carrying a colormap and value range
  • A dictionary of ticks
  • A figure title
  • A list of colors

Answer: A plot object carrying a colormap and value range. A mappable (from imshow, scatter, contourf, etc.) carries the colormap and limits the colorbar reads.

Which call attaches a colorbar for a captured image im on a given Axes?

  • ax.colorbar(im)
  • fig.colorbar(im, ax=ax)
  • plt.bar(im)
  • im.colorbar(ax)

Answer: fig.colorbar(im, ax=ax). fig.colorbar(im, ax=ax) draws the colorbar and ties it to that Axes.

Which method adds a text label to the colorbar object cbar?

  • cbar.title()
  • cbar.text()
  • cbar.set_label('Value')
  • cbar.label()

Answer: cbar.set_label('Value'). cbar.set_label('Value') labels the colorbar.

Which argument places the colorbar below the plot?

  • shrink=0.5
  • pad=0.1

orientation='horizontal' drops the bar below the plot.

What does the shrink argument do to a colorbar?

  • Scales its length
  • Changes its color
  • Moves it to the top
  • Adds ticks

Answer: Scales its length. shrink scales the bar's length, e.g. shrink=0.7 makes it 70% as long.

To share one colorbar across two panels, you pass ax as:

  • a single Axes

Passing a list of Axes makes one bar serve all of them.

What must match across panels for a shared colorbar to be honest?

  • The figure size
  • The titles
  • The same vmin and vmax
  • The number of ticks

Answer: The same vmin and vmax. Same vmin/vmax means a color means the same value in every panel.

Which argument chooses exactly which values get marked on the bar?

  • pad
  • shrink
  • orientation

ticks=[0, 25, 50, 75, 100] marks just those values.

What error appears if you call fig.colorbar() with no mappable?

  • RuntimeError: No mappable was found
  • ValueError: bad shape
  • KeyError: cmap
  • IndexError

Answer: RuntimeError: No mappable was found. You must capture and pass the plot object; otherwise there is no mappable.

Which method restyles the colorbar's tick labels?

  • cbar.set_label()
  • cbar.ax.tick_params()
  • cbar.ticks()
  • cbar.format()

Answer: cbar.ax.tick_params(). cbar.ax.tick_params(labelsize=9) adjusts the tick text on the colorbar.