📖 Scenario: You work as a data analyst for a small bakery. You have daily sales data for different types of bread over a week. You want to visualize how sales changed each day to understand trends.
🎯 Goal: Create a line plot using pandas plot() to show daily sales of different bread types over a week.
📋 What You'll Learn
Create a pandas DataFrame with daily sales data for three bread types.
Set a variable for the days of the week.
Use the DataFrame's plot() method to create a line plot.
Display the plot using plt.show().
💡 Why This Matters
🌍 Real World
Line plots help visualize trends over time, such as daily sales, temperature changes, or stock prices.
💼 Career
Data analysts and scientists use line plots to communicate patterns and insights clearly to teams and decision makers.
Progress0 / 4 steps
1
Create the sales data DataFrame
Import pandas as pd and create a DataFrame called sales_data with these exact columns and values: 'Sourdough' with [20, 22, 19, 24, 30, 28, 25], 'Baguette' with [15, 18, 14, 20, 22, 21, 19], and 'Rye' with [10, 12, 11, 13, 15, 14, 13].
Pandas
Hint
Use pd.DataFrame() with a dictionary where keys are bread types and values are lists of sales numbers.
2
Add days of the week as index
Create a list called days with these exact values: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']. Then set this list as the index of the sales_data DataFrame.
Pandas
Hint
Assign the list days to sales_data.index to label rows by day.
3
Create the line plot
Import matplotlib.pyplot as plt. Use the plot() method on sales_data to create a line plot showing sales trends for each bread type.
Pandas
Hint
Call sales_data.plot() to create the line plot. Make sure to import matplotlib.pyplot as plt.
4
Display the plot
Use plt.show() to display the line plot you created.
Pandas
Hint
Call plt.show() after plotting to see the graph window.
Practice
(1/5)
1. What does the plot() function in pandas primarily create?
easy
A. A list of column names
B. A table of data values
C. A summary statistics report
D. A line plot to show data trends over an axis
Solution
Step 1: Understand the purpose of plot() in pandas
The plot() function is used to create visual graphs from data, most commonly line plots.
Step 2: Identify the type of plot created
By default, DataFrame.plot() creates a line plot to show how data changes over an ordered axis like time.
Final Answer:
A line plot to show data trends over an axis -> Option D
Quick Check:
plot() = line plot [OK]
Hint: plot() makes line plots by default to show trends [OK]
Common Mistakes:
Thinking plot() creates tables
Confusing plot() with summary functions
Assuming plot() lists columns
2. Which of the following is the correct syntax to plot the column 'Sales' from a DataFrame df using the y parameter?
easy
A. df.plot('Sales')
B. df.plot(y='Sales')
C. df.plot(column='Sales')
D. df.plot(x='Sales')
Solution
Step 1: Recall the parameter to select columns in plot()
The y parameter specifies which column(s) to plot on the y-axis.
Step 2: Match the correct syntax
Using df.plot(y='Sales') correctly tells pandas to plot the 'Sales' column as the y-axis data.
Final Answer:
df.plot(y='Sales') -> Option B
Quick Check:
Use y='column' to plot that column [OK]
Hint: Use y='column' to plot that column in plot() [OK]
Common Mistakes:
Using 'column' instead of 'y' parameter
Passing column name without parameter
Using x='Sales' to plot y-axis
3. Given the code:
import pandas as pd
import matplotlib.pyplot as plt
data = {'Month': ['Jan', 'Feb', 'Mar'], 'Sales': [100, 150, 120]}
df = pd.DataFrame(data)
df.plot(x='Month', y='Sales')
plt.show()
What will the plot show?
medium
A. A scatter plot of sales vs months
B. A bar plot with sales on x-axis and months on y-axis
C. A line plot with months on x-axis and sales on y-axis
D. An error because 'Month' is not numeric
Solution
Step 1: Understand the plot parameters
The code uses x='Month' and y='Sales' to plot sales against months.
Step 2: Recognize default plot type
By default, plot() creates a line plot, even if x-axis is categorical like months.
Final Answer:
A line plot with months on x-axis and sales on y-axis -> Option C
Quick Check:
plot(x=cat, y=num) = line plot [OK]
Hint: plot() defaults to line plot even with categorical x-axis [OK]
Common Mistakes:
Expecting bar or scatter plot by default
Thinking non-numeric x causes error
Confusing axis labels
4. What is wrong with this code snippet?
df = pd.DataFrame({'A': [1, 2, 3]})
df.plot('A')
medium
A. It will plot column 'A' on y-axis but x-axis will be default index
B. It will cause a syntax error because 'y' parameter is missing
C. It will raise a runtime error because 'A' is not specified as y
D. It will plot column 'A' on x-axis instead of y-axis
Solution
Step 1: Analyze the plot() call with one positional string argument
When a string is passed positionally to plot(), it specifies the column for the y-axis.
Step 2: Understand default x-axis behavior
Since no x parameter is specified, the x-axis uses the DataFrame's index.
Final Answer:
It will plot column 'A' on y-axis but x-axis will be default index -> Option A
Quick Check:
plot('A') = y='A' vs index [OK]
Hint: plot('col') sets y='col' vs index; specify x/y to control [OK]
Common Mistakes:
Assuming 'A' is x-axis without specifying
Expecting syntax or runtime error
Confusing x and y parameters
5. You have a DataFrame df with columns 'Date', 'Temperature', and 'Humidity'. You want to plot both 'Temperature' and 'Humidity' over 'Date' on the same line plot with a title 'Weather Data'. Which code correctly does this?
hard
A. df.plot(x='Date', y=['Temperature', 'Humidity'], title='Weather Data')
B. df.plot(y=['Temperature', 'Humidity'], x='Date', label='Weather Data')
C. df.plot(x='Date', y='Temperature', 'Humidity', title='Weather Data')
D. df.plot(x='Date', y=['Temperature', 'Humidity']).title('Weather Data')
Solution
Step 1: Select multiple columns for y-axis correctly
Use a list for the y parameter: y=['Temperature', 'Humidity'].
Step 2: Add title properly in plot()
The title parameter inside plot() sets the plot title directly.
Step 3: Verify syntax correctness
df.plot(x='Date', y=['Temperature', 'Humidity'], title='Weather Data') uses correct syntax with x, y as list, and title parameter all in one call.
Final Answer:
df.plot(x='Date', y=['Temperature', 'Humidity'], title='Weather Data') -> Option A
Quick Check:
Use y=[cols], x=col, title='text' in plot() [OK]
Hint: Use y=[cols], x=col, title='text' inside plot() [OK]
Common Mistakes:
Passing multiple y columns without list
Using label instead of title for plot title
Using .title() instead of .set_title()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.