Bird
Raised Fist0
Matplotlibdata~20 mins

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

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

Practice

(1/5)
1. What does setting the axis scale to log do in a matplotlib plot?
easy
A. It shows data spaced by powers of 10, useful for wide-ranging values.
B. It reverses the axis direction.
C. It colors the axis labels differently.
D. It removes the axis ticks.

Solution

  1. Step 1: Understand axis scale types

    Linear scale spaces data evenly, while log scale spaces data by powers of 10.
  2. Step 2: Effect of log scale on data display

    Log scale compresses large ranges, showing exponential growth clearly.
  3. Final Answer:

    It shows data spaced by powers of 10, useful for wide-ranging values. -> Option A
  4. Quick Check:

    Log scale = spaced by powers of 10 [OK]
Hint: Log scale spaces data by powers of 10, not evenly [OK]
Common Mistakes:
  • Thinking log scale reverses axis
  • Confusing log scale with color changes
  • Assuming log scale removes ticks
2. Which of the following is the correct way to set the y-axis to a logarithmic scale in matplotlib?
easy
A. plt.yaxis('log')
B. plt.axis('log')
C. plt.set_yscale('log')
D. plt.yscale('log')

Solution

  1. Step 1: Recall matplotlib axis scale syntax

    The correct function to set axis scale is plt.yscale() for y-axis.
  2. Step 2: Check correct usage

    Calling plt.yscale('log') sets y-axis to logarithmic scale.
  3. Final Answer:

    plt.yscale('log') -> Option D
  4. Quick Check:

    Use plt.yscale('log') to set y-axis log scale [OK]
Hint: Use plt.yscale('log') to set y-axis log scale [OK]
Common Mistakes:
  • Using plt.axis('log') which is invalid
  • Trying plt.set_yscale which does not exist
  • Using plt.yaxis('log') which is incorrect
3. What will be the y-axis tick labels when running this code?
import matplotlib.pyplot as plt
plt.plot([1, 10, 100, 1000])
plt.yscale('log')
plt.show()
medium
A. [1, 10, 100, 1000]
B. [0, 1, 2, 3]
C. [1, 2, 3, 4]
D. [10, 100, 1000, 10000]

Solution

  1. Step 1: Understand the data and scale

    The data points are 1, 10, 100, 1000 and y-axis is set to log scale.
  2. Step 2: Effect of log scale on ticks

    Log scale shows ticks at powers of 10 matching data values, so ticks will be 1, 10, 100, 1000.
  3. Final Answer:

    [1, 10, 100, 1000] -> Option A
  4. Quick Check:

    Log scale ticks = powers of 10 matching data [OK]
Hint: Log scale ticks match powers of 10 in data [OK]
Common Mistakes:
  • Expecting linear spaced ticks like 0,1,2,3
  • Confusing tick labels with data indices
  • Assuming ticks multiply by 10 beyond data range
4. Identify the error in this code that tries to set the x-axis to logarithmic scale:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.xscale('logarithmic')
plt.show()
medium
A. The method name should be plt.set_xscale()
B. The scale argument should be 'log', not 'logarithmic'
C. plt.plot() cannot be used with log scale
D. Missing plt.show() call

Solution

  1. Step 1: Check method and argument correctness

    The method plt.xscale() is correct, but the argument must be 'log' for logarithmic scale.
  2. Step 2: Identify the wrong argument

    The code uses 'logarithmic' which is invalid and causes an error.
  3. Final Answer:

    The scale argument should be 'log', not 'logarithmic' -> Option B
  4. Quick Check:

    Use 'log' as argument in plt.xscale() [OK]
Hint: Use 'log' exactly as argument for log scale [OK]
Common Mistakes:
  • Using 'logarithmic' instead of 'log'
  • Thinking plt.set_xscale() is needed
  • Believing plt.plot() can't work with log scale
5. You have data ranging from 0.001 to 1000. Which axis scale setting will best show all data clearly on a plot?
hard
A. Use linear scale on both axes
B. Use linear scale on x-axis and log scale on y-axis
C. Use log scale on both axes
D. Use log scale on x-axis and linear scale on y-axis

Solution

  1. Step 1: Analyze data range

    The data spans from 0.001 to 1000, a wide range over several orders of magnitude.
  2. Step 2: Choose scale to display wide range clearly

    Log scale compresses large ranges and shows small and large values clearly on the same plot.
  3. Step 3: Decide axis scales

    Using log scale on both axes best displays wide-ranging data without distortion.
  4. Final Answer:

    Use log scale on both axes -> Option C
  5. Quick Check:

    Wide range data = log scale both axes [OK]
Hint: Wide range data needs log scale on both axes [OK]
Common Mistakes:
  • Using linear scale hides small values
  • Mixing linear and log scales without reason
  • Assuming linear scale works for all ranges
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