0
0
Matplotlibdata~15 mins

Axis scales (linear, log) in Matplotlib - Deep Dive

Choose your learning style9 modes available
Overview - Axis scales (linear, log)
What is it?
Axis scales control how data values are shown along the axes of a plot. The two common types are linear and logarithmic scales. A linear scale spaces values evenly, while a logarithmic scale spaces values based on their order of magnitude. This helps show data that changes very fast or covers a wide range.
Why it matters
Without axis scales like logarithmic, it is hard to see patterns in data that grows or shrinks exponentially. For example, population growth or earthquake magnitudes span large ranges. Using the right scale makes graphs easier to read and understand, helping people make better decisions based on data.
Where it fits
Before learning axis scales, you should know how to create basic plots with matplotlib. After this, you can learn about customizing ticks, labels, and advanced plotting techniques like multiple axes or interactive plots.
Mental Model
Core Idea
Axis scales transform how data values map to positions on a plot, changing the spacing to reveal different patterns.
Think of it like...
Imagine a ruler that can stretch or shrink between numbers: a normal ruler spaces numbers evenly, but a special ruler spaces numbers so that small numbers are far apart and big numbers are closer, helping you see tiny and huge things on the same line.
Linear scale: 0 ── 1 ── 2 ── 3 ── 4 ── 5
Log scale:    1 ── 10 ── 100 ── 1000 ── 10000

In linear, equal steps mean equal distance. In log, equal steps mean equal multiplication.
Build-Up - 7 Steps
1
FoundationUnderstanding linear axis scale basics
🤔
Concept: Linear scale spaces data points evenly along the axis.
In matplotlib, the default axis scale is linear. This means if you plot points at 1, 2, 3, 4, they appear equally spaced. For example, plotting y = x shows a straight line with equal gaps between points.
Result
A plot with evenly spaced ticks and data points along the axis.
Understanding linear scale is key because it matches how we usually count and measure things in everyday life.
2
FoundationPlotting with matplotlib linear scale
🤔
Concept: How to create a simple plot with linear scale using matplotlib.
import matplotlib.pyplot as plt x = [1, 2, 3, 4, 5] y = [1, 4, 9, 16, 25] plt.plot(x, y) plt.xlabel('X axis') plt.ylabel('Y axis') plt.title('Linear scale plot') plt.show()
Result
A line plot with x and y axes spaced evenly showing y = x squared.
Seeing the default linear scale plot helps you recognize the normal spacing of data points.
3
IntermediateIntroducing logarithmic axis scale
🤔Before reading on: do you think a log scale spaces numbers by addition or multiplication? Commit to your answer.
Concept: Logarithmic scale spaces data points based on multiplication, not addition.
A log scale means each step on the axis multiplies the previous value by a fixed number, usually 10. For example, ticks might be at 1, 10, 100, 1000 instead of 1, 2, 3, 4. This helps show data that changes exponentially or covers many orders of magnitude.
Result
Data points that differ by large factors appear spaced evenly, revealing patterns hidden in linear scale.
Knowing that log scale spaces by multiplication helps you understand why it reveals exponential growth or decay clearly.
4
IntermediateApplying log scale in matplotlib plots
🤔Before reading on: do you think setting log scale affects both axes or just one? Commit to your answer.
Concept: Matplotlib allows setting log scale on x-axis, y-axis, or both independently.
import matplotlib.pyplot as plt import numpy as np x = np.linspace(1, 1000, 100) y = x ** 2 plt.plot(x, y) plt.xscale('log') plt.yscale('log') plt.xlabel('Log X axis') plt.ylabel('Log Y axis') plt.title('Log-log scale plot') plt.show()
Result
A plot where both axes use log scale, showing a straight line for y = x squared.
Understanding how to set log scale on each axis lets you tailor plots to your data's nature.
5
IntermediateChoosing between linear and log scales
🤔Before reading on: do you think log scale is always better for large numbers? Commit to your answer.
Concept: Choosing the right scale depends on data distribution and what you want to highlight.
Linear scale is best for data with small ranges or additive changes. Log scale is best for data spanning many orders of magnitude or showing multiplicative changes. For example, population growth or sound intensity often use log scale.
Result
Better visual understanding of data patterns by choosing the appropriate scale.
Knowing when to use each scale prevents misleading or confusing plots.
6
AdvancedHandling zero and negative values in log scale
🤔Before reading on: do you think log scale can display zero or negative values? Commit to your answer.
Concept: Log scale cannot show zero or negative values because logarithm is undefined there.
If your data has zero or negative values, matplotlib will raise errors or ignore those points on log scale. You must filter or transform data, or use symlog scale which handles small negatives with linear behavior near zero.
Result
Understanding data limitations and choosing appropriate scales or transformations.
Knowing log scale limits helps avoid confusing errors and guides data preparation.
7
ExpertUsing symlog and custom log bases in matplotlib
🤔Before reading on: do you think log scale base can be changed easily? Commit to your answer.
Concept: Matplotlib supports symlog scale for data with negatives and custom log bases for flexibility.
import matplotlib.pyplot as plt import numpy as np x = np.linspace(-100, 100, 400) y = x ** 3 plt.plot(x, y) plt.yscale('symlog', linthresh=10) plt.xlabel('X axis') plt.ylabel('Symmetric log Y axis') plt.title('Symlog scale plot') plt.show() # Custom log base example plt.xscale('log', base=2) plt.show()
Result
Plots that handle negative values smoothly and use different log bases for specialized needs.
Understanding advanced scale options expands your ability to visualize complex data accurately.
Under the Hood
Matplotlib transforms data values to positions on the plot by applying a function to each value before drawing. For linear scale, the function is identity: position = value. For log scale, the function is logarithm: position = log(value). This changes spacing so that equal distances represent equal ratios, not equal differences. Internally, matplotlib calculates tick positions and labels based on these transformed values and maps them back to the original data for display.
Why designed this way?
Logarithmic scales were designed to handle data spanning many orders of magnitude, common in science and engineering. Linear scales are intuitive for everyday measurements. Matplotlib supports both to give users flexibility. The design balances simplicity for common cases and power for complex data, avoiding confusion by restricting log scale to positive values only.
Data values ──▶ [Scale function]
                      │
                      ▼
               Transformed positions
                      │
                      ▼
               Plot axis rendering

Scale function:
Linear: f(x) = x
Log:    f(x) = log(x)

Positions determine spacing of ticks and points.
Myth Busters - 4 Common Misconceptions
Quick: Does log scale mean the axis shows logarithm values instead of original data? Commit yes or no.
Common Belief:Log scale means the axis shows the logarithm of the data values instead of the original values.
Tap to reveal reality
Reality:The axis shows the original data values, but spaces them according to their logarithm. Tick labels remain in original scale, not log values.
Why it matters:Misunderstanding this leads to misreading graphs, thinking values are smaller or different than they really are.
Quick: Can log scale display zero or negative values? Commit yes or no.
Common Belief:Log scale can display zero and negative values just like linear scale.
Tap to reveal reality
Reality:Log scale cannot display zero or negative values because logarithm is undefined there.
Why it matters:Trying to plot such data on log scale causes errors or missing points, confusing users.
Quick: Is log scale always better for large numbers? Commit yes or no.
Common Belief:Log scale is always better than linear scale for large numbers.
Tap to reveal reality
Reality:Log scale is better only when data spans many orders of magnitude or grows multiplicatively. For small ranges or additive changes, linear scale is clearer.
Why it matters:Using log scale unnecessarily can distort data interpretation and hide important details.
Quick: Does setting log scale on one axis automatically apply it to the other? Commit yes or no.
Common Belief:Setting log scale on one axis automatically applies it to both axes.
Tap to reveal reality
Reality:Log scale must be set independently for each axis in matplotlib.
Why it matters:Assuming automatic application can cause unexpected plot appearances and confusion.
Expert Zone
1
Log scale tick placement is not uniform; matplotlib uses specialized locators to place ticks at powers and multiples, improving readability.
2
Symlog scale combines linear and log scales to handle data near zero, but choosing the linthresh parameter requires understanding data distribution.
3
Changing log base affects tick labels and spacing, useful in domains like computer science (base 2) or chemistry (base 10), but can confuse general audiences.
When NOT to use
Avoid log scale when data contains zero or negative values unless using symlog. Also avoid log scale if data range is narrow or differences are additive, as it can distort interpretation. Use linear or other transformations like root scales instead.
Production Patterns
In real-world data science, log scales are common in financial charts, scientific measurements, and machine learning feature visualization. Professionals often combine log scales with data filtering and annotation to clarify plots. Interactive tools let users switch scales dynamically to explore data.
Connections
Data normalization
Log scale is a form of nonlinear normalization that rescales data to reveal patterns.
Understanding axis scales helps grasp how normalization changes data representation for analysis.
Exponential growth and decay
Log scale linearizes exponential trends, making them easier to analyze and interpret.
Knowing log scales clarifies why exponential processes appear as straight lines on log plots.
Music pitch perception
Human perception of pitch is logarithmic, similar to log scales in plotting frequency data.
Recognizing logarithmic perception in music helps appreciate why log scales match natural senses in other fields.
Common Pitfalls
#1Trying to plot zero or negative values on a log scale axis.
Wrong approach:import matplotlib.pyplot as plt x = [0, 1, 10, 100] y = [1, 2, 3, 4] plt.plot(x, y) plt.xscale('log') plt.show()
Correct approach:import matplotlib.pyplot as plt x = [1, 10, 100] y = [1, 2, 3] plt.plot(x, y) plt.xscale('log') plt.show()
Root cause:Logarithm is undefined for zero and negative numbers, so these values cannot be shown on log scale.
#2Assuming log scale changes the data values shown on the axis labels.
Wrong approach:plt.xscale('log') # Then reading axis labels as log values instead of original data
Correct approach:plt.xscale('log') # Read axis labels as original data values spaced logarithmically
Root cause:Confusing axis transformation with data transformation leads to misinterpretation.
#3Setting log scale on one axis but expecting it on both axes automatically.
Wrong approach:plt.xscale('log') plt.plot(x, y) plt.show() # y axis remains linear
Correct approach:plt.xscale('log') plt.yscale('log') plt.plot(x, y) plt.show()
Root cause:Matplotlib requires explicit scale setting per axis; assuming global effect causes errors.
Key Takeaways
Axis scales control how data values map to positions on a plot, changing spacing to reveal different patterns.
Linear scale spaces values evenly, matching everyday counting and measurement.
Logarithmic scale spaces values by multiplication, useful for data spanning many orders of magnitude.
Log scale cannot display zero or negative values, requiring data filtering or alternative scales like symlog.
Choosing the right scale depends on data nature and what patterns you want to highlight.