Bird
Raised Fist0
Matplotlibdata~10 mins

Axis scales (linear, log) in Matplotlib - Interactive Code Practice

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

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

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'
A'log10'
B'log'
C'logarithmic'
D'linear'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'logarithmic' instead of 'log'.
Using 'log10' which is not a valid scale name.
Forgetting to put the scale type in quotes.
2fill in blank
medium

Complete the code to set the x-axis scale to linear.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.xscale([1])
plt.show()
Drag options to blanks, or click blank then click option'
A'linear'
B'logarithmic'
C'log'
D'log10'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'log' when linear scale is needed.
Using 'logarithmic' which is not recognized.
Not putting the scale type in quotes.
3fill in blank
hard

Fix the error in the code to set the y-axis scale to logarithmic.

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'
Alogarithmic
B'logarithmic'
Clog
D'log'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the scale type without quotes causing a NameError.
Using 'logarithmic' which is not a valid scale name.
4fill in blank
hard

Fill both blanks to set the x-axis to logarithmic and y-axis to linear scale.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 10, 100, 1000])
plt.xscale([1])
plt.yscale([2])
plt.show()
Drag options to blanks, or click blank then click option'
A'log'
B'linear'
C'logarithmic'
D'log10'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'logarithmic' which is not recognized.
Not quoting the scale type strings.
Mixing up xscale and yscale.
5fill in blank
hard

Fill all three blanks to create a plot with x-axis logarithmic, y-axis logarithmic, and set the base of the log scale to 2.

Matplotlib
import matplotlib.pyplot as plt
plt.plot([1, 2, 4, 8, 16])
plt.xscale([1], base=[2])
plt.yscale([3], base=2)
plt.show()
Drag options to blanks, or click blank then click option'
A'log'
B2
C'linear'
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'linear' instead of 'log' for axes.
Not setting the base parameter correctly.
Using base 10 instead of 2.

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