📖 Scenario: You are creating a simple line chart to show sales over a week. You want to make the chart easier to read by adding grid lines.
🎯 Goal: Build a line chart using matplotlib with grid lines enabled and customized.
📋 What You'll Learn
Create a list called days with the days of the week as strings.
Create a list called sales with sales numbers for each day.
Create a variable called grid_style to set the style of grid lines.
Plot the sales data using plt.plot().
Enable grid lines using plt.grid() with the style from grid_style.
Print the plot with grid lines visible.
💡 Why This Matters
🌍 Real World
Grid lines help people read charts more easily by showing reference lines behind the data points.
💼 Career
Data scientists and analysts often customize plots to make data clearer and more professional for reports and presentations.
Progress0 / 4 steps
1
Create sales data lists
Create a list called days with these exact values: 'Mon', 'Tue', 'Wed', 'Thu', 'Fri'. Create another list called sales with these exact values: 150, 200, 170, 220, 180.
Matplotlib
Hint
Use square brackets to create lists. Separate items with commas.
2
Set grid line style
Create a variable called grid_style and set it to the string '--' to represent dashed grid lines.
Matplotlib
Hint
Assign the string '--' to the variable grid_style.
3
Plot sales and enable grid lines
Import matplotlib.pyplot as plt. Use plt.plot() with days and sales to create the line chart. Then enable grid lines using plt.grid() with the argument linestyle=grid_style.
Matplotlib
Hint
Use import matplotlib.pyplot as plt to import. Use plt.plot() to plot. Use plt.grid(linestyle=grid_style) to add grid lines.
4
Display the plot
Use plt.show() to display the plot with the grid lines.
Matplotlib
Hint
Call plt.show() to open the plot window.
Practice
(1/5)
1. What does the plt.grid(True) command do in a matplotlib plot?
easy
A. It turns on the grid lines on the plot.
B. It hides the grid lines on the plot.
C. It changes the color of the grid lines to blue.
D. It saves the plot as an image file.
Solution
Step 1: Understand the plt.grid function
The plt.grid() function controls the visibility of grid lines on the plot.
Step 2: Effect of passing True
Passing True to plt.grid() turns the grid lines on, making them visible.
Final Answer:
It turns on the grid lines on the plot. -> Option A
Quick Check:
plt.grid(True) = grid visible [OK]
Hint: True means grid lines show, False hides them [OK]
Common Mistakes:
Thinking plt.grid(True) hides grid lines
Confusing grid color change with visibility
Assuming plt.grid(True) saves the plot
2. Which of the following is the correct syntax to set grid lines only on the y-axis in matplotlib?
easy
A. plt.grid(axis='y')
B. plt.grid(axis='xy')
C. plt.grid(axis='x')
D. plt.grid(axis='z')
Solution
Step 1: Understand the axis parameter in plt.grid
The axis parameter controls which axis the grid lines appear on: 'x', 'y', or 'both'.
Step 2: Correct value for y-axis grid lines
To show grid lines only on the y-axis, use axis='y'. Other options like 'xy' or 'z' are invalid.
Final Answer:
plt.grid(axis='y') -> Option A
Quick Check:
axis='y' shows horizontal grid lines [OK]
Hint: Use axis='y' to show grid lines horizontally only [OK]
Common Mistakes:
Using axis='xy' which is invalid
Using axis='x' to get y-axis grid lines
Using axis='z' which does not exist
3. What will be the effect of the following code snippet?
B. The axis='x' should be axis='y' to show x-axis grid lines.
C. true is undefined; use True (capitalized) instead.
D. Missing plt.show() to display the plot.
Solution
Step 1: Check plt.grid parameters
The code raises NameError because true is not defined in Python. Boolean true is True (capitalized).
Step 2: Correct way to enable grid on x-axis
Use plt.grid(True, axis='x') or plt.grid(visible=True, axis='x').
Final Answer:
true is undefined; use True (capitalized) instead. -> Option C
Quick Check:
plt.grid(True, axis='x') is correct [OK]
Hint: Booleans in Python are True/False (capitalized) [OK]
Common Mistakes:
Using lowercase 'true' which causes NameError
Confusing axis='x' with axis='y'
Thinking plt.grid can't be used with plt.plot
5. You want to create a plot with grid lines only on the y-axis, styled as dotted blue lines, but only for major ticks. Which code snippet achieves this correctly?
hard
A. plt.grid(axis='y', linestyle=':', color='blue', which='minor')
B. plt.grid(True, axis='y', linestyle=':', color='blue', which='major')
C. plt.grid(True, axis='y', linestyle='--', color='blue', which='minor')
D. plt.grid(True, axis='x', linestyle=':', color='blue', which='major')
Solution
Step 1: Understand parameters for grid customization
To show grid lines only on the y-axis, use axis='y'. For dotted lines, use linestyle=':'. To color them blue, use color='blue'. To apply only to major ticks, use which='major'.
Step 2: Verify each option
plt.grid(True, axis='y', linestyle=':', color='blue', which='major') matches all requirements. plt.grid(axis='y', linestyle=':', color='blue', which='minor') uses which='minor', incorrect. plt.grid(True, axis='y', linestyle='--', color='blue', which='minor') uses minor ticks and dashed lines, incorrect. plt.grid(True, axis='x', linestyle=':', color='blue', which='major') applies to x-axis, not y-axis.
Final Answer:
plt.grid(True, axis='y', linestyle=':', color='blue', which='major') -> Option B