import matplotlib.pyplot as plt months = ["Jan", "Feb", "Mar", "Apr"] north = [18, 22, 24, 28] south = [14, 18, 20, 23] target = [16, 20, 23, 26] fig, ax = plt.subplots(layout="constrained") ax.plot(months, north, marker="o", label="North region") ax.plot(months, south, marker="s", label="South region") ax.plot(months, target, linestyle="--", color="0.35", label="Target") legend = ax.legend(loc="upper left", title="Sales plan", ncols=1, frameon=True) ax.set_title("Quarterly bookings") ax.set_ylabel("Bookings") fig.savefig("legend-add-output.png", dpi=160) labels = ", ".join(text.get_text() for text in legend.get_texts()) print(f"Legend labels: {labels}") print("Saved figure: legend-add-output.png")