A line plot connects data points with lines and is the best chart for showing how a value changes over time or along an ordered axis. In Matplotlib you draw it with plt.plot().
What You Will Learn
- Draw a basic line plot
- Add a title and axis labels
- Style text, line colour, width and style
- Add markers
- Plot several lines with a legend
- Grids, tight_layout and xkcd style
Prerequisites
Basic Line Plot
import matplotlib.pyplot as plt
X = [2, 4, 6, 8, 10]
Y = [36, 19, 30, 52, 25]
plt.plot(X, Y)
plt.show()

Explanation: plot(X, Y) draws straight lines between the points (2, 36), (4, 19) and so on.
Only Y Values
plt.plot([36, 19, 30, 52, 25])
plt.show()

Explanation: With one list, Matplotlib treats it as Y values and uses 0, 1, 2, 3, 4 as X – Python counts from 0.

Title and Labels
import matplotlib.pyplot as plt
X = [2, 4, 6, 8, 10]
Y = [36, 19, 30, 52, 25]
plt.title("Info")
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.plot(X, Y)
plt.show()

Font Style and Colour
import matplotlib.pyplot as plt
X = [2, 4, 6, 8, 10]
Y = [36, 19, 30, 52, 25]
style = dict(fontsize=20, fontname="Times New Roman", color="blue", weight="bold")
plt.title("Info", **style)
plt.xlabel("X axis", **style)
plt.ylabel("Y axis", **style)
plt.plot(X, Y)
plt.show()

Explanation: Putting the shared settings in a dictionary and unpacking it with **style avoids repeating them three times.
| Code | Colour |
|---|---|
| b | blue |
| g | green |
| r | red |
| c | cyan |
| m | magenta |
| y | yellow |
| k | black |
| w | white |

Any CSS name (“orange”) or hex code (“#0f766e”) also works.
Line Width and Style
import matplotlib.pyplot as plt
X = [2, 4, 6, 8, 10]
Y = [36, 19, 30, 52, 25]
plt.plot(X, Y, color="k", linewidth=3, linestyle="dashed")
plt.show()



| linestyle | Looks like |
|---|---|
| “solid” or “-“ | continuous |
| “dashed” or “–“ | dashes |
| “dotted” or “:” | dots |
| “dashdot” or “-.” | dash-dot |
Markers
import matplotlib.pyplot as plt
X = [2, 4, 6, 8, 10]
Y = [36, 19, 30, 52, 25]
plt.plot(X, Y, color="k", linewidth=3, marker="o", markerfacecolor="red", markersize=10)
plt.show()

Explanation: Markers show the actual data points. Common markers: “o” circle, “s” square, “^” triangle, “*” star, “x” cross.
Several Lines with a Legend
import matplotlib.pyplot as plt
X = [2, 4, 6, 8, 10]
plt.plot(X, [36, 19, 30, 52, 25], color="k", linewidth=3, marker="o", label="Chandigarh")
plt.plot(X, [6, 49, 10, 72, 45], color="blue", linewidth=3, marker="o", label="Delhi")
plt.legend()
plt.show()


Explanation: Give each line a label and call plt.legend() to explain which line is which.
Real Example: Salary by Age
import matplotlib.pyplot as plt
ages = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
salary = [38496, 42000, 46752, 49320, 53200, 56000, 62316, 64928, 67317, 68748, 73752]
plt.plot(ages, salary, linewidth=3, label="Python developers")
plt.xlabel("Age")
plt.ylabel("Median Salary (USD)")
plt.title("Median Salary (USD) by Age")
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()

Explanation: grid(True) adds guide lines; tight_layout() adjusts spacing so labels are not cut off.
Fun: xkcd Style
with plt.xkcd():
plt.plot(ages, salary, linewidth=3)
plt.title("Median Salary by Age")
plt.show()

Explanation: Using with plt.xkcd(): applies the hand-drawn style only inside the block; calling plt.xkcd() alone changes every later plot too.
Common Errors
| Problem | Cause and fix |
|---|---|
| Font warning for Times New Roman | The font is not installed (common on Linux); use “serif” or another installed font. |
| Lines appear in the wrong order | Sort the data by X before plotting. |
| Nothing is shown in a script | Call plt.show(). |
Series
Matplotlib introduction · Bar plot · Scatter plot · Histogram
Interview Questions
Q: When should you use a line plot?
For ordered or time-based data where the trend matters.
Q: How do you show two series on one chart?
Call plot() twice with labels, then legend().
Practice Exercises
- Plot your monthly expenses for 6 months with markers and a title.
- Plot two cities’ temperatures over a week with a legend.
Summary
plt.plot draws line charts; add titles, labels, colours, widths, styles, markers, several lines with legends, grids and tight_layout for clear, readable plots.
Next: Bar Plot
Machine Learning Tutorials, Courses and Certifications