0
0
Matplotlibdata~10 mins

Log scale and symlog scale in Matplotlib - Interactive Code Practice

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

Complete the code to set the y-axis to a logarithmic scale.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 10, 100, 1000])
plt.yscale([1])
plt.show()
Drag options to blanks, or click blank then click option'
Alog
Blinear
Csymlog
Dsqrt
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'linear' instead of 'log' for logarithmic scale.
Forgetting to call plt.show() to display the plot.
2fill in blank
medium

Complete the code to set the x-axis to a symmetric logarithmic scale.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([-1000, -10, 0, 10, 1000])
plt.xscale([1])
plt.show()
Drag options to blanks, or click blank then click option'
Alinear
Blog
Csymlog
Dsqrt
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'log' scale which cannot handle zero or negative values.
Not importing matplotlib.pyplot as plt.
3fill in blank
hard

Fix the error in the code to correctly set the y-axis to a symmetric logarithmic scale with a linear threshold of 1.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([-5, -1, 0, 1, 5])
plt.yscale([1], linthresh=1)
plt.show()
Drag options to blanks, or click blank then click option'
Alinear
Bsymlog
Clog
Dlogit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'log' scale with zero or negative values causes errors.
Forgetting to set linthresh parameter for symlog scale.
4fill in blank
hard

Fill both blanks to create a plot with x-axis on symmetric log scale and y-axis on log scale.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([-100, -10, 0, 10, 100], [1, 10, 100, 1000, 10000])
plt.xscale([1])
plt.yscale([2])
plt.show()
Drag options to blanks, or click blank then click option'
Asymlog
Blinear
Clog
Dsqrt
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'log' scale on x-axis with negative values causes errors.
Mixing up xscale and yscale calls.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each number to its log scale value, but uses linear scale for values near zero.

Matplotlib
import numpy as np
numbers = [-10, -1, 0, 1, 10]
scale_map = {n: np.sign(n) * np.log10(abs(n)) if abs(n) > [1] else n for n in numbers if n != [2] or n == [3]
Drag options to blanks, or click blank then click option'
A1
B0
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using zero as threshold causes log10(0) error.
Not excluding zero from log calculation.