Bird
Raised Fist0
Matplotlibdata~20 mins

Color mapping with colorbar in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Color Mapping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this matplotlib color mapping code?
Consider the following code that creates a scatter plot with color mapping and a colorbar. What will be the color of the point at x=2, y=2?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.array([1, 2, 3, 4])
y = np.array([1, 2, 3, 4])
colors = np.array([10, 20, 30, 40])

plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar()
plt.close()  # Close plot to avoid display in test

color_at_point = plt.cm.viridis((20 - 10) / (40 - 10))
print(color_at_point)
A[0.127568, 0.566949, 0.550556, 1.0]
B[0.229739, 0.322361, 0.545706, 1.0]
C[0.993248, 0.906157, 0.143936, 1.0]
D[0.267004, 0.004874, 0.329415, 1.0]
Attempts:
2 left
💡 Hint
The color mapping normalizes values between the minimum and maximum of the data array before applying the colormap.
❓ data_output
intermediate
1:30remaining
How many ticks will the colorbar show by default?
Given this code snippet that creates a scatter plot with a colorbar, how many tick marks will the colorbar display by default?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(5)
y = np.arange(5)
colors = np.array([5, 10, 15, 20, 25])

sc = plt.scatter(x, y, c=colors, cmap='plasma')
cbar = plt.colorbar(sc)
plt.close()
A5
B6
C7
D4
Attempts:
2 left
💡 Hint
Matplotlib colorbars by default show 6 ticks unless specified otherwise.
❓ visualization
advanced
2:30remaining
Which option produces a colorbar with discrete color segments?
You want to create a scatter plot with 4 distinct color segments in the colorbar, each representing a range of values. Which code snippet will produce this effect?
A
plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar(ticks=[5, 10, 15, 20])
B
plt.scatter(x, y, c=colors, cmap='viridis', norm=plt.Normalize(vmin=5, vmax=20))
cbar = plt.colorbar(ticks=[5, 10, 15, 20])
C
plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar(boundaries=[5, 10, 15, 20, 25])
D
from matplotlib.colors import BoundaryNorm
bounds = [5, 10, 15, 20, 25]
norm = BoundaryNorm(bounds, ncolors=256)
plt.scatter(x, y, c=colors, cmap='viridis', norm=norm)
plt.colorbar(ticks=bounds)
Attempts:
2 left
💡 Hint
Use BoundaryNorm to create discrete color segments matching boundaries.
🔧 Debug
advanced
1:30remaining
What error does this code raise?
Examine this code that attempts to create a scatter plot with a colorbar. What error will it raise when run?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.array([1, 2, 3])
y = np.array([1, 2, 3])
colors = ['red', 'green', 'blue']

plt.scatter(x, y, c=colors, cmap='viridis')
plt.colorbar()
plt.close()
AAttributeError: 'list' object has no attribute 'shape'
BTypeError: Invalid RGBA argument
CValueError: 'c' argument must be a sequence of numbers
DNo error, plot displays correctly
Attempts:
2 left
💡 Hint
The 'c' parameter expects numeric values when a colormap is specified.
🚀 Application
expert
3:00remaining
How to create a horizontal colorbar with label and custom ticks?
You want to create a scatter plot with a horizontal colorbar below the plot. The colorbar should have the label 'Intensity' and show ticks at 0, 0.5, and 1. Which code snippet achieves this?
A
sc = plt.scatter(x, y, c=colors, cmap='inferno')
cbar = plt.colorbar(sc, orientation='horizontal')
cbar.set_label('Intensity')
cbar.set_ticks([0, 0.5, 1])
B
sc = plt.scatter(x, y, c=colors, cmap='inferno')
cbar = plt.colorbar(sc)
cbar.set_label('Intensity')
cbar.set_ticks([0, 0.5, 1])
C
sc = plt.scatter(x, y, c=colors, cmap='inferno')
cbar = plt.colorbar(sc, orientation='horizontal')
cbar.set_label('Intensity')
cbar.set_ticks([1, 0.5, 0])
D
sc = plt.scatter(x, y, c=colors, cmap='inferno')
cbar = plt.colorbar(sc, orientation='vertical')
cbar.set_label('Intensity')
cbar.set_ticks([0, 0.5, 1])
Attempts:
2 left
💡 Hint
Set orientation='horizontal' in colorbar and use set_label and set_ticks methods.

Practice

(1/5)
1. What is the main purpose of adding a colorbar in a matplotlib plot with color mapping?
easy
A. To show the scale of colors representing data values
B. To change the plot title
C. To add grid lines to the plot
D. To save the plot as an image file

Solution

  1. Step 1: Understand color mapping

    Color mapping assigns colors to data values to visualize differences.
  2. Step 2: Role of colorbar

    The colorbar shows which colors correspond to which data values, acting as a legend.
  3. Final Answer:

    To show the scale of colors representing data values -> Option A
  4. Quick Check:

    Colorbar explains colors = B [OK]
Hint: Colorbar always explains color scale in plots [OK]
Common Mistakes:
  • Thinking colorbar changes plot title
  • Confusing colorbar with grid lines
  • Assuming colorbar saves the plot
2. Which of the following is the correct way to add a colorbar to a scatter plot in matplotlib?
easy
A. plt.scatter(x, y); plt.colorbar(c=z)
B. plt.colorbar(); plt.scatter(x, y, c=z)
C. plt.scatter(x, y, c=z); plt.colorbar()
D. plt.scatter(x, y, c=z, colorbar=True)

Solution

  1. Step 1: Plot with color mapping

    Use plt.scatter(x, y, c=z) to map colors to values z.
  2. Step 2: Add colorbar after plotting

    Call plt.colorbar() after the scatter plot to add the color scale.
  3. Final Answer:

    plt.scatter(x, y, c=z); plt.colorbar() -> Option C
  4. Quick Check:

    Plot first, then colorbar [OK]
Hint: Call plt.colorbar() after plotting with color data [OK]
Common Mistakes:
  • Calling plt.colorbar() before plotting
  • Passing colorbar as a parameter to scatter
  • Trying to pass c=z inside plt.colorbar()
3. What will the following code display?
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(5)
y = np.arange(5)
z = x * y
plt.scatter(x, y, c=z)
plt.colorbar()
plt.show()
medium
A. A scatter plot with points colored from 0 to 16 and a colorbar showing this range
B. A scatter plot with all points in the same color and no colorbar
C. A line plot connecting points with a colorbar
D. An error because colorbar needs a colormap argument

Solution

  1. Step 1: Understand data and color mapping

    x and y are arrays 0 to 4, z is their product (0,1,4,9,16).
  2. Step 2: Scatter plot with color mapping and colorbar

    Points colored by z values, colorbar shows color scale from min to max z.
  3. Final Answer:

    A scatter plot with points colored from 0 to 16 and a colorbar showing this range -> Option A
  4. Quick Check:

    Color mapped scatter with colorbar = D [OK]
Hint: Colorbar shows range of c= values in scatter [OK]
Common Mistakes:
  • Thinking colorbar is missing
  • Expecting a line plot instead of scatter
  • Believing colorbar needs explicit colormap argument
4. Identify the error in this code snippet that tries to add a colorbar:
import matplotlib.pyplot as plt
x = [1, 2, 3]
y = [4, 5, 6]
plt.scatter(x, y)
plt.colorbar()
plt.show()
medium
A. Error because plt.show() is missing
B. No error; code runs fine and shows colorbar
C. Error because plt.colorbar() must be called before scatter
D. Error because scatter plot has no color mapping (c parameter missing)

Solution

  1. Step 1: Check scatter plot parameters

    Scatter plot lacks c= parameter, so no color mapping exists.
  2. Step 2: Understand colorbar requirement

    Colorbar needs a mappable object with colors; without c=, colorbar cannot be created.
  3. Final Answer:

    Error because scatter plot has no color mapping (c parameter missing) -> Option D
  4. Quick Check:

    Colorbar needs color data = A [OK]
Hint: Colorbar requires color mapping (c=) in plot [OK]
Common Mistakes:
  • Calling colorbar before plotting
  • Forgetting plt.show() (though optional)
  • Assuming colorbar works without color data
5. You want to create a heatmap using imshow with a colorbar that shows values from 0 to 1. Which code snippet correctly sets the color limits and adds the colorbar?
hard
A. plt.imshow(data); plt.colorbar(vmin=0, vmax=1)
B. plt.imshow(data, vmin=0, vmax=1); plt.colorbar()
C. plt.imshow(data, colorbar=True, vmin=0, vmax=1)
D. plt.imshow(data); plt.colorbar(); plt.clim(0, 1)

Solution

  1. Step 1: Set color limits in imshow

    Use vmin=0 and vmax=1 in imshow to fix color scale range.
  2. Step 2: Add colorbar without extra arguments

    Call plt.colorbar() after plotting to add the colorbar matching the scale.
  3. Final Answer:

    plt.imshow(data, vmin=0, vmax=1); plt.colorbar() -> Option B
  4. Quick Check:

    Set vmin/vmax in imshow, then colorbar [OK]
Hint: Set vmin/vmax in imshow, then call plt.colorbar() [OK]
Common Mistakes:
  • Passing vmin/vmax to plt.colorbar() instead of imshow
  • Using colorbar=True inside imshow (not valid)
  • Calling plt.clim() after colorbar (wrong order)
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard