Axis scales help us show data clearly. Linear scale shows data evenly. Log scale shows data that changes a lot in a smaller space.
Axis scales (linear, log) in 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.
plt.xscale('linear') plt.yscale('linear')
plt.xscale('log') plt.yscale('linear')
plt.xscale('log') plt.yscale('log')
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.
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()
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.
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.
Practice
log do in a matplotlib plot?Solution
Step 1: Understand axis scale types
Linear scale spaces data evenly, while log scale spaces data by powers of 10.Step 2: Effect of log scale on data display
Log scale compresses large ranges, showing exponential growth clearly.Final Answer:
It shows data spaced by powers of 10, useful for wide-ranging values. -> Option AQuick Check:
Log scale = spaced by powers of 10 [OK]
- Thinking log scale reverses axis
- Confusing log scale with color changes
- Assuming log scale removes ticks
Solution
Step 1: Recall matplotlib axis scale syntax
The correct function to set axis scale isplt.yscale()for y-axis.Step 2: Check correct usage
Callingplt.yscale('log')sets y-axis to logarithmic scale.Final Answer:
plt.yscale('log') -> Option DQuick Check:
Use plt.yscale('log') to set y-axis log scale [OK]
- Using plt.axis('log') which is invalid
- Trying plt.set_yscale which does not exist
- Using plt.yaxis('log') which is incorrect
import matplotlib.pyplot as plt
plt.plot([1, 10, 100, 1000])
plt.yscale('log')
plt.show()Solution
Step 1: Understand the data and scale
The data points are 1, 10, 100, 1000 and y-axis is set to log scale.Step 2: Effect of log scale on ticks
Log scale shows ticks at powers of 10 matching data values, so ticks will be 1, 10, 100, 1000.Final Answer:
[1, 10, 100, 1000] -> Option AQuick Check:
Log scale ticks = powers of 10 matching data [OK]
- Expecting linear spaced ticks like 0,1,2,3
- Confusing tick labels with data indices
- Assuming ticks multiply by 10 beyond data range
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.xscale('logarithmic')
plt.show()Solution
Step 1: Check method and argument correctness
The method plt.xscale() is correct, but the argument must be 'log' for logarithmic scale.Step 2: Identify the wrong argument
The code uses 'logarithmic' which is invalid and causes an error.Final Answer:
The scale argument should be 'log', not 'logarithmic' -> Option BQuick Check:
Use 'log' as argument in plt.xscale() [OK]
- Using 'logarithmic' instead of 'log'
- Thinking plt.set_xscale() is needed
- Believing plt.plot() can't work with log scale
Solution
Step 1: Analyze data range
The data spans from 0.001 to 1000, a wide range over several orders of magnitude.Step 2: Choose scale to display wide range clearly
Log scale compresses large ranges and shows small and large values clearly on the same plot.Step 3: Decide axis scales
Using log scale on both axes best displays wide-ranging data without distortion.Final Answer:
Use log scale on both axes -> Option CQuick Check:
Wide range data = log scale both axes [OK]
- Using linear scale hides small values
- Mixing linear and log scales without reason
- Assuming linear scale works for all ranges
Ready to prove it?
Pass the test and this pattern moves unknown
