Bird
Raised Fist0
Matplotlibdata~20 mins

Creating custom style sheets in Matplotlib - Practice Exercises

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Custom Style Sheet Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of applying a custom style sheet

What will be the color of the line in the plot after applying the custom style sheet?

Matplotlib
import matplotlib.pyplot as plt
import matplotlib as mpl

custom_style = {
    'lines.linewidth': 3,
    'lines.color': 'red',
    'axes.titlesize': 'large'
}

mpl.rcParams.update(custom_style)
plt.plot([1, 2, 3], [1, 4, 9])
plt.title('Test Plot')
plt.show()
AThe line will be thick and red in color.
BThe line will be thin and blue in color.
CThe line will be thick and blue in color.
DThe line will be thin and red in color.
Attempts:
2 left
💡 Hint

Check the lines.linewidth and lines.color keys in the style dictionary.

❓ data_output
intermediate
1:30remaining
Number of style parameters applied

How many style parameters are applied when using the following custom style sheet?

Matplotlib
custom_style = {
    'lines.linewidth': 2,
    'axes.facecolor': 'lightgray',
    'grid.color': 'white',
    'grid.linestyle': '--'
}

import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.rcParams.update(custom_style)
params = mpl.rcParams
count = sum(1 for key in custom_style if key in params)
print(count)
A3
B4
C2
D1
Attempts:
2 left
💡 Hint

Count how many keys in custom_style exist in mpl.rcParams.

🔧 Debug
advanced
2:00remaining
Identify the error in custom style sheet usage

What error will occur when running this code?

Matplotlib
import matplotlib.pyplot as plt
import matplotlib as mpl

custom_style = {
    'lines.linewidth': 'thick',
    'axes.titlesize': 14
}

mpl.rcParams.update(custom_style)
plt.plot([1, 2, 3], [1, 4, 9])
plt.title('Test Plot')
plt.show()
ANo error, plot shows with thick lines.
BKeyError because 'lines.linewidth' is not a valid rcParam.
CTypeError because 'lines.linewidth' expects a number, not a string.
DValueError because 'axes.titlesize' must be a string, not an integer.
Attempts:
2 left
💡 Hint

Check the expected data type for lines.linewidth.

❓ visualization
advanced
2:00remaining
Effect of custom style on grid appearance

Which option best describes the grid appearance after applying this custom style?

Matplotlib
import matplotlib.pyplot as plt
import matplotlib as mpl

custom_style = {
    'grid.color': 'green',
    'grid.linestyle': ':',
    'grid.linewidth': 2
}

mpl.rcParams.update(custom_style)
plt.plot([1, 2, 3], [1, 4, 9])
plt.grid(True)
plt.show()
AThe grid lines are green, dotted, and thick.
BThe grid lines are green, dashed, and thin.
CThe grid lines are blue, dotted, and thick.
DThe grid lines are blue, solid, and thin.
Attempts:
2 left
💡 Hint

Look at the grid.color, grid.linestyle, and grid.linewidth values.

🧠 Conceptual
expert
2:30remaining
Understanding custom style sheet priority

Given the following code, what will be the color of the plot line?

Matplotlib
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.style.use('seaborn-darkgrid')
custom_style = {
    'lines.color': 'purple',
    'lines.linewidth': 1
}
mpl.rcParams.update(custom_style)
plt.plot([1, 2, 3], [1, 4, 9])
plt.show()
AThe code will raise an error because multiple styles are used.
BThe line color will be the seaborn default because it has higher priority.
CThe line color will be blue because matplotlib defaults override styles.
DThe line color will be purple because the custom style overrides the seaborn style.
Attempts:
2 left
💡 Hint

Consider the order of mpl.style.use() calls and which style is applied last.

Practice

(1/5)
1. What is the main purpose of creating a custom style sheet in matplotlib?
easy
A. To make charts look consistent and save time when plotting
B. To write Python code faster
C. To change the data in the plot
D. To export plots as images

Solution

  1. Step 1: Understand the role of style sheets

    Style sheets control the appearance of plots, such as colors and fonts.
  2. Step 2: Identify the benefit of custom style sheets

    Custom style sheets help keep charts consistent and save time by reusing styles.
  3. Final Answer:

    To make charts look consistent and save time when plotting -> Option A
  4. Quick Check:

    Custom style sheets = consistent charts + time saving [OK]
Hint: Custom styles = consistent look + less work [OK]
Common Mistakes:
  • Thinking style sheets change data
  • Confusing style sheets with exporting images
  • Believing style sheets speed up coding
2. Which of the following is the correct way to apply a custom style sheet named my_style.mplstyle in matplotlib?
easy
A. plt.style('my_style')
B. plt.use_style('my_style')
C. plt.style.use('my_style.mplstyle')
D. plt.style.apply('my_style.mplstyle')

Solution

  1. Step 1: Recall the correct function to apply styles

    The function to apply a style sheet is plt.style.use().
  2. Step 2: Check the correct argument format

    The style file name should be passed as a string, including the extension if needed.
  3. Final Answer:

    plt.style.use('my_style.mplstyle') -> Option C
  4. Quick Check:

    Use plt.style.use() with filename [OK]
Hint: Use plt.style.use('filename') to apply styles [OK]
Common Mistakes:
  • Using incorrect function names like plt.use_style
  • Omitting .mplstyle extension when needed
  • Using plt.style() instead of plt.style.use()
3. Given the following code, what will be the color of the line in the plot?
import matplotlib.pyplot as plt
plt.style.use('custom_style.mplstyle')
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()

Assuming custom_style.mplstyle contains:
lines.color: red
medium
A. Black
B. Blue
C. Green
D. Red

Solution

  1. Step 1: Understand style sheet effect

    The style sheet sets lines.color to red, which changes line color.
  2. Step 2: Apply style to plot

    Since plt.style.use('custom_style.mplstyle') is called, the line color becomes red.
  3. Final Answer:

    Red -> Option D
  4. Quick Check:

    Style sets lines.color = red, so line is red [OK]
Hint: Style file sets line color, so plot uses that color [OK]
Common Mistakes:
  • Ignoring style file and expecting default color
  • Confusing color names
  • Not applying style before plotting
4. What is wrong with this code snippet if the custom style sheet my_style.mplstyle is not applied?
import matplotlib.pyplot as plt
plt.style.use(my_style.mplstyle)
plt.plot([1, 2], [3, 4])
plt.show()
medium
A. The style file name should be a string in quotes
B. plt.plot() is missing required arguments
C. plt.show() should be called before plt.plot()
D. The style file must be imported as a module

Solution

  1. Step 1: Check how style file is passed

    The style file name must be a string, so it needs quotes around it.
  2. Step 2: Identify the error in code

    Without quotes, Python treats my_style.mplstyle as a variable, causing an error.
  3. Final Answer:

    The style file name should be a string in quotes -> Option A
  4. Quick Check:

    File name must be string in plt.style.use() [OK]
Hint: Put style file name in quotes inside plt.style.use() [OK]
Common Mistakes:
  • Forgetting quotes around style file name
  • Calling plt.show() too early
  • Trying to import style file as Python module
5. You want to create a custom style sheet that sets the plot background to light gray and the grid lines to dashed blue. Which of the following .mplstyle file contents will achieve this?
hard
A. figure.facecolor: #d3d3d3 grid.color: blue grid.linestyle: '--'
B. axes.facecolor: lightgray grid.color: blue grid.linestyle: '--'
C. background.color: lightgray grid.color: blue grid.line: dashed
D. axes.background: lightgray grid.style: dashed grid.color: blue

Solution

  1. Step 1: Identify correct keys for background and grid

    The correct key for plot background is axes.facecolor. Grid color uses grid.color and line style uses grid.linestyle.
  2. Step 2: Match values to keys

    Light gray is 'lightgray', grid color is 'blue', and dashed lines use '--'. axes.facecolor: lightgray grid.color: blue grid.linestyle: '--' matches all correctly.
  3. Final Answer:

    axes.facecolor: lightgray grid.color: blue grid.linestyle: '--' -> Option B
  4. Quick Check:

    Use axes.facecolor and grid.linestyle for background and grid [OK]
Hint: Use axes.facecolor and grid.linestyle keys in style file [OK]
Common Mistakes:
  • Using wrong keys like axes.background or grid.style
  • Wrong color names or formats
  • Missing quotes or incorrect line style syntax
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