Axis scales help us show data clearly. Linear scale shows data evenly. Log scale shows data that changes a lot in a smaller space.
0
0
Axis scales (linear, log) in Matplotlib
Introduction
When you want to show normal data that changes evenly.
When your data has very big and very small values together.
When you want to see percentage changes or growth rates clearly.
When you want to compare data that grows exponentially.
When you want to avoid big values hiding small values on the graph.
Syntax
Matplotlib
import matplotlib.pyplot as plt plt.plot(x, y) plt.xscale('linear' or 'log') plt.yscale('linear' or 'log') plt.show()
Use 'linear' for normal scale and 'log' for logarithmic scale.
You can set scale for x-axis, y-axis, or both.
Examples
Both axes use normal linear scale.
Matplotlib
plt.xscale('linear') plt.yscale('linear')
X-axis uses log scale, Y-axis uses linear scale.
Matplotlib
plt.xscale('log') plt.yscale('linear')
Both axes use log scale, good for data with wide range.
Matplotlib
plt.xscale('log') plt.yscale('log')
Sample Program
This code plots the function y = x² twice. The first plot uses normal linear scales on both axes. The second plot uses logarithmic scales on both axes. This helps see how the data grows quickly and fits better on the graph.
Matplotlib
import matplotlib.pyplot as plt import numpy as np x = np.linspace(1, 100, 100) y = x ** 2 plt.figure(figsize=(10, 4)) plt.subplot(1, 2, 1) plt.plot(x, y) plt.title('Linear scale') plt.xscale('linear') plt.yscale('linear') plt.xlabel('x') plt.ylabel('y = x^2') plt.subplot(1, 2, 2) plt.plot(x, y) plt.title('Log scale') plt.xscale('log') plt.yscale('log') plt.xlabel('x (log scale)') plt.ylabel('y = x^2 (log scale)') plt.tight_layout() plt.show()
OutputSuccess
Important Notes
Log scale cannot show zero or negative values. Make sure data is positive.
Log scale helps to see multiplicative changes clearly.
You can combine linear and log scales on different axes for better visualization.
Summary
Linear scale shows data evenly spaced.
Log scale shows data on a multiplicative scale, useful for wide ranges.
Use plt.xscale() and plt.yscale() to set axis scales in matplotlib.