0
0
Matplotlibdata~10 mins

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

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