Line charts for change over time
Draw a trend, then give it a title and axis labels.
A chart lets people see a pattern in a second that would take minutes to find in a table. matplotlib is Python’s main charting library. The clearest way to use it is fig, ax = plt.subplots(): fig is the whole picture, and ax is the area you draw on. A line chart, ax.plot(x, y), connects points in order, so it is the right choice when x is time and you want to show a trend. Every chart needs a title and axis labels (with units) so readers know what they are looking at.
import io
import pandas as pd
csv_text = """month,high,low,rain_mm
Jan,8,1,78
Feb,10,2,60
Mar,14,5,55
Apr,18,8,42
May,23,12,35
Jun,28,17,20
Jul,31,20,12
Aug,30,19,15
Sep,26,15,30
Oct,20,10,58
Nov,13,5,72
Dec,9,2,85
"""
weather = pd.read_csv(io.StringIO(csv_text))
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(weather["month"], weather["high"], marker="o", label="High")
ax.plot(weather["month"], weather["low"], marker="o", label="Low")
ax.set_title("Average temperatures by month")
ax.set_xlabel("Month")
ax.set_ylabel("Temperature (°C)")
ax.legend()Each ax.plot call adds one line. The label= values appear in the legend, which ax.legend() draws. On FactorSir the finished chart appears below your output automatically.
marker="o" draws a dot at each data point, which helps when there are only a few points.
In other places (like a notebook or your own computer) you may need plt.show() at the end to display the chart.