bhylu.com

Advertisements

Matplotlib Plotting

A plot pairs x and y observations. Make sure both arrays describe matching observations and have compatible lengths.

Understand the idea

Explicit x-values preserve the true spacing between observations. If you supply only y, Matplotlib uses positional indices, which can conceal irregular time gaps. Sorting and aggregation are data decisions, not automatic chart fixes. Check units and missing values before interpreting the visual pattern.

Where to try it

Run this complete script in a local virtual environment with Matplotlib installed (py -m pip install matplotlib). It uses a non-interactive backend and saves workshop.png in the current folder. The site shows a rendered example below; its text-only online runner does not display plot windows.

A worked example

Python

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(7, 4), layout="constrained")
ax.plot([1, 2, 5], [10, 12, 11], marker="o")
ax.set(xlabel="Day", ylabel="Orders", title="Uneven observation gaps")
fig.savefig("workshop.png", dpi=140)
plt.close(fig)
print("Saved workshop.png")

Read the result

The last interval is wider because day 5 is farther from day 2 than day 2 is from day 1.

The last interval is wider because day 5 is farther from day 2 than day 2 is from day 1.
Rendered from this original example. Change and rerun the script locally to make your own chart.

Try one change

Change the final day to 3 and compare the visual slope without changing the order counts.

For package options, see the Matplotlib documentation.