0
0
Matplotlibdata~20 mins

Axis scales (linear, log) in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Axis Scale Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of linear vs log scale plot commands
What will be the y-axis limits after running this code snippet?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1, 10, 10)
y = x ** 2
plt.plot(x, y)
plt.yscale('log')
plt.ylim(1, 100)
plt.show()
print(plt.gca().get_ylim())
A(1.0, 100.0)
B(0.0, 100.0)
C(1.0, 1000.0)
D(1.0, 10000.0)
Attempts:
2 left
💡 Hint
Remember that plt.ylim sets limits directly, even if scale is log.
data_output
intermediate
2:00remaining
Number of ticks on log scale axis
How many major ticks will appear on the y-axis after running this code?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1, 100, 100)
y = x
plt.plot(x, y)
plt.yscale('log')
plt.show()
print(len(plt.gca().get_yticks()))
A11
B6
C10
D3
Attempts:
2 left
💡 Hint
Log scale major ticks usually appear at powers of 10.
visualization
advanced
3:00remaining
Identify the plot with log scale on y-axis
Which plot shows a straight line when y = 10^x is plotted?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 3, 100)
y = 10 ** x

plt.figure(figsize=(8, 4))
plt.subplot(1, 2, 1)
plt.plot(x, y)
plt.title('Plot 1')

plt.subplot(1, 2, 2)
plt.plot(x, y)
plt.yscale('log')
plt.title('Plot 2')
plt.show()
APlot 2
BBoth plots show straight lines
CPlot 1
DNeither plot shows a straight line
Attempts:
2 left
💡 Hint
Log scale turns exponential curves into straight lines.
🧠 Conceptual
advanced
2:00remaining
Effect of log scale on zero or negative values
What happens if you set y-axis to log scale but your data contains zero or negative values?
APlot crashes with a SyntaxError
BPlot raises a ValueError or warning and skips those points
CPlot converts zero and negative values to positive automatically
DPlot shows zero and negative values normally on log scale
Attempts:
2 left
💡 Hint
Logarithm of zero or negative numbers is undefined.
🔧 Debug
expert
3:00remaining
Why does this log scale plot show empty y-axis?
Given this code, why does the plot show no data on y-axis?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.yscale('log')
plt.show()
ABecause x values are zero, log scale fails on x-axis
BBecause plt.plot does not support log scale
CBecause y contains negative values, log scale cannot display them
DBecause y values are too small to plot
Attempts:
2 left
💡 Hint
Check the range of y values and log scale requirements.