Line graphs are the go-to chart for showing change over time and trends. In this lesson you'll learn simple line plots, multiple series, markers & styles, smoothing / moving averages (trend lines), and how to annotate important points. Try examples live below using Python (Pyodide)!
import matplotlib.pyplot as plt
months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug']
sales = [120, 130, 115, 150, 170, 165, 180, 190]
plt.plot(months, sales)
plt.title('Monthly Sales')
plt.xlabel('Month')
plt.ylabel('Sales')
plt.show()
plt.plot(months, sales, marker='o', linestyle='-', label='This Year')
plt.plot(months, [100,110,120,130,140,150,160,170], marker='s', linestyle='--', label='Last Year')
plt.legend()
plt.plot(months, sales, color='#2b9bd3', marker='o', linewidth=2)
plt.grid(alpha=0.3)
import numpy as np
window = 3
movavg = np.convolve(sales, np.ones(window)/window, mode='valid')
# plot movavg aligned to the middle of the window
plt.plot(months[window-1:], movavg, color='orange', label='3-month MA')
plt.annotate('Peak', xy=('Aug', 190), xytext=('Jun',200),
arrowprops=dict(arrowstyle='->'))
💬 Tip: In the Pyodide practice section below, use
matplotlib.use('Agg') and save your figure with
plt.savefig() to display it in-browser.
Click Preload Pyodide once to install
matplotlib and numpy. Then press
Run & Render to display your chart as an image!
plt.plot() for continuous data — ideal for time
series and trends.
label and plt.legend().
plt.annotate() to call
out peaks or events.