13. Visualization with matplotlib
13.2 Line and Hist
Quick answer: the short, self-contained version of this lesson is the guide matplotlib and seaborn: First Charts for Storytelling.
# Fictional daily orders in Pune around Diwali
days = list(range(1, 11))
orders = [180, 190, 210, 250, 900, 880, 400, 220, 200, 190]
fig, ax = plt.subplots()
ax.plot(days, orders, marker="o")
ax.set_title("Pune daily orders (fictional) — Diwali spike")
ax.set_xlabel("Day of October 2026")
ax.set_ylabel("Orders")
ax.axhline(500, linestyle="--", label="Spike threshold")
ax.legend()
plt.show()
mins = [9, 12, 11, 14, 18, 10, 8, 13, 16, 17, 11, 9]
fig, ax = plt.subplots()
ax.hist(mins, bins=5)
ax.set_title("Delivery minutes distribution (fictional sample)")
ax.set_xlabel("Minutes")
ax.set_ylabel("Count")
plt.show()
Steps in Jupyter
- Plot the city GMV bar chart.
- Rotate x labels so Sambhaji Nagar is readable.
- Plot the Diwali line chart with a threshold line.
- Save:
fig.savefig("figures/pune_diwali.png", dpi=120).
What you should see. Clear spike on days 5–6; PNG appears in figures/.
Ravindra Bagale's Tip
Save your chart with fig.savefig("figures/pune_diwali.png", dpi=120) instead of taking a screenshot, because the same code can make the same clear image again. The safest habit is to call savefig before plt.show() in the same cell. Create the figures folder first, because savefig does not create folders and stops with a FileNotFoundError.
Ravindra Bagale's Tip – मराठी
Screenshot घेण्याऐवजी fig.savefig("figures/pune_diwali.png", dpi=120) ने chart save करा, कारण तोच code पुन्हा तशीच स्पष्ट image बनवू शकतो. सगळ्यात सुरक्षित सवय म्हणजे त्याच cell मध्ये plt.show() च्या आधी savefig चालवा. आधी figures folder तयार करा, कारण savefig स्वतः folder बनवत नाही आणि FileNotFoundError देऊन थांबतो.
Ravindra Bagale's Tip – हिंदी
Screenshot लेने के बजाय fig.savefig("figures/pune_diwali.png", dpi=120) से chart save करो, क्योंकि वही code दोबारा वैसी ही साफ़ image बना सकता है। सबसे सुरक्षित आदत यह है कि उसी cell में plt.show() से पहले savefig चलाओ। पहले figures folder बना लो, क्योंकि savefig खुद folder नहीं बनाता और FileNotFoundError देकर रुक जाता है।