Axis Scaling in Matplotlib
Learn how to control axis scales in Matplotlib. Understand linear, logarithmic, and symmetric logarithmic scales, customize axis transformations, and visualize data with different numerical ranges.
1. Learning Objectives
After completing this lesson, you will be able to:
- Understand axis scaling in Matplotlib.
- Understand the difference between linear and logarithmic scales.
- Use
plt.xscale()andplt.yscale(). - Create logarithmic plots using log scales.
- Understand symmetric logarithmic scaling.
- Use logit scaling for probability data.
- Apply axis scaling through the Object-Oriented API.
- Choose a suitable scale for different datasets.
2. Introduction to Axis Scaling
Axis scaling determines how data values are represented along the X-axis and Y-axis. It controls the relationship between numerical values and their positions in a chart.
Matplotlib uses a linear scale by default. In a linear scale, equal numerical differences are represented by equal distances on the axis.
However, some datasets contain values that differ by very large amounts. In these situations, logarithmic or other specialized scales can make the data easier to understand.
What is Axis Scaling?
Axis scaling is the method of mapping data values to positions on a chart. Different scaling methods can change how the same dataset appears without changing the original data.
Linear and logarithmic scales use different spacing for numerical values.
3. Linear Scale
A linear scale is the default axis scale in Matplotlib. In this scale, equal numerical differences are represented by equal distances along the axis.
For example, the distance between 10 and 20 is the same as the distance between 20 and 30.
Example: Linear Scale
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 30, 40, 50]
plt.plot(x, y, marker="o")
plt.xscale("linear")
plt.yscale("linear")
plt.title("Linear Axis Scale")
plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.show()
Expected Output
A line chart displays five data points.
Both X-axis and Y-axis use linear scaling.
Equal numerical differences have equal spacing
on their respective axes.
Understanding Linear Scale
| Value | Difference from Previous Value | Axis Spacing |
|---|---|---|
| 10 | - | Starting position |
| 20 | 10 | Equal distance |
| 30 | 10 | Equal distance |
| 40 | 10 | Equal distance |
| 50 | 10 | Equal distance |
4. Logarithmic Scale
A logarithmic scale represents values using logarithmic spacing rather than equal numerical differences. It is useful when data contains values that vary across several orders of magnitude.
In a base-10 logarithmic scale, the main tick positions commonly represent values such as 1, 10, 100, 1000, and 10000.
Why Use a Logarithmic Scale?
A logarithmic scale helps visualize datasets containing both small and very large positive values. It compresses large numerical ranges so that important patterns can be easier to compare.
Example: Logarithmic Y-Axis
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6]
y = [10, 100, 1000, 10000, 100000, 1000000]
plt.plot(x, y, marker="o")
plt.yscale("log")
plt.title("Logarithmic Y-Axis")
plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.show()
Expected Output
A line chart displays values from 10 to 1,000,000.
The Y-axis uses logarithmic scaling.
The tick positions represent logarithmic intervals.
Large values are displayed without requiring
a proportionally huge vertical plotting area.
A conceptual logarithmic Y-axis showing successive powers of ten.
5. Setting Logarithmic Scale Using xscale()
The plt.xscale() function changes the scaling method
of the X-axis.
You can use "log" to apply logarithmic scaling to
the horizontal axis.
Syntax
plt.xscale("log")
Example: Logarithmic X-Axis
import matplotlib.pyplot as plt
x = [1, 10, 100, 1000, 10000]
y = [10, 20, 30, 40, 50]
plt.plot(x, y, marker="o")
plt.xscale("log")
plt.title("Logarithmic X-Axis")
plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.show()
Expected Output
A line chart displays five data points.
The X-axis uses logarithmic scaling.
The X-axis values are 1, 10, 100, 1000, and 10000.
The Y-axis uses its default linear scale.
6. Setting Logarithmic Scale Using yscale()
The plt.yscale() function changes the scaling method
of the Y-axis.
It is useful when vertical data values cover a very large numerical range.
Syntax
plt.yscale("log")
Example: Logarithmic Y-Axis
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 10, 100, 1000, 10000]
plt.plot(x, y, marker="o", color="green")
plt.yscale("log")
plt.title("Logarithmic Y-Axis")
plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.show()
Expected Output
A green line chart displays values from 1 to 10,000.
The Y-axis uses logarithmic scaling.
The X-axis remains linear.
xscale() for horizontal axis scaling
and yscale() for vertical axis scaling.
7. Logarithmic Scale on Both Axes
Matplotlib allows you to apply logarithmic scaling to both the X-axis and Y-axis. This is commonly called a log-log plot.
A log-log plot is useful when both variables cover several orders of magnitude.
Example: Log-Log Plot
import matplotlib.pyplot as plt
x = [1, 10, 100, 1000, 10000]
y = [10, 100, 1000, 10000, 100000]
plt.plot(x, y, marker="o", color="purple")
plt.xscale("log")
plt.yscale("log")
plt.title("Log-Log Plot")
plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.show()
Expected Output
A line chart displays data using logarithmic scaling
on both axes.
The X-axis and Y-axis use logarithmic intervals.
The chart shows the relationship between values
across multiple orders of magnitude.
8. Logarithmic Scale with Different Bases
Matplotlib supports logarithmic scales with different bases.
You can configure the logarithmic base using the base
parameter.
Base 10 is commonly used, but base 2 and base 3 can also be useful for particular datasets.
Example: Base 2 Logarithmic Scale
import matplotlib.pyplot as plt
x = [1, 2, 4, 8, 16, 32, 64]
y = [1, 2, 3, 4, 5, 6, 7]
plt.plot(x, y, marker="o")
plt.xscale("log", base=2)
plt.title("Base 2 Logarithmic Scale")
plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.show()
Expected Output
A line chart displays X values using base 2 logarithmic scaling.
The X-axis uses powers of 2:
1, 2, 4, 8, 16, 32, and 64.
The Y-axis remains linear.
Example: Base 10 Logarithmic Scale
import matplotlib.pyplot as plt
x = [1, 10, 100, 1000, 10000]
y = [1, 2, 3, 4, 5]
plt.plot(x, y, marker="o")
plt.xscale("log", base=10)
plt.title("Base 10 Logarithmic Scale")
plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.show()
Expected Output
The X-axis uses base 10 logarithmic scaling.
The major values include 1, 10, 100, 1000, and 10000.
9. Symmetric Logarithmic Scale
A normal logarithmic scale cannot represent zero or negative values. However, some datasets contain positive, negative, and zero values.
Matplotlib provides the symmetric logarithmic scale, also known as
symlog, to handle this type of data.
The symlog scale uses a linear region around zero and logarithmic behavior outside that region.
Example: Symmetric Logarithmic Scale
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6, 7]
y = [-1000, -100, -10, 0, 10, 100, 1000]
plt.plot(x, y, marker="o")
plt.yscale("symlog", linthresh=10)
plt.title("Symmetric Logarithmic Scale")
plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.show()
Expected Output
A line chart displays negative, zero, and positive values.
The Y-axis uses symmetric logarithmic scaling.
A linear region is maintained around zero.
Values outside the linear region use logarithmic behavior.
Conceptual illustration of a symmetric logarithmic scale with a linear region around zero.
symlog when your data contains
both negative and positive values and you need logarithmic behavior
outside a central linear region.
10. Logit Scale
Matplotlib also supports a logit scale. It is useful for probability values that lie between 0 and 1.
Logit scaling is commonly used when visualizing probabilities that are very close to zero or very close to one.
Example: Logit Y-Axis
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6]
probability = [0.01, 0.05, 0.1, 0.5, 0.9, 0.99]
plt.plot(x, probability, marker="o")
plt.yscale("logit")
plt.title("Logit Scale")
plt.xlabel("X Values")
plt.ylabel("Probability")
plt.show()
Expected Output
A line chart displays probability values.
The Y-axis uses logit scaling.
Values close to zero and one are represented
using the logit transformation.
11. Axis Scaling Using the Object-Oriented API
You can also configure axis scales using the Object-Oriented API.
The Axes object provides set_xscale() and
set_yscale() methods.
| Pyplot Function | Axes Method | Purpose |
|---|---|---|
plt.xscale() |
ax.set_xscale() |
Set X-axis scaling. |
plt.yscale() |
ax.set_yscale() |
Set Y-axis scaling. |
Example: Logarithmic Scaling with Axes
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = [1, 10, 100, 1000, 10000]
y = [10, 20, 30, 40, 50]
ax.plot(x, y, marker="o", color="blue")
ax.set_xscale("log")
ax.set_title("Object-Oriented Log Scale")
ax.set_xlabel("X Values")
ax.set_ylabel("Y Values")
plt.show()
Expected Output
A line chart is displayed.
The X-axis uses logarithmic scaling.
The Y-axis remains linear.
The scale is configured through the Axes object.
Example: Configure Both Axes
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = [1, 10, 100, 1000, 10000]
y = [10, 100, 1000, 10000, 100000]
ax.plot(x, y, marker="o")
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_title("Log-Log Plot Using Axes")
ax.set_xlabel("X Values")
ax.set_ylabel("Y Values")
plt.show()
Expected Output
A line chart uses logarithmic scaling on both axes.
The X-axis and Y-axis display logarithmic intervals.
Both scales are configured through the Axes object.
12. Comparing Different Axis Scales
Different axis scales are suitable for different types of data. Choosing the correct scale helps communicate the information accurately.
| Scale | Behavior | Suitable Data |
|---|---|---|
| Linear | Equal numerical differences have equal spacing. | Regular numerical datasets. |
| Logarithmic | Equal ratios have equal spacing. | Large positive numerical ranges. |
| Symmetric Logarithmic | Linear near zero and logarithmic outside the central region. | Positive, negative, and zero values. |
| Logit | Uses a logit transformation. | Probability values between 0 and 1. |
Example: Compare Linear and Logarithmic Scales
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5, 6]
y = [10, 100, 1000, 10000, 100000, 1000000]
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
axes[0].plot(x, y, marker="o")
axes[0].set_title("Linear Scale")
axes[0].set_xlabel("X Values")
axes[0].set_ylabel("Y Values")
axes[1].plot(x, y, marker="o")
axes[1].set_yscale("log")
axes[1].set_title("Logarithmic Scale")
axes[1].set_xlabel("X Values")
axes[1].set_ylabel("Y Values")
plt.tight_layout()
plt.show()
Expected Output
Two charts are displayed side by side.
The first chart uses a linear Y-axis.
The second chart uses a logarithmic Y-axis.
Both charts display the same dataset.
The logarithmic chart makes the different orders
of magnitude easier to compare.
13. Common Mistakes
| Mistake | Explanation | Correct Approach |
|---|---|---|
| Using log scale with zero | A standard logarithmic scale cannot represent zero. | Use positive values or consider symlog. |
| Using log scale with negative values | Negative values are outside the standard log domain. | Use symlog when negative values are required. |
| Choosing the wrong scale | The selected scale may make the data difficult to interpret. | Understand the dataset before selecting a scale. |
| Confusing limits with scaling | Limits control the visible range, while scaling controls value transformation. | Use the correct axis method for each requirement. |
| Using logit with zero or one | The logit transformation is not finite at these endpoints. | Use values strictly between zero and one. |
14. Practical Activity
Activity: Compare Website Growth
Create a chart showing website traffic growth over six months. Compare the same dataset using linear and logarithmic scaling.
Requirements
- Create six monthly traffic values.
- Display the data using a line chart.
- Create one chart using linear scaling.
- Create another chart using logarithmic Y-axis scaling.
- Add titles and axis labels to both charts.
- Compare the appearance of both charts.
Sample Solution
import matplotlib.pyplot as plt
months = [1, 2, 3, 4, 5, 6]
traffic = [100, 500, 2000, 10000, 50000, 250000]
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
axes[0].plot(months, traffic, marker="o", color="blue")
axes[0].set_title("Website Traffic - Linear")
axes[0].set_xlabel("Months")
axes[0].set_ylabel("Visitors")
axes[1].plot(months, traffic, marker="o", color="green")
axes[1].set_yscale("log")
axes[1].set_title("Website Traffic - Logarithmic")
axes[1].set_xlabel("Months")
axes[1].set_ylabel("Visitors")
plt.tight_layout()
plt.show()
Expected Output
Two charts display website traffic growth.
The first chart uses a linear Y-axis.
The second chart uses a logarithmic Y-axis.
Both charts display the same traffic data.
The logarithmic chart makes growth across
different numerical magnitudes easier to inspect.
15. Practice Questions
Basic Questions
- What is axis scaling in Matplotlib?
- Which scale is used by default?
- What is a logarithmic scale?
- What is the purpose of
plt.xscale()? - What is the purpose of
plt.yscale()?
Intermediate Questions
- What is the difference between linear and logarithmic scaling?
- What is a log-log plot?
- Why can a standard logarithmic scale not display zero?
- What is symmetric logarithmic scaling?
- When should you use a logit scale?
Practical Questions
- Create a line plot using linear scaling.
- Create a chart using a logarithmic X-axis.
- Create a chart using a logarithmic Y-axis.
- Create a log-log plot using both axes.
- Compare linear and logarithmic charts using subplots.
- Create a symmetric logarithmic chart with negative and positive values.
Summary
Axis scaling determines how numerical values are represented along the X-axis and Y-axis. Matplotlib uses linear scaling by default, where equal numerical differences have equal distances on the chart.
Logarithmic scaling is useful when data covers several orders of magnitude. The xscale() and yscale() functions allow you to apply logarithmic scaling to individual axes, while applying it to both axes creates a log-log plot.
Matplotlib also provides symmetric logarithmic scaling for datasets containing negative, zero, and positive values. Logit scaling is useful for probability values strictly between zero and one.
The Object-Oriented API provides set_xscale() and set_yscale() methods for configuring axis scales through an Axes object. Choosing the correct scale helps represent data accurately and makes charts easier to interpret.