Bird
Raised Fist0
Matplotlibdata~20 mins

Rcparams for global defaults in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Rcparams 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 rcParams change?

Consider this code snippet that changes a global default in matplotlib using rcParams. What will be the color of the line in the plot?

Matplotlib
import matplotlib.pyplot as plt
plt.rcParams['lines.color'] = 'red'
plt.plot([1, 2, 3], [4, 5, 6])
line_color = plt.gca().lines[0].get_color()
print(line_color)
A'black'
B'blue'
C'green'
D'red'
Attempts:
2 left
💡 Hint

Changing plt.rcParams['lines.color'] sets the default line color globally.

❓ data_output
intermediate
1:30remaining
What is the default font size after this rcParams update?

After running the following code, what is the default font size used in matplotlib plots?

Matplotlib
import matplotlib.pyplot as plt
plt.rcParams['font.size'] = 14
font_size = plt.rcParams['font.size']
print(font_size)
A14.0
B12.0
C10.0
D16.0
Attempts:
2 left
💡 Hint

Look at the value assigned to plt.rcParams['font.size'].

🔧 Debug
advanced
2:30remaining
Why does this rcParams setting not change the plot background color?

Look at this code snippet:

import matplotlib.pyplot as plt
plt.rcParams['axes.facecolor'] = 'yellow'
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()

The plot background color does not change to yellow. Why?

AThe plot uses a style that overrides rcParams, so the change is ignored.
BThe key 'axes.facecolor' is misspelled and should be 'axes.face_color'.
CThe background color changes only if set before importing matplotlib.
DThe plot background color is controlled by 'figure.facecolor', not 'axes.facecolor'.
Attempts:
2 left
💡 Hint

Check if any style or context is overriding rcParams after setting it.

🧠 Conceptual
advanced
1:30remaining
Which rcParams key controls the default grid line style?

In matplotlib, which rcParams key sets the default style of grid lines?

A'grid.linewidth'
B'grid.linestyle'
C'grid.line_style'
D'grid.style'
Attempts:
2 left
💡 Hint

Look for the key that controls line style specifically for grid lines.

🚀 Application
expert
3:00remaining
How to globally set all line widths to 3 and marker size to 10 using rcParams?

You want to set the default line width to 3 and the default marker size to 10 for all plots globally using rcParams. Which code snippet achieves this?

A
import matplotlib.pyplot as plt
plt.rcParams['line.width'] = 3
plt.rcParams['marker.size'] = 10
B
import matplotlib.pyplot as plt
plt.rcParams['lines.width'] = 3
plt.rcParams['markers.size'] = 10
C
import matplotlib.pyplot as plt
plt.rcParams['lines.linewidth'] = 3
plt.rcParams['lines.markersize'] = 10
D
import matplotlib.pyplot as plt
plt.rcParams['lines.linewidth'] = 10
plt.rcParams['lines.markersize'] = 3
Attempts:
2 left
💡 Hint

Check the exact keys for line width and marker size in rcParams.

Practice

(1/5)
1. What is the main purpose of rcParams in matplotlib?
easy
A. To set global style defaults for all plots
B. To create new plots with random styles
C. To save plots as image files
D. To import data into matplotlib

Solution

  1. Step 1: Understand what rcParams controls

    rcParams is a dictionary-like object in matplotlib that stores default settings for plot styles.
  2. Step 2: Identify the purpose of setting rcParams

    Setting rcParams changes the default appearance of all plots globally, so you don't have to style each plot individually.
  3. Final Answer:

    To set global style defaults for all plots -> Option A
  4. Quick Check:

    rcParams = global style defaults [OK]
Hint: Remember rcParams sets defaults for all plots globally [OK]
Common Mistakes:
  • Thinking rcParams creates plots
  • Confusing rcParams with saving files
  • Believing rcParams imports data
2. Which of the following is the correct way to change the default figure size using rcParams?
easy
A. plt.rcParams['figure.size'] = (8, 6)
B. plt.rcParams['figsize'] = (8, 6)
C. plt.rcParams['fig.size'] = (8, 6)
D. plt.rcParams['figure.figsize'] = (8, 6)

Solution

  1. Step 1: Recall the exact rcParams key for figure size

    The correct key is 'figure.figsize', which expects a tuple for width and height.
  2. Step 2: Check the syntax for assignment

    Assigning a tuple like (8, 6) to plt.rcParams['figure.figsize'] sets the default figure size.
  3. Final Answer:

    plt.rcParams['figure.figsize'] = (8, 6) -> Option D
  4. Quick Check:

    Key 'figure.figsize' = correct syntax [OK]
Hint: Use 'figure.figsize' key exactly for figure size [OK]
Common Mistakes:
  • Using wrong key names like 'figsize' or 'figure.size'
  • Assigning a list instead of a tuple
  • Forgetting to import matplotlib.pyplot as plt
3. What will be the default line width for all plots after running this code?
import matplotlib.pyplot as plt
plt.rcParams['lines.linewidth'] = 3
print(plt.rcParams['lines.linewidth'])
medium
A. 1
B. 2
C. 3
D. Error

Solution

  1. Step 1: Understand the code effect on rcParams

    The code sets 'lines.linewidth' to 3, changing the default line width globally.
  2. Step 2: Check the printed value

    After setting, printing plt.rcParams['lines.linewidth'] outputs 3, confirming the change.
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    Set then print 'lines.linewidth' = 3 [OK]
Hint: Setting rcParams key changes default; print confirms value [OK]
Common Mistakes:
  • Expecting default 1 without setting
  • Confusing linewidth with linestyle
  • Thinking print causes error
4. Identify the error in this code that tries to set the default font size:
import matplotlib.pyplot as plt
plt.rcParams['font.size'] = 'large'
medium
A. The key 'font.size' is incorrect
B. The value should be a number, not a string
C. plt.rcParams cannot be modified
D. Missing import statement

Solution

  1. Step 1: Check the key correctness

    'font.size' is the correct rcParams key for font size.
  2. Step 2: Validate the value type

    The value must be a number (int or float), not a string like 'large'.
  3. Final Answer:

    The value should be a number, not a string -> Option B
  4. Quick Check:

    font.size value must be numeric [OK]
Hint: Font size values must be numbers, not words [OK]
Common Mistakes:
  • Using string values for numeric rcParams keys
  • Assuming rcParams keys are case-insensitive
  • Forgetting to import matplotlib.pyplot
5. You want all your matplotlib plots to have a dark background and white grid lines by default. Which rcParams settings should you apply?
hard
A. plt.rcParams['axes.facecolor'] = 'black'; plt.rcParams['grid.color'] = 'white'
B. plt.rcParams['figure.facecolor'] = 'white'; plt.rcParams['grid.color'] = 'black'
C. plt.rcParams['axes.facecolor'] = 'white'; plt.rcParams['grid.color'] = 'black'
D. plt.rcParams['figure.facecolor'] = 'black'; plt.rcParams['grid.color'] = 'black'

Solution

  1. Step 1: Identify the rcParams key for plot background

    'axes.facecolor' sets the background color inside the plot area, so set it to 'black' for dark background.
  2. Step 2: Identify the rcParams key for grid line color

    'grid.color' controls the color of grid lines; setting it to 'white' makes them visible on a dark background.
  3. Final Answer:

    plt.rcParams['axes.facecolor'] = 'black'; plt.rcParams['grid.color'] = 'white' -> Option A
  4. Quick Check:

    Dark background + white grid = axes.facecolor black, grid.color white [OK]
Hint: Use axes.facecolor for background, grid.color for grid lines [OK]
Common Mistakes:
  • Changing figure.facecolor instead of axes.facecolor
  • Setting grid color to black on black background
  • Confusing axes and figure color keys
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