📖 Scenario: You are analyzing sales data that grows exponentially. You want to see how the sales look on a normal scale and on a logarithmic scale to better understand the growth pattern.
🎯 Goal: Create a simple line plot of sales data using matplotlib. First, plot the data with a linear scale on the y-axis. Then, change the y-axis scale to logarithmic to compare the views.
📋 What You'll Learn
Create a list called sales with the values: 10, 100, 1000, 10000, 100000
Create a list called months with the values: 1, 2, 3, 4, 5
Create a variable called y_scale and set it to the string 'linear'
Use matplotlib to plot months vs sales with the y-axis scale set to y_scale
Change the value of y_scale to 'log' and plot the same data again
Print the plots so the output shows both linear and logarithmic scale plots
💡 Why This Matters
🌍 Real World
Scientists and business analysts often use logarithmic scales to better understand data that grows exponentially or covers a wide range.
💼 Career
Knowing how to change axis scales in plots is important for data visualization roles and helps communicate insights clearly.
Progress0 / 4 steps
1
Create the sales and months data lists
Create a list called sales with the values 10, 100, 1000, 10000, and 100000. Also create a list called months with the values 1, 2, 3, 4, and 5.
Matplotlib
Hint
Use square brackets [] to create lists with the exact numbers given.
2
Set the initial y-axis scale to linear
Create a variable called y_scale and set it to the string 'linear'.
Matplotlib
Hint
Assign the string 'linear' to the variable y_scale.
3
Plot the sales data with the y-axis scale set to y_scale
Import matplotlib.pyplot as plt. Use plt.plot() to plot months on the x-axis and sales on the y-axis. Then set the y-axis scale using plt.yscale(y_scale). Finally, add a title 'Sales Data with Linear Scale' and call plt.show() to display the plot.
Matplotlib
Hint
Remember to import matplotlib.pyplot as plt. Use plt.plot() and plt.yscale() to set the scale.
4
Change y_scale to 'log' and plot again
Change the value of y_scale to the string 'log'. Then plot months vs sales again using plt.plot(), set the y-axis scale with plt.yscale(y_scale), add the title 'Sales Data with Logarithmic Scale', and call plt.show() to display the plot.
Matplotlib
Hint
Assign 'log' to y_scale and repeat the plotting steps with the new scale.
Practice
(1/5)
1. What does setting the axis scale to log do in a matplotlib plot?
easy
A. It shows data spaced by powers of 10, useful for wide-ranging values.
B. It reverses the axis direction.
C. It colors the axis labels differently.
D. It removes the axis ticks.
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 A
Quick Check:
Log scale = spaced by powers of 10 [OK]
Hint: Log scale spaces data by powers of 10, not evenly [OK]
Common Mistakes:
Thinking log scale reverses axis
Confusing log scale with color changes
Assuming log scale removes ticks
2. Which of the following is the correct way to set the y-axis to a logarithmic scale in matplotlib?
easy
A. plt.yaxis('log')
B. plt.axis('log')
C. plt.set_yscale('log')
D. plt.yscale('log')
Solution
Step 1: Recall matplotlib axis scale syntax
The correct function to set axis scale is plt.yscale() for y-axis.
Step 2: Check correct usage
Calling plt.yscale('log') sets y-axis to logarithmic scale.
Final Answer:
plt.yscale('log') -> Option D
Quick Check:
Use plt.yscale('log') to set y-axis log scale [OK]
Hint: Use plt.yscale('log') to set y-axis log scale [OK]
Common Mistakes:
Using plt.axis('log') which is invalid
Trying plt.set_yscale which does not exist
Using plt.yaxis('log') which is incorrect
3. What will be the y-axis tick labels when running this code?
import matplotlib.pyplot as plt
plt.plot([1, 10, 100, 1000])
plt.yscale('log')
plt.show()
medium
A. [1, 10, 100, 1000]
B. [0, 1, 2, 3]
C. [1, 2, 3, 4]
D. [10, 100, 1000, 10000]
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 A
Quick Check:
Log scale ticks = powers of 10 matching data [OK]
Hint: Log scale ticks match powers of 10 in data [OK]
Common Mistakes:
Expecting linear spaced ticks like 0,1,2,3
Confusing tick labels with data indices
Assuming ticks multiply by 10 beyond data range
4. Identify the error in this code that tries to set the x-axis to logarithmic scale:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.xscale('logarithmic')
plt.show()
medium
A. The method name should be plt.set_xscale()
B. The scale argument should be 'log', not 'logarithmic'
C. plt.plot() cannot be used with log scale
D. Missing plt.show() call
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 B
Quick Check:
Use 'log' as argument in plt.xscale() [OK]
Hint: Use 'log' exactly as argument for log scale [OK]
Common Mistakes:
Using 'logarithmic' instead of 'log'
Thinking plt.set_xscale() is needed
Believing plt.plot() can't work with log scale
5. You have data ranging from 0.001 to 1000. Which axis scale setting will best show all data clearly on a plot?
hard
A. Use linear scale on both axes
B. Use linear scale on x-axis and log scale on y-axis
C. Use log scale on both axes
D. Use log scale on x-axis and linear scale on y-axis
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 C
Quick Check:
Wide range data = log scale both axes [OK]
Hint: Wide range data needs log scale on both axes [OK]
Common Mistakes:
Using linear scale hides small values
Mixing linear and log scales without reason
Assuming linear scale works for all ranges
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.