Line Plot
Python · Data Visualization
Theory
A line plot joins data points in order, so the slope of the line shows how a value changes from one step to the next. It is the standard chart for time series: monthly sales, daily temperature, website visits. ax.plot(x, y) draws one line; calling it again on the same Axes adds another, and label='Online' plus ax.legend() tells them apart.
Styling arguments control the look: marker='o' shows each point, linestyle='--' makes a dashed line, linewidth sets thickness and color picks the colour. The x values should be ordered, otherwise the line zig-zags back and forth. With five months of online sales [120, 135, 160, 150, 190], the line rises, dips in April and jumps in May, and the size of each step is the month-on-month change.
Why we use it
Trends, seasonality and sudden changes are easiest to see as a line. Comparing several lines on one Axes shows which series grows faster or when they cross, something hard to read from a table.
When to use it
Use a line plot when the x-axis is ordered, especially time. For unordered categories (cities, products) use a bar chart, and for the relationship between two measured variables use a scatter plot. Keep to a handful of lines so the chart stays readable.
How it works
- Ordered x values and one or more y series
- Is x ordered (time, sequence)? Yes: Line plot fits. No: Use bar (categories) or scatter (two measures).
- ax.plot(x, y, label=...) for each series
- Add markers, axis labels and ax.legend()
- Trend chart: read slopes and crossings
Syntax
ax.plot(x, y, label='name', marker='o', linestyle='-', linewidth=2, color='tab:blue')
ax.legend()Example
months = ['Jan', 'Feb', 'Mar']
fig, ax = plt.subplots()
ax.plot(months, [120, 135, 160], marker='o', label='Online')
ax.legend()Key points
- Best for ordered x values such as dates or months
- Call ax.plot again to add another line on the same Axes
- label= plus ax.legend() names each line
- marker='o' shows the actual data points
- Unsorted x values make the line zig-zag