0
0
Matplotlibdata~20 mins

Creating custom style sheets in Matplotlib - Practice Exercises

Choose your learning style9 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.