A colorbar shows the scale of colors used in a plot. It helps us understand what the colors mean in numbers.
Colorbar configuration in Matplotlib
import matplotlib.pyplot as plt # Create a plot with a colorbar im = plt.imshow(data) cb = plt.colorbar(im, orientation='vertical', fraction=0.046, pad=0.04) # Customize colorbar cb.set_label('Label text') cb.set_ticks([0, 0.5, 1]) cb.ax.tick_params(labelsize=10)
plt.colorbar()
You can change orientation with orientation='vertical' or 'horizontal'.
im.plt.colorbar(im)
plt.colorbar(im, orientation='horizontal')cb = plt.colorbar(im)
cb.set_label('Temperature (°C)')cb = plt.colorbar(im) cb.set_ticks([0, 0.5, 1])
This code creates a 10x10 grid of random numbers and shows it as a colored image. The colorbar on the right explains the color scale from 0 to 1 with ticks at 0, 0.5, and 1. The label 'Random values' describes what the colors mean.
import matplotlib.pyplot as plt import numpy as np # Create sample data np.random.seed(0) data = np.random.rand(10, 10) # Plot the data as an image im = plt.imshow(data, cmap='viridis') # Add a vertical colorbar with label and custom ticks cb = plt.colorbar(im, orientation='vertical', fraction=0.046, pad=0.04) cb.set_label('Random values') cb.set_ticks([0, 0.5, 1]) cb.ax.tick_params(labelsize=8) plt.title('Heatmap with Colorbar') plt.show()
You can control the size and position of the colorbar using fraction and pad parameters.
Use cb.ax.tick_params() to change tick label size and style.
Colorbar orientation can be vertical or horizontal depending on your plot layout.
A colorbar helps explain the meaning of colors in your plot.
Use plt.colorbar() to add and customize it.
You can change orientation, labels, and ticks to make it clear.
Practice
Solution
Step 1: Understand the role of color in plots
Colors often represent data values or categories in a plot.Step 2: Purpose of colorbar
A colorbar explains what each color means, helping viewers interpret the plot correctly.Final Answer:
To show the meaning of colors used in the plot -> Option CQuick Check:
Colorbar explains colors = C [OK]
- Thinking colorbar changes background color
- Confusing colorbar with plot title
- Assuming colorbar saves the plot
im?Solution
Step 1: Identify the function to add colorbar
Matplotlib usesplt.colorbar()and requires the image or plot object as argument.Step 2: Check the correct syntax
Passingimtoplt.colorbar(im)correctly links the colorbar to the plot.Final Answer:
plt.colorbar(im) -> Option AQuick Check:
plt.colorbar(im) adds colorbar [OK]
- Calling colorbar without argument
- Trying to call colorbar on plot object
- Using non-existent plt.add_colorbar function
import matplotlib.pyplot as plt import numpy as np img = np.random.rand(5,5) im = plt.imshow(img, cmap='viridis') cb = plt.colorbar(im, orientation='horizontal') plt.show()
Solution
Step 1: Understand the orientation parameter
Settingorientation='horizontal'places the colorbar horizontally below or above the plot.Step 2: Check the code behavior
The code creates a heatmap and adds a horizontal colorbar below it, so the colorbar is horizontal.Final Answer:
A horizontal colorbar appears below the image -> Option BQuick Check:
orientation='horizontal' means horizontal colorbar [OK]
- Assuming default vertical colorbar ignores orientation
- Thinking orientation='horizontal' causes error
- Confusing colorbar position with plot title
import matplotlib.pyplot as plt import numpy as np img = np.random.rand(10,10) im = plt.imshow(img) plt.clf() plt.colorbar() plt.show()
Solution
Step 1: Check plt.colorbar() usage
When adding a colorbar, you must pass the image or plot object toplt.colorbar().Step 2: Identify missing argument
The code callsplt.colorbar()withoutim, so matplotlib doesn't know which plot to link the colorbar to.Final Answer:
plt.colorbar() missing the plot object argument -> Option DQuick Check:
colorbar needs plot object argument [OK]
- Forgetting to pass the image to colorbar
- Thinking plt.imshow() can't have colorbar
- Calling plt.show() before colorbar
import matplotlib.pyplot as plt import numpy as np img = np.random.rand(10,10) im = plt.imshow(img, cmap='plasma') cb = plt.colorbar(im) # Which line below sets the ticks correctly?
Solution
Step 1: Identify the colorbar object
The variablecbholds the colorbar returned byplt.colorbar(im).Step 2: Use the correct method to set ticks
The method to set ticks on a colorbar isset_ticks()called on the colorbar object.Final Answer:
cb.set_ticks([0.2, 0.5, 0.8]) -> Option AQuick Check:
Use colorbar.set_ticks() to set ticks [OK]
- Trying to set ticks on plt.colorbar directly
- Setting ticks on the image object
- Assigning ticks list to attribute instead of method
Ready to prove it?
Pass the test and this pattern moves unknown
