Line plots help us see how data changes over time or order. They make trends easy to spot.
Line plots with plot() in Pandas
DataFrame.plot(kind='line', x=None, y=None, title=None)
kind='line' tells pandas to make a line plot (default).
x and y specify which columns to use for the horizontal and vertical axes.
df.plot()
df.plot(x='Month', y='Sales')
df.plot(kind='line', y=['Sales', 'Profit'])
This code creates a small table of sales data by month. Then it draws a line plot showing sales for each month. Markers highlight each data point.
import pandas as pd import matplotlib.pyplot as plt # Create sample data sales_data = { 'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'], 'Sales': [200, 220, 250, 270, 300], 'Profit': [50, 55, 60, 65, 70] } df = pd.DataFrame(sales_data) # Plot Sales over Month ax = df.plot(x='Month', y='Sales', title='Monthly Sales', marker='o') plt.ylabel('Sales') plt.show()
If you don't specify x, pandas uses the DataFrame index for the horizontal axis.
You can add markers to points with marker='o' to see exact values.
Use plt.show() to display the plot when running in scripts.
Line plots show how data changes over an ordered axis like time.
Use df.plot(x=..., y=...) to pick which columns to plot.
Markers and titles make plots easier to understand.
Practice
plot() function in pandas primarily create?Solution
Step 1: Understand the purpose of
Theplot()in pandasplot()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 DQuick Check:
plot() = line plot [OK]
- Thinking plot() creates tables
- Confusing plot() with summary functions
- Assuming plot() lists columns
df using the y parameter?Solution
Step 1: Recall the parameter to select columns in plot()
Theyparameter specifies which column(s) to plot on the y-axis.Step 2: Match the correct syntax
Usingdf.plot(y='Sales')correctly tells pandas to plot the 'Sales' column as the y-axis data.Final Answer:
df.plot(y='Sales') -> Option BQuick Check:
Use y='column' to plot that column [OK]
- Using 'column' instead of 'y' parameter
- Passing column name without parameter
- Using x='Sales' to plot y-axis
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?
Solution
Step 1: Understand the plot parameters
The code usesx='Month'andy='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 CQuick Check:
plot(x=cat, y=num) = line plot [OK]
- Expecting bar or scatter plot by default
- Thinking non-numeric x causes error
- Confusing axis labels
df = pd.DataFrame({'A': [1, 2, 3]})
df.plot('A')Solution
Step 1: Analyze the plot() call with one positional string argument
When a string is passed positionally toplot(), it specifies the column for the y-axis.Step 2: Understand default x-axis behavior
Since noxparameter 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 AQuick Check:
plot('A') = y='A' vs index [OK]
- Assuming 'A' is x-axis without specifying
- Expecting syntax or runtime error
- Confusing x and y parameters
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?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()
Thetitleparameter insideplot()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 AQuick Check:
Use y=[cols], x=col, title='text' in plot() [OK]
- Passing multiple y columns without list
- Using label instead of title for plot title
- Using .title() instead of .set_title()
Ready to prove it?
Pass the test and this pattern moves unknown
