# Line Charts: Change Over Time

A line chart shows how a value changed over time, so dates go along the x-axis, a marker shows each measurement, fig.autofmt_xdate() keeps the dates readable, and two lines on one Axes, each with a label= and ax.legend(), compare two groups over the same months.

Apprentice · Python · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Draw a line chart of a value over time with dates on the x-axis, markers and readable date labels
- Draw two lines on one chart with label= and ax.legend(), and explain when a line chart fits

## Key takeaways

- A line chart is for change over time. Its line joins measurements that come in order, so it fits days and months, not categories such as flavors.
- Give ax.plot a column of dates for x and matplotlib spaces them like a calendar; marker="o" puts a dot on each real measurement.
- fig.autofmt_xdate() tilts the date labels and makes room for them, so dates that ran into each other can be read.
- Two ax.plot calls draw two lines on the same Axes. Give each a label= and call ax.legend() to show which line is which.

Did sales go up after the new menu? Is the website busier on Mondays? Did the river rise after the storm? Every "how did it change?" question is answered with a line chart, and it's the chart you'll see most in reports, lab write-ups and news stories.

Hilltop Creamery's `daily.csv` has one row for each day of 2025: the `date`, the high temperature (`high_temp_f`), the `cones_sold` and whether it was `rainy`. July is the shop's busiest month, and the owner wants to see how it went, day by day.

## Dates along the bottom

The cell keeps July's 31 rows and draws a line through them. With `parse_dates=["date"]`, the `date` column holds real dates, not text, so matplotlib spaces them like a calendar: one step for each day.

```python
import pandas as pd
import matplotlib.pyplot as plt

daily = pd.read_csv("daily.csv", parse_dates=["date"])
july = daily[daily["date"].dt.month == 7]

fig, ax = plt.subplots()
ax.plot(july["date"], july["cones_sold"], marker="o")
ax.set_title("Cones sold each day, July 2025")
ax.set_xlabel("Date")
ax.set_ylabel("Cones sold")
plt.show()
```

`marker="o"` puts a dot on each day. The dots are the 31 real measurements; the line between two dots only joins them, since nobody counted cones at midnight. With dots, a reader can count the days and see exactly where each value sits.

The chart has one problem. matplotlib wrote each date in full, 2025-07-01, 2025-07-05 and so on, and side by side under a chart that's only a few inches wide, the dates run into each other.

## Readable dates: fig.autofmt_xdate()

One line fixes it:

```python
fig.autofmt_xdate()
fig
```

**`fig.autofmt_xdate()`** tilts every date label (by 30 degrees), lines it up under its tick and makes room at the bottom of the Figure. It's `fig`, not `ax`, because the room comes from the sheet of paper. A cell whose last line is `fig` shows the chart again, now with the change.

![A line chart titled Cones sold each day, July 2025: one dot for each of July's 31 days joined by a line, the dates along the x-axis from 2025-07-01 to 2025-08-01 tilted so they can be read, Cones sold up the y-axis, most days between 200 and 340 cones, the highest 445 on July 12, and dips on rainy days such as 103 on July 7](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/63d23c02a47c4151bd557b4e0dd114ff.webp)

The dots show more than a table would: most days sold 200 to 340 cones, July 12 sold 445, and the low points, such as 103 on July 7 and 124 on July 17, are all rainy days.

## Two lines and a legend

That gives the owner a question: how much does rain cost, all year? Average the cones on dry days and on rainy days, month by month, and put both on one chart. Two `ax.plot` calls draw two lines on the same Axes, and matplotlib gives the second its own color:

```python
daily["month"] = daily["date"].dt.month
dry_days = daily[~daily["rainy"]].groupby("month")["cones_sold"].mean().round()
rainy_days = daily[daily["rainy"]].groupby("month")["cones_sold"].mean().round()

fig, ax = plt.subplots()
ax.plot(dry_days.index, dry_days.values, marker="o", label="Dry days")
ax.plot(rainy_days.index, rainy_days.values, marker="s", label="Rainy days")
ax.set_title("Average cones sold per day, 2025")
ax.set_xlabel("Month")
ax.set_ylabel("Cones sold per day")
ax.legend()
plt.show()
```

Two lines need a key that says which is which: the **legend**. `label=` names each line as it's drawn, and `ax.legend()` draws the box that lists the names beside a short sample of each line. The second line has square markers (`marker="s"`), so the two lines differ by more than color.

![A line chart titled Average cones sold per day, 2025, with two lines over the months 1 to 12 and a legend naming them: Dry days, with round markers, climbs from 19 in January to 291 in July and falls to 32 in December; Rainy days, with square markers, climbs from 6 to 162 in July and falls to 12, about half the dry line all year](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/7ec5eefd5f3740f787896b09eaf8b13f.webp)

Rainy days sell about half as many cones as dry days, month after month: 162 a day against 291 in July.

## When a line fits

A line says: these points come in order, and the way between two of them means something. That's true of days and months, and of anything measured in order, such as a runner's split times. It isn't true of flavors, stores or neighborhoods: a line from one flavor to the next would suggest a change that never happened. Those get bars, in *Bar Charts: Comparing Categories*.

## Check yourself

2 questions to try on the lesson page.

## So what

When the question is "how did it change?", put the time along the bottom, draw a line with a dot on each measurement, tilt the dates so they can be read, and give every line a name in a legend.

## Key terms

- [Line Chart](https://www.ask-codey.com/glossary/line-chart)
- [Legend](https://www.ask-codey.com/glossary/legend-python)
- [Axes](https://www.ask-codey.com/glossary/axes)

## Related

- [Figures and Axes: How matplotlib Draws](https://www.ask-codey.com/lessons/data-matplotlib-figure-and-axes)
- [Parts of a Date: .dt](https://www.ask-codey.com/lessons/pandas-dt-accessor)

Source: https://www.ask-codey.com/lessons/data-line-charts
