YouTip LogoYouTip

Matplotlib Ref Colorbar

Matplotlib colorbar() Function * * Matplotlib Reference Documentation](#) `colorbar()` is used to add a color bar (color scale) to a chart, mapping colors to a range of values. The color bar is a necessary element used with colormap, allowing readers to map colors back to the original data values. ## Function Definition matplotlib.pyplot.colorbar(mappable=None, cax=None, ax=None, **kwargs)Figure.colorbar(mappable=None, cax=None, ax=None, use_gridspec=True, **kwargs) ## Parameter Description | Parameter | Description | | --- | --- | | mappable | The object that can be color-mapped, such as the return value of imshow(), scatter(), contourf() (required) | | ax | The Axes to which the color bar belongs (recommended, automatically adjusts layout) | | cax | Dedicated color bar Axes (used for fine control) | | shrink | The scaling ratio of the color bar relative to Axes, default 1.0, commonly 0.8 | | aspect | The aspect ratio of the color bar, default 20 | | label | The label text of the color bar | | orientation | 'vertical' (default) or 'horizontal' | | ticks | Custom tick positions | | extend | 'neither'/'both'/'min'/'max', indicates colors beyond the range | | pad | The spacing between the color bar and Axes | > Using `fig.colorbar(im, ax=ax)` is the recommended approach, as it automatically adjusts the subplot layout to make space for the color bar. * * * ## Usage Examples ### Example 1: imshow + Vertical Color Bar ## Example import matplotlib.pyplot as plt import numpy as np data = np.random.rand(10,10) fig, ax = plt.subplots(figsize=(6,5), layout='constrained') im = ax.imshow(data, cmap='viridis', aspect='auto') # Add vertical color bar cbar = fig.colorbar(im, ax=ax, label='Value', shrink=0.8) ax.set_title('imshow with Colorbar') ax.set_xlabel('Column') ax.set_ylabel('Row') plt.show() ### Example 2: Horizontal Color Bar + Custom Ticks ## Example import matplotlib.pyplot as plt import numpy as np x = np.linspace(-3,3,100) y = np.linspace(-3,3,100) X, Y = np.meshgrid(x, y) Z = np.sin(X) * np.cos(Y) fig, ax = plt.subplots(figsize=(7,5), layout='constrained') im = ax.contourf(X, Y, Z, levels=15, cmap='coolwarm') # Horizontal color bar (below Axes) cbar = fig.colorbar(im, ax=ax, orientation='horizontal', label='Amplitude', shrink=0.8, pad=0.08, ticks=[-0.8, -0.4,0,0.4,0.8]) ax.set_title('Horizontal Colorbar') ax.set_xlabel('X') ax.set_ylabel('Y') plt.show() ### Example 3: scatter + Color Bar ## Example import matplotlib.pyplot as plt import numpy as np np.random.seed(42) n =100 x = np.random.rand(n) * 10 y = np.random.rand(n) * 10 values = np.random.rand(n) * 100# Data for color mapping fig, ax = plt.subplots(figsize=(7,5), layout='constrained') sc = ax.scatter(x, y, c=values, cmap='YlOrRd', s=100, edgecolors='white', linewidth=0.5) # The PathCollection returned by scatter can also be passed to colorbar cbar = fig.colorbar(sc, ax=ax, label='Score', shrink=0.8) ax.set_title('Scatter with Colorbar') ax.set_xlabel('X') ax.set_ylabel('Y') plt.show() * * * ## Frequently Asked Questions ### Color bar causes inconsistent subplot sizes? Using the `layout='constrained'` parameter can automatically handle this. Or call `fig.colorbar(im, ax=axes)` on the Figure, passing in all affected Axes lists. ### How to share one color bar among multiple subplots? When associating a color bar with multiple subplots, pass `ax=axes_list`. [![Image 2: Matplotlib Reference Documentation](#) Matplotlib Reference Documentation](#)
← Matplotlib Ref ErrorbarMatplotlib Ref Bar β†’