Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Data visualization is a vital discipline in modern software engineering, data science, machine learning, and business analytics. While raw numbers and tables convey exact quantities, graphical visualizations immediately reveal patterns, trends, correlations, and anomalies. In Python, Matplotlib is the foundational plotting library upon which the entire scientific visualization ecosystem (including Seaborn, Pandas plotting, and scikit-learn) is built.
Install the library into your active virtual environment:
Import the standard plotting module:
To master Matplotlib, you must understand its hierarchical object architecture:
+-------------------------------------------------------------+ | Figure (The complete canvas / window) | | | | Title: Global Sales 2026 | | +-------------------------------------------------------+ | | | Axes (The actual coordinate plot area) | | | | | | | | Y-Axis (Ticks & Labels) | | | | ^ | | | | | * (Data Point) | | | | | / | | | | | *---* | | | | +---------------------------> X-Axis | | | +-------------------------------------------------------+ | +-------------------------------------------------------------+
Figure: The top-level bounding container holding all axes, titles, subtitles, colorbars, and legends.Axes: The actual plotting coordinate region where data is drawn. A single Figure can contain multiple Axes (subplots).Axis: The numerical number lines (X and Y) defining limits, ticks, and tick labels.Artist: Virtually everything visible on the canvas (lines, text, patches, markers) is an Artist.Matplotlib offers two programming paradigms:
plt.plot)Emulates MATLAB's stateful syntax. It keeps track of the "current" figure and axes automatically. Great for quick exploratory checks in interactive notebooks, but harder to maintain in complex applications:
Explicitly creates and manipulates Figure and Axes objects. This is the industry standard for clean, scalable, multi-plot code:
savefig()In web servers, automated reporting scripts, and backend pipelines, you save charts as high-resolution images or vector graphics instead of displaying interactive GUI windows:
Setting bbox_inches="tight" ensures no labels or legends are cropped off at the image margins.
plt.show()In standalone Python scripts, plots are not displayed on screen until plt.show() is invoked.
Matplotlib requires X and Y sequences to have the exact same number of data points. If len(x) != len(y), it raises a ValueError: x and y must have same first dimension.
Plot daily temperatures recorded over one week in Shikohabad using Matplotlib. Add gridlines, axis labels, markers, and a descriptive title.
Figure and an Axes?fig, ax = plt.subplots()) preferred over stateful plt.plot() for production code?bbox_inches="tight" do when saving a figure?In the next lesson, we will continue your intermediate Python journey with Line, Bar and Pie Charts (13: Data Visualization Basics).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Project: Package-Ready Mini App | Line, Bar and Pie Charts |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.