Time series plot with dates
A time series plot puts real dates on the x-axis. Parsing the date column properly lets matplotlib place the ticks sensibly and handle gaps and uneven sampling correctly.
When to use it
- Your CSV has a date or timestamp column.
- You want to mark events, holidays or periods on the timeline.
Ask for it in pltchat
Upload your file and type something like:
"plot temperature over time, shade the summer months"
pltchat writes the loader and the plot for your columns, runs it in your browser and shows you the code.
The matplotlib code
Self-contained with example data; swap in pd.read_csv(...) or
pd.read_excel(...) for your own file.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
rng = np.random.default_rng(22)
dates = pd.date_range("2023-01-01", "2024-12-31", freq="D")
doy = dates.day_of_year.to_numpy()
df = pd.DataFrame({"date": dates,
"temp": 11 - 9 * np.cos(2 * np.pi * doy / 365) + rng.normal(0, 2.5, dates.size)})
# With a CSV: df = pd.read_csv("file.csv", parse_dates=["date"])
fig, ax = plt.subplots(figsize=(6.4, 4))
ax.plot(df["date"], df["temp"], lw=0.8)
for year in (2023, 2024):
ax.axvspan(pd.Timestamp(f"{year}-06-01"), pd.Timestamp(f"{year}-08-31"),
color="C1", alpha=0.15)
ax.xaxis.set_major_locator(mdates.MonthLocator(bymonth=[1, 4, 7, 10]))
ax.xaxis.set_major_formatter(mdates.DateFormatter("%b %Y"))
ax.set_ylabel("Temperature (°C)")
fig.autofmt_xdate()
plt.tight_layout()
plt.show()
Upload a CSV, Excel, JSON or Parquet file. Your first plot is always free.