0
0
Matplotlibdata~10 mins

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

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