Line Plot in Matplotlib

Last Updated: 08 Nov 2025


Line plots are the foundation of data visualization in Matplotlib. They connect data points with straight lines — perfect for showing trends over time, continuous changes, or relationships between two variables.

Hinglish Tip: Line plot = "time ya sequence ke saath value kaise badal raha hai" dikhane ka sabse simple aur powerful tareeka.


Basic Line Plot

import matplotlib.pyplot as plt

# Data
x = [1, 2, 3, 4]
y = [10, 15, 13, 18]

# Plot
plt.plot(x, y)
plt.show()

Output: A simple blue line connecting the points.


Customize Your Line Plot (Key Parameters)

ParameterValues / ShorthandPurpose
color'red', '#2E8B57', 'teal'Set line color
linewidth / lw2, 3.5Control line thickness
linestyle / ls'-' (solid), '--' (dashed), ':' (dotted), '-.'Style of the line
marker'o' (circle), 's' (square), '^' (triangle), 'D' (diamond)Shape at data points
markersize / ms8, 12Size of markers
markerfacecolor / mfc'yellow', 'gold'Fill color inside marker
markeredgecolor / mec'black', 'darkred'Border color of marker
markeredgewidth / mew1.5, 2Thickness of marker border
label'Sales', 'Temp'Name for legend
alpha0.5 to 1.0Transparency level

Full Styling Example

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 15, 13, 18]

plt.figure(figsize=(10, 5))
plt.plot(
    x, y,
    color='teal',
    lw=3,
    ls='--',
    marker='o',
    ms=12,
    mfc='gold',
    mec='darkred',
    mew=2,
    label='Growth Trend',
    alpha=0.8
)

plt.title('Sales Growth Over 4 Quarters', fontsize=16, fontweight='bold', pad=15)
plt.xlabel('Quarter', fontsize=12)
plt.ylabel('Revenue (in Thousands)', fontsize=12)
plt.xlim(0.5, 4.5)
plt.ylim(8, 20)
plt.xticks(x, ['Q1', 'Q2', 'Q3', 'Q4'])
plt.yticks(range(10, 21, 2))
plt.grid(True, ls='--', alpha=0.7)
plt.legend(loc='upper left', fontsize=11)
plt.tight_layout()
plt.show()

Hinglish Tip: Shorthand (lw, ms, mfc) use karo → code short aur pro lagega!


Fill Area Under Line (fill_between)

Show range, confidence band, or min-max values.

import matplotlib.pyplot as plt

days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
avg_temp = [30, 32, 33, 31, 29, 28, 27]
min_temp = [25, 26, 27, 26, 24, 23, 22]

plt.plot(days, avg_temp, 'o-', color='orangered', label='Avg Temp')
plt.fill_between(days, avg_temp, min_temp, color='lightcoral', alpha=0.3, label='Min Range')

plt.title('Weekly Temperature Trend')
plt.xlabel('Day')
plt.ylabel('Temperature (°C)')
plt.legend()
plt.grid(True, alpha=0.5)
plt.savefig('temp_range.png', dpi=300, bbox_inches='tight')
plt.show()

Hinglish Tip:
fill_between() = graph ko depth deta hai — report mein impress karega!


Multiple Lines on One Plot

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]

plt.plot(x, [10, 20, 15, 25], 'o-',  label='Product A', color='green')
plt.plot(x, [5, 7, 9, 6],     's--', label='Product B', color='purple')
plt.plot(x, [8, 12, 10, 14],  '^:',  label='Product C', color='orange')

plt.title('Product Sales Comparison')
plt.xlabel('Month')
plt.ylabel('Units Sold')
plt.legend()
plt.grid(True)
plt.show()

Pro Trick: Use format string 'o-' = marker='o', ls='-', color=...


Save High-Quality Plot

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 15, 13, 18]

plt.plot(x, y, 'o-', color='blue')
plt.title('Final Report Plot')
plt.xlabel('X')
plt.ylabel('Y')

# High-res + clean edges
plt.savefig('line_plot_final.png', dpi=300, bbox_inches='tight', facecolor='white')
plt.show()

Hinglish Tip: bbox_inches='tight' → extra white space nahi, perfect fit!