Bird
Raised Fist0
Matplotlibdata~10 mins

Text boxes with bbox in Matplotlib - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a text box with a bounding box around the text.

Matplotlib
import matplotlib.pyplot as plt

plt.text(0.5, 0.5, 'Hello', bbox=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A{'boxstyle': 'round', 'facecolor': 'yellow'}
Bfontsize=12
Ccolor='red'
Dalpha=0.5
Attempts:
3 left
💡 Hint
Common Mistakes
Passing color or fontsize directly to bbox instead of a dictionary.
Not using a dictionary for bbox parameter.
2fill in blank
medium

Complete the code to set the bounding box style to 'circle' for the text box.

Matplotlib
import matplotlib.pyplot as plt

plt.text(0.3, 0.7, 'Circle Box', bbox=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A{'boxstyle': 'square', 'facecolor': 'lightblue'}
B{'boxstyle': 'round', 'facecolor': 'pink'}
C{'boxstyle': 'larrow', 'facecolor': 'orange'}
D{'boxstyle': 'circle', 'facecolor': 'lightgreen'}
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'square' or 'round' instead of 'circle' for the boxstyle.
Not passing a dictionary to bbox.
3fill in blank
hard

Fix the error in the code to properly add a text box with a bounding box and a red edge color.

Matplotlib
import matplotlib.pyplot as plt

plt.text(0.6, 0.4, 'Edge Color', bbox=[1])
plt.show()
Drag options to blanks, or click blank then click option'
A{'boxstyle': 'round', 'facecolor': 'white', 'color': 'red'}
B{'boxstyle': 'round', 'facecolor': 'white', 'edgecolor': 'red'}
C{'boxstyle': 'round', 'facecolor': 'white', 'linewidth': 2}
D{'boxstyle': 'round', 'facecolor': 'white', 'bordercolor': 'red'}
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'color' or 'bordercolor' instead of 'edgecolor'.
Not passing a dictionary to bbox.
4fill in blank
hard

Fill both blanks to create a text box with a shadow effect and a dashed edge.

Matplotlib
import matplotlib.pyplot as plt

plt.text(0.2, 0.8, 'Shadow Box', bbox=[1], [2])
plt.show()
Drag options to blanks, or click blank then click option'
A{'boxstyle': 'round', 'facecolor': 'lightgrey', 'alpha': 0.5}
B{'boxstyle': 'round', 'facecolor': 'white', 'edgecolor': 'black'}
Cedgecolor='black', linestyle='--'
D{'boxstyle': 'round', 'facecolor': 'yellow', 'edgecolor': 'blue'}
Attempts:
3 left
💡 Hint
Common Mistakes
Putting all keys in one dictionary instead of splitting into two.
Using solid line style instead of dashed.
5fill in blank
hard

Fill all three blanks to create a text box with a fancy box style, green face color, and bold font weight.

Matplotlib
import matplotlib.pyplot as plt

plt.text(0.4, 0.6, 'Fancy Box', bbox=[1], fontweight=[2], color=[3])
plt.show()
Drag options to blanks, or click blank then click option'
A{'boxstyle': 'fancy', 'facecolor': 'green'}
B'bold'
C'black'
D'normal'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'normal' fontweight instead of 'bold'.
Not passing a dictionary to bbox.
Using wrong color strings.

Practice

(1/5)
1. What does the bbox parameter do when adding text in matplotlib?
easy
A. It moves the text to a different position on the plot.
B. It changes the font size of the text.
C. It adds a colored box behind the text to highlight it.
D. It removes the text from the plot.

Solution

  1. Step 1: Understand the role of bbox in matplotlib text

    The bbox parameter is used to create a box around the text, usually with color and border, to make it stand out.
  2. Step 2: Compare options with this understanding

    Only It adds a colored box behind the text to highlight it. correctly describes adding a colored box behind the text. Other options describe unrelated text properties.
  3. Final Answer:

    It adds a colored box behind the text to highlight it. -> Option C
  4. Quick Check:

    bbox adds background box [OK]
Hint: Remember bbox means 'box' around text [OK]
Common Mistakes:
  • Confusing bbox with font size or position
  • Thinking bbox removes text
  • Assuming bbox changes text color only
2. Which of the following is the correct syntax to add a text box with a red background and black border in matplotlib?
easy
A. plt.text(0.5, 0.5, 'Hello', bbox={'facecolor':'red', 'edgecolor':'black'})
B. plt.text(0.5, 0.5, 'Hello', boxcolor='red', border='black')
C. plt.text(0.5, 0.5, 'Hello', bgcolor='red', bordercolor='black')
D. plt.text(0.5, 0.5, 'Hello', box={'color':'red', 'border':'black'})

Solution

  1. Step 1: Recall the correct bbox dictionary keys

    The keys for background and border colors in bbox are 'facecolor' and 'edgecolor'.
  2. Step 2: Match syntax with options

    Only plt.text(0.5, 0.5, 'Hello', bbox={'facecolor':'red', 'edgecolor':'black'}) uses bbox={'facecolor':'red', 'edgecolor':'black'} which is correct syntax for text boxes in matplotlib.
  3. Final Answer:

    plt.text(0.5, 0.5, 'Hello', bbox={'facecolor':'red', 'edgecolor':'black'}) -> Option A
  4. Quick Check:

    Use bbox with facecolor and edgecolor [OK]
Hint: Use bbox dict with facecolor and edgecolor keys [OK]
Common Mistakes:
  • Using wrong parameter names like bgcolor or boxcolor
  • Passing bbox as a string instead of a dictionary
  • Confusing border color key with edgecolor
3. What will be the effect of this code snippet?
import matplotlib.pyplot as plt
plt.text(0.3, 0.5, 'Data', bbox=dict(boxstyle='round,pad=0.5', facecolor='yellow', alpha=0.3))
plt.show()
medium
A. Text 'Data' appears without any box or background.
B. Text 'Data' appears with a yellow rounded box with some padding and slight transparency.
C. Text 'Data' appears with a solid yellow square box and no transparency.
D. Code will raise an error due to invalid bbox syntax.

Solution

  1. Step 1: Analyze the bbox dictionary parameters

    The boxstyle='round,pad=0.5' creates a rounded box with padding. facecolor='yellow' sets the box color. alpha=0.3 makes it transparent.
  2. Step 2: Understand the visual effect

    The text will have a yellow rounded box behind it with some padding and slight transparency, matching Text 'Data' appears with a yellow rounded box with some padding and slight transparency..
  3. Final Answer:

    Text 'Data' appears with a yellow rounded box with some padding and slight transparency. -> Option B
  4. Quick Check:

    Rounded box + yellow + alpha = Text 'Data' appears with a yellow rounded box with some padding and slight transparency. [OK]
Hint: Look for boxstyle and alpha for shape and transparency [OK]
Common Mistakes:
  • Ignoring alpha causes confusion about transparency
  • Assuming default square box instead of rounded
  • Thinking code raises error due to correct syntax
4. Identify the error in this code that tries to add a text box with a green background:
plt.text(0.2, 0.8, 'Hello', bbox={'facecolor':green, 'edgecolor':'black'})
medium
A. The color green should be a string: 'green'.
B. The bbox dictionary keys are incorrect.
C. The text coordinates are out of range.
D. The text string 'Hello' is missing quotes.

Solution

  1. Step 1: Check the bbox dictionary values

    The value for facecolor must be a string like 'green'. Here, green is unquoted, causing a NameError.
  2. Step 2: Verify other parts of the code

    Keys are correct, coordinates are valid (0.2, 0.8), and text is quoted properly.
  3. Final Answer:

    The color green should be a string: 'green'. -> Option A
  4. Quick Check:

    Colors must be strings in bbox dict [OK]
Hint: Always quote color names in bbox dict [OK]
Common Mistakes:
  • Forgetting quotes around color names
  • Assuming bbox keys are wrong
  • Thinking coordinates cause error
5. You want to add multiple text labels on a plot, each with a different colored box and 50% transparency. Which approach correctly applies this using a loop?
labels = ['A', 'B', 'C']
colors = ['red', 'green', 'blue']
for i, label in enumerate(labels):
    plt.text(i, i, label, bbox={???})
plt.show()

What should replace {???} to achieve this?
hard
A. {'facecolor': colors[i], 'alpha': 50, 'edgecolor': 'black'}
B. {'facecolor': colors, 'alpha': 0.5, 'edgecolor': 'black'}
C. {'facecolor': colors[i], 'alpha': '0.5', 'edgecolor': 'black'}
D. {'facecolor': colors[i], 'alpha': 0.5, 'edgecolor': 'black'}

Solution

  1. Step 1: Understand how to use loop variables in bbox

    We want each text box to have a different color from colors[i], alpha as 0.5 (float), and a black border.
  2. Step 2: Check each option for correctness

    {'facecolor': colors[i], 'alpha': 0.5, 'edgecolor': 'black'} correctly uses colors[i] for facecolor, alpha as float 0.5, and edgecolor black. {'facecolor': colors, 'alpha': 0.5, 'edgecolor': 'black'} wrongly uses the whole list. {'facecolor': colors[i], 'alpha': '0.5', 'edgecolor': 'black'} uses alpha as string which is invalid. {'facecolor': colors[i], 'alpha': 50, 'edgecolor': 'black'} uses alpha as 50 which is out of range (0 to 1).
  3. Final Answer:

    {'facecolor': colors[i], 'alpha': 0.5, 'edgecolor': 'black'} -> Option D
  4. Quick Check:

    Use colors[i] and alpha as float [OK]
Hint: Use colors[i] and alpha as float between 0 and 1 [OK]
Common Mistakes:
  • Passing entire colors list instead of single color
  • Using alpha as string or out-of-range number
  • Forgetting to index colors with i
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