0
0
Matplotlibdata~15 mins

Colorbar positioning in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Colorbar positioning
📖 Scenario: You are creating a simple heatmap to visualize temperature data across a small grid. To make the heatmap easier to understand, you want to add a colorbar that shows the temperature scale.
🎯 Goal: Build a heatmap using matplotlib and add a colorbar positioned on the right side of the heatmap.
📋 What You'll Learn
Create a 2D list called temperature_data with exact values
Create a matplotlib figure and axis
Plot the heatmap using imshow with the temperature_data
Add a colorbar positioned on the right side of the heatmap
Print the colorbar's orientation to confirm it is vertical
💡 Why This Matters
🌍 Real World
Colorbars help people understand the meaning of colors in heatmaps, such as temperature or intensity in scientific data.
💼 Career
Data scientists and analysts often use colorbars in visualizations to communicate data clearly to others.
Progress0 / 4 steps
1
Create the temperature data
Create a 2D list called temperature_data with these exact values: [[22, 24, 19], [20, 23, 21], [18, 22, 20]]
Matplotlib
Need a hint?

Use square brackets to create a list of lists. Each inner list is a row of temperatures.

2
Set up the matplotlib figure and axis
Import matplotlib.pyplot as plt and create a figure and axis using plt.subplots(). Store the axis in a variable called ax.
Matplotlib
Need a hint?

Use import matplotlib.pyplot as plt and then fig, ax = plt.subplots().

3
Plot the heatmap and add a colorbar on the right
Use ax.imshow(temperature_data) to plot the heatmap and save the result in a variable called heatmap. Then add a colorbar on the right side of the heatmap using fig.colorbar(heatmap, ax=ax, orientation='vertical') and save it in a variable called cbar.
Matplotlib
Need a hint?

Use ax.imshow() to plot and fig.colorbar() with orientation='vertical' to add the colorbar on the right.

4
Print the colorbar orientation
Print the orientation of the colorbar using print(cbar.orientation) to confirm it is vertical.
Matplotlib
Need a hint?

Use print(cbar.orientation) to see the orientation of the colorbar.