Bird
Raised Fist0
Matplotlibdata~10 mins

Axis scales (linear, log) in Matplotlib - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Axis scales (linear, log)
Start: Prepare data
↓
Choose scale type
↓
Set axis scale
↓
Plot data
→Show plot
↓
End
This flow shows preparing data, choosing axis scale (linear or log), setting it, plotting, and displaying the plot.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
x = [1, 10, 100, 1000]
y = [1, 2, 3, 4]
plt.plot(x, y)
plt.xscale('log')
plt.show()
Plot y vs x with x-axis on a logarithmic scale.
Execution Table
StepActionVariable/ParameterValue/EffectOutput/Plot State
1Import matplotlib.pyplotpltmodule loadedNo plot yet
2Define x datax[1, 10, 100, 1000]No plot yet
3Define y datay[1, 2, 3, 4]No plot yet
4Plot dataplt.plot(x, y)Line plot createdPlot with linear axes
5Set x-axis scaleplt.xscale('log')x-axis scale set to logarithmicPlot x-axis changes to log scale
6Show plotplt.show()Plot displayedVisible plot with log-scaled x-axis
💡 Plot displayed with x-axis on logarithmic scale, showing data spaced accordingly.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
xundefined[1, 10, 100, 1000][1, 10, 100, 1000][1, 10, 100, 1000][1, 10, 100, 1000][1, 10, 100, 1000]
yundefinedundefined[1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4][1, 2, 3, 4]
x-axis scalelinear (default)linearlinearlinearlogarithmiclogarithmic
plot statenonenonenoneline plot with linear axesline plot with log x-axisdisplayed plot with log x-axis
Key Moments - 3 Insights
Why does the plot look different after setting plt.xscale('log')?
Because the x-axis changes from linear spacing to logarithmic spacing, the points spread out differently as shown in step 5 of the execution_table.
Can we set the axis scale before plotting the data?
Yes, but in this example, setting the scale after plotting still updates the axis correctly, as seen in steps 4 and 5.
What happens if x contains zero or negative values when using log scale?
Log scale cannot display zero or negative values; matplotlib will raise an error or ignore those points. This is important to remember when choosing log scale.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what change happens to the plot?
AThe y-axis scale changes to logarithmic
BThe x-axis scale changes to logarithmic
CThe plot line color changes
DThe plot is cleared
💡 Hint
Check the 'Action' and 'Value/Effect' columns at step 5 in execution_table.
According to variable_tracker, what is the x-axis scale after step 4?
Alogarithmic
Bunknown
Clinear
Dcategorical
💡 Hint
Look at the 'x-axis scale' row and the 'After Step 4' column in variable_tracker.
If we change x to include zero, what will likely happen when setting plt.xscale('log')?
AMatplotlib will raise an error or ignore zero values
BPlot will display normally
CPlot will switch to linear scale automatically
DPlot will invert the x-axis
💡 Hint
Refer to the key_moments section about zero or negative values with log scale.
Concept Snapshot
Axis scales control how data is spaced on plot axes.
Linear scale spaces data evenly.
Log scale spaces data by powers of 10.
Use plt.xscale('log') or plt.yscale('log') to set log scale.
Log scale cannot handle zero or negative values.
Changing scale affects how data points appear on the plot.
Full Transcript
This visual execution traces plotting data with matplotlib and changing the x-axis scale from linear to logarithmic. We start by importing matplotlib.pyplot and defining x and y data arrays. Initially, plotting uses the default linear scale. Then, we set the x-axis scale to logarithmic using plt.xscale('log'). This changes how the x-axis values are spaced, spreading points according to powers of ten. Finally, plt.show() displays the plot with the log-scaled x-axis. Variables like x, y, and axis scale are tracked step-by-step. Key moments clarify why the plot changes after setting log scale, when to set the scale, and the limitation of log scale with zero or negative values. The quiz tests understanding of axis scale changes and their effects on the plot.

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