Bird
Raised Fist0
Matplotlibdata~10 mins

Log scale and symlog scale in Matplotlib - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Log scale and symlog scale
Start: Plot data
↓
Choose axis scale
↓
Linear scale
↓
Log scale
↓
Symlog scale
↓
Apply scale to axis
↓
Render plot with scale
↓
Show plot output
The flow shows how data is plotted, then a scale type is chosen (linear, log, or symlog), applied to the axis, and finally the plot is rendered with that scale.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 100)
y = x**3
plt.plot(x, y)
plt.yscale('symlog', linthresh=100)
plt.show()
This code plots y = x^3 with a symmetric log scale on the y-axis, allowing negative and positive values with a linear region near zero.
Execution Table
StepActionx values sampley values sampleAxis scale appliedPlot effect
1Generate x values[-10, -5, 0, 5, 10][...]NoneData ready for plotting
2Calculate y = x^3[-10, -5, 0, 5, 10][-1000, -125, 0, 125, 1000]Noney values computed
3Plot x vs y[-10, -5, 0, 5, 10][-1000, -125, 0, 125, 1000]NonePlot with default linear scale
4Set y-axis scale to 'symlog' with linthresh=100[-10, -5, 0, 5, 10][-1000, -125, 0, 125, 1000]symlogNegative and positive y shown with linear zone near 0
5Render and show plot[-10, -5, 0, 5, 10][-1000, -125, 0, 125, 1000]symlogPlot displayed with symlog y-axis scale
💡 Plot is shown with y-axis using symlog scale, handling negative and positive values smoothly.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
xNone[-10, -5, 0, 5, 10][-10, -5, 0, 5, 10][-10, -5, 0, 5, 10]
yNone[-1000, -125, 0, 125, 1000][-1000, -125, 0, 125, 1000][-1000, -125, 0, 125, 1000]
axis_scalelinear (default)linear (default)symlog (linthresh=100)symlog (linthresh=100)
Key Moments - 2 Insights
Why can't we use a normal log scale when y has negative values?
Log scale only works with positive values because log of zero or negative numbers is undefined. The symlog scale solves this by combining linear scale near zero and log scale away from zero, allowing negative values.
What does the 'linthresh' parameter do in symlog scale?
'linthresh' sets the range around zero where the scale is linear instead of logarithmic. This prevents extreme compression near zero and helps visualize values close to zero clearly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at Step 4, what axis scale is applied?
ALinear scale
BLog scale
CSymlog scale
DNo scale applied
💡 Hint
Check the 'Axis scale applied' column in Step 4 of the execution table.
According to variable_tracker, what is the value of y after Step 2 for x=5?
A25
B125
C5
D-125
💡 Hint
Look at the 'y' row and 'After Step 2' column in variable_tracker.
If we set linthresh=10 instead of 100 in symlog, what changes in the plot effect?
AThe linear zone near zero becomes smaller
BThe linear zone near zero becomes larger
CNegative values are not shown
DThe plot switches to linear scale
💡 Hint
Refer to the key_moments explanation about 'linthresh' parameter.
Concept Snapshot
Log scale: axis uses logarithm, only positive values allowed.
Symlog scale: combines linear near zero and log away from zero.
Use symlog to plot data with negative and positive values.
Set 'linthresh' to control linear region size near zero.
In matplotlib: plt.yscale('log') or plt.yscale('symlog', linthresh=...)
Symlog helps visualize wide range including negatives.
Full Transcript
This visual execution shows how to plot data using log and symlog scales in matplotlib. First, x values are generated and y values calculated as x cubed. Then the plot is created with default linear scale. Next, the y-axis scale is changed to symlog with a linear threshold of 100, allowing negative and positive y values to be displayed properly. The plot is rendered showing the effect of symlog scale. Variables x and y remain the same, but the axis scale changes from linear to symlog. Key points include that log scale cannot handle negative values, while symlog can by using a linear region near zero controlled by linthresh. The quizzes test understanding of axis scale applied, y values, and effect of linthresh parameter.

Practice

(1/5)
1. What is the main difference between a log scale and a symlog scale in matplotlib?
easy
A. Log scale only works for positive values, while symlog can handle negative and zero values.
B. Log scale can handle negative values, but symlog cannot.
C. Symlog scale is only for categorical data, log scale is for numerical data.
D. Log scale and symlog scale are exactly the same.

Solution

  1. Step 1: Understand log scale limitations

    Log scale transforms data using logarithms and only supports positive values because logarithm of zero or negative numbers is undefined.
  2. Step 2: Understand symlog scale features

    Symlog scale combines linear and logarithmic scales to handle negative, zero, and positive values by using a linear region around zero.
  3. Final Answer:

    Log scale only works for positive values, while symlog can handle negative and zero values. -> Option A
  4. Quick Check:

    Log scale positive only, symlog includes negatives [OK]
Hint: Log scale = positive only; symlog = includes negatives and zero [OK]
Common Mistakes:
  • Thinking log scale supports zero or negative values
  • Confusing symlog with linear scale
  • Assuming symlog is only for positive values
2. Which of the following is the correct way to set the y-axis to a symlog scale in matplotlib?
easy
A. ax.set_scale('symlog')
B. ax.set_yscale('logarithm')
C. ax.set_yscale('symlog')
D. ax.set_scale('log')

Solution

  1. Step 1: Recall matplotlib scale setting methods

    Matplotlib uses ax.set_xscale() or ax.set_yscale() to set axis scales.
  2. Step 2: Identify correct scale name and method

    To set y-axis to symlog, use ax.set_yscale('symlog'). Other options are either incorrect method names or scale names.
  3. Final Answer:

    ax.set_yscale('symlog') -> Option C
  4. Quick Check:

    Use set_yscale with 'symlog' string [OK]
Hint: Use ax.set_yscale('symlog') to set y-axis symlog scale [OK]
Common Mistakes:
  • Using ax.set_scale instead of ax.set_yscale
  • Using wrong scale name like 'logarithm'
  • Trying to set scale on x-axis when y-axis is intended
3. What will be the output of the following code snippet?
import matplotlib.pyplot as plt
import numpy as np

x = np.array([-100, -10, -1, 0, 1, 10, 100])
y = x

fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xscale('symlog', linthresh=1)
plt.show()
medium
A. A plot with x-axis on a linear scale only.
B. A plot with x-axis showing only positive values on a logarithmic scale, negative values ignored.
C. A runtime error because negative values cannot be plotted on log scale.
D. A plot with x-axis showing a linear scale near zero and logarithmic scale away from zero, including negative values.

Solution

  1. Step 1: Understand symlog scale behavior

    Symlog scale allows negative and zero values by using a linear region around zero defined by linthresh.
  2. Step 2: Analyze code and data

    The x values include negative, zero, and positive numbers. Setting x-axis to symlog with linthresh=1 means values between -1 and 1 are linear, outside are logarithmic.
  3. Final Answer:

    A plot with x-axis showing a linear scale near zero and logarithmic scale away from zero, including negative values. -> Option D
  4. Quick Check:

    Symlog handles negatives with linear region near zero [OK]
Hint: Symlog mixes linear near zero and log away, including negatives [OK]
Common Mistakes:
  • Expecting error due to negative values on log scale
  • Thinking symlog is fully logarithmic without linear region
  • Assuming negative values are ignored or cause crash
4. The following code throws a ValueError: "Data has no positive values, and therefore can not be log-scaled." How can you fix it?
import matplotlib.pyplot as plt
import numpy as np

x = np.array([-10, -5, -1, 0])
y = np.arange(4)

fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_xscale('log')
plt.show()
medium
A. Change ax.set_xscale('log') to ax.set_xscale('symlog').
B. Remove negative values from x before plotting.
C. Change y-axis scale instead of x-axis scale.
D. Use ax.set_xscale('linear') instead.

Solution

  1. Step 1: Identify cause of error

    Log scale requires positive values only. The x array has negative and zero values, causing the error.
  2. Step 2: Apply symlog scale to handle negatives

    Symlog scale supports negative and zero values by combining linear and log scales, so changing to symlog fixes the error.
  3. Final Answer:

    Change ax.set_xscale('log') to ax.set_xscale('symlog'). -> Option A
  4. Quick Check:

    Use symlog for negative values on log scale [OK]
Hint: Use symlog scale to plot negative or zero values on log axis [OK]
Common Mistakes:
  • Trying to plot negative values on log scale directly
  • Removing data instead of changing scale
  • Changing unrelated axis scale
5. You have a dataset with values ranging from -1000 to 1000, including zero. You want to plot it using matplotlib so that the axis shows a logarithmic scale for large values but remains linear near zero to avoid distortion. Which approach is best?
hard
A. Use ax.set_xscale('log') and filter out negative and zero values.
B. Use ax.set_xscale('symlog', linthresh=10) to combine linear and log scales.
C. Use ax.set_xscale('linear') because log scales cannot handle negatives.
D. Use ax.set_xscale('log') with linthresh=10 parameter.

Solution

  1. Step 1: Understand data range and requirements

    Data includes negative, zero, and positive values from -1000 to 1000. We want log scale for large values but linear near zero to avoid distortion.
  2. Step 2: Choose appropriate scale and parameters

    Log scale cannot handle negatives or zero. Symlog scale supports negatives and zero by mixing linear region near zero (controlled by linthresh) and log scale outside.
  3. Step 3: Evaluate options

    Use ax.set_xscale('symlog', linthresh=10) to combine linear and log scales. uses symlog with linthresh=10, which fits the requirement. Use ax.set_xscale('log') and filter out negative and zero values. filters data, losing information. Use ax.set_xscale('linear') because log scales cannot handle negatives. uses linear scale only, losing log benefits. Use ax.set_xscale('log') with linthresh=10 parameter. is invalid because log scale does not accept linthresh.
  4. Final Answer:

    Use ax.set_xscale('symlog', linthresh=10) to combine linear and log scales. -> Option B
  5. Quick Check:

    Symlog with linthresh mixes linear near zero and log away [OK]
Hint: Use symlog with linthresh to mix linear near zero and log away [OK]
Common Mistakes:
  • Trying to use log scale with negative values
  • Filtering out negative values instead of using symlog
  • Using linear scale only losing log benefits
  • Using invalid parameters with log scale
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