0
0
MatplotlibHow-ToBeginner ยท 3 min read

How to Add Colorbar to Scatter Plot in Matplotlib

To add a colorbar to a scatter plot in matplotlib, first create the scatter plot with a c parameter for colors and save the returned PathCollection object. Then call plt.colorbar() with that object to display the colorbar. This links the colors in the scatter plot to the colorbar scale.
๐Ÿ“

Syntax

Use scatter() to create the scatter plot with a color array, then pass the returned object to colorbar() to add the colorbar.

  • scatter(x, y, c=colors, cmap='colormap'): Plots points with colors from colors.
  • colorbar(scatter_obj): Adds a colorbar linked to the scatter plot colors.
python
scatter_obj = plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar(scatter_obj)
๐Ÿ’ป

Example

This example shows how to create a scatter plot with points colored by their values and add a colorbar to explain the color scale.

python
import matplotlib.pyplot as plt
import numpy as np

# Sample data
x = np.random.rand(50)
y = np.random.rand(50)
colors = x + y  # Color by sum of x and y

# Create scatter plot with color mapping
scatter = plt.scatter(x, y, c=colors, cmap='plasma')

# Add colorbar linked to scatter
plt.colorbar(scatter)

plt.title('Scatter Plot with Colorbar')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.show()
Output
A scatter plot window appears showing points colored from purple to yellow with a vertical colorbar on the right indicating the color scale.
โš ๏ธ

Common Pitfalls

Common mistakes when adding a colorbar to scatter plots include:

  • Not passing the scatter plot object to plt.colorbar(), which results in no colorbar or an unrelated one.
  • Using a constant color for all points without a color array, so the colorbar has no meaning.
  • Forgetting to specify a colormap (cmap) when colors are numeric, which can lead to default or unclear colors.
python
import matplotlib.pyplot as plt
import numpy as np

x = np.random.rand(10)
y = np.random.rand(10)

# Wrong: No color array, so colorbar is meaningless
scatter = plt.scatter(x, y, color='red')
# plt.colorbar(scatter)  # This will raise an error or show no colorbar

# Right: Use color array and pass scatter to colorbar
colors = x * y
scatter = plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar(scatter)
plt.show()
Output
The first scatter plot with fixed red points does not produce a colorbar; the second plot shows points colored by product of x and y with a matching colorbar.
๐Ÿ“Š

Quick Reference

Tips for adding colorbars to scatter plots:

  • Always assign the scatter plot to a variable to pass it to plt.colorbar().
  • Use c= with numeric data to map colors.
  • Choose a colormap with cmap= for better visualization.
  • Call plt.colorbar() after the scatter plot to display the colorbar.
โœ…

Key Takeaways

Pass the scatter plot object returned by plt.scatter() to plt.colorbar() to add a colorbar.
Use the c parameter with numeric data to color points meaningfully.
Specify a colormap with cmap for clear color representation.
Avoid fixed colors if you want a meaningful colorbar.
Call plt.colorbar() after creating the scatter plot.