# Profile: Charts · Matplotlib. Choose a safe display range from current data.
import matplotlib.pyplot as plt
def chart_values(labels, cents):
if len(labels) != len(cents):
raise ValueError("each label needs one value")
values = []
for amount in cents:
values.append(amount / 100)
largest = 0.0
for value in values:
if value > largest:
largest = value
upper = max(1.0, largest * 1.2)
fig, ax = plt.subplots(figsize=(6, 3.5), layout="constrained")
ax.bar(labels, values, color="#2563eb")
ax.set(title="Synthetic accepted subtotal", xlabel="Category",
ylabel="Fictional currency units", ylim=(0, upper))
if len(values) == 0:
ax.text(0.5, 0.5, "No accepted records", transform=ax.transAxes,
ha="center")
total = sum(cents) / 100
print(f"Subtotal: {total:.2f}; y-axis upper bound: {upper}")
return fig, ax
fig, ax = chart_values(["Pens", "Paper"], [14000, 2000])
assert ax.get_ylim()[1] > 140
fig.savefig("changed_values.png", dpi=160)
# Run again with labels=[], cents=[], or with two zero values. Explain each.