0
0
Pandasdata~5 mins

Line plots with plot() in Pandas

Choose your learning style9 modes available
Introduction

Line plots help us see how data changes over time or order. They make trends easy to spot.

Tracking daily temperatures over a week.
Showing sales growth month by month.
Comparing stock prices over several days.
Visualizing how a student's grades change each semester.
Syntax
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.

Examples
Plot all numeric columns as lines using the DataFrame index for x-axis.
Pandas
df.plot()
Plot 'Sales' values over 'Month' values as a line.
Pandas
df.plot(x='Month', y='Sales')
Plot multiple columns 'Sales' and 'Profit' as separate lines.
Pandas
df.plot(kind='line', y=['Sales', 'Profit'])
Sample Program

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.

Pandas
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()
OutputSuccess
Important Notes

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.

Summary

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.