How to make colorbar orientation horizontal in Python using Matplotlib?

In Matplotlib, colorbars are displayed vertically by default. To create a horizontal colorbar, use the orientation="horizontal" parameter in the colorbar() method.

Basic Syntax

plt.colorbar(mappable, orientation="horizontal")

Example

Let's create a scatter plot with a horizontal colorbar ?

import numpy as np
import matplotlib.pyplot as plt

# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Generate random data points
x, y, z = np.random.rand(3, 50)

# Create figure and subplot
f, ax = plt.subplots()

# Create scatter plot with color mapping
points = ax.scatter(x, y, c=z, s=50, cmap="plasma")

# Add horizontal colorbar
f.colorbar(points, orientation="horizontal")

plt.show()

Positioning the Horizontal Colorbar

You can control the position and size of the horizontal colorbar using additional parameters ?

import numpy as np
import matplotlib.pyplot as plt

# Generate data
x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(X) * np.cos(Y)

# Create figure and plot
fig, ax = plt.subplots(figsize=(8, 6))
im = ax.contourf(X, Y, Z, levels=20, cmap='viridis')

# Add horizontal colorbar with custom positioning
cbar = fig.colorbar(im, orientation='horizontal', shrink=0.8, pad=0.1)
cbar.set_label('Values', rotation=0, labelpad=15)

plt.show()

Parameters for Horizontal Colorbar

Parameter Description Default
orientation Direction of colorbar "vertical"
shrink Fraction to shrink colorbar 1.0
pad Distance between plot and colorbar 0.05
aspect Ratio of long to short dimension 20

Common Use Cases

Horizontal colorbars work well for:

  • Wide plots − When you have more horizontal than vertical space
  • Multiple subplots − Shared colorbar at the bottom
  • Dashboard layouts − Better integration with horizontal layouts

Conclusion

Use orientation="horizontal" in colorbar() to create horizontal colorbars. Combine with shrink and pad parameters to control positioning and size for better plot layouts.

Updated on: 2026-03-25T23:42:55+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements