Bird
Raised Fist0
Matplotlibdata~5 mins

Axis scales (linear, log) in Matplotlib

Choose your learning style10 modes available
Introduction

Axis scales help us show data clearly. Linear scale shows data evenly. Log scale shows data that changes a lot in a smaller space.

When you want to show normal data that changes evenly.
When your data has very big and very small values together.
When you want to see percentage changes or growth rates clearly.
When you want to compare data that grows exponentially.
When you want to avoid big values hiding small values on the graph.
Syntax
Matplotlib
import matplotlib.pyplot as plt

plt.plot(x, y)
plt.xscale('linear' or 'log')
plt.yscale('linear' or 'log')
plt.show()

Use 'linear' for normal scale and 'log' for logarithmic scale.

You can set scale for x-axis, y-axis, or both.

Examples
Both axes use normal linear scale.
Matplotlib
plt.xscale('linear')
plt.yscale('linear')
X-axis uses log scale, Y-axis uses linear scale.
Matplotlib
plt.xscale('log')
plt.yscale('linear')
Both axes use log scale, good for data with wide range.
Matplotlib
plt.xscale('log')
plt.yscale('log')
Sample Program

This code plots the function y = x² twice. The first plot uses normal linear scales on both axes. The second plot uses logarithmic scales on both axes. This helps see how the data grows quickly and fits better on the graph.

Matplotlib
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(1, 100, 100)
y = x ** 2

plt.figure(figsize=(10, 4))

plt.subplot(1, 2, 1)
plt.plot(x, y)
plt.title('Linear scale')
plt.xscale('linear')
plt.yscale('linear')
plt.xlabel('x')
plt.ylabel('y = x^2')

plt.subplot(1, 2, 2)
plt.plot(x, y)
plt.title('Log scale')
plt.xscale('log')
plt.yscale('log')
plt.xlabel('x (log scale)')
plt.ylabel('y = x^2 (log scale)')

plt.tight_layout()
plt.show()
OutputSuccess
Important Notes

Log scale cannot show zero or negative values. Make sure data is positive.

Log scale helps to see multiplicative changes clearly.

You can combine linear and log scales on different axes for better visualization.

Summary

Linear scale shows data evenly spaced.

Log scale shows data on a multiplicative scale, useful for wide ranges.

Use plt.xscale() and plt.yscale() to set axis scales in matplotlib.

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