USD ($)
$
United States Dollar
Euro Member Countries
India Rupee
د.إ
United Arab Emirates dirham
ر.س
Saudi Arabia Riyal

Matplotlib Basics (Data Visualization)

Lesson 26/35 | Study Time: 60 Min

Numbers alone rarely tell the full story. Visualizing data helps you spot patterns, understand distributions, identify outliers, and communicate findings clearly — all critical steps in AI and data science.

Matplotlib is Python's most widely used data visualization library. It gives you full control over charts and plots, from simple line graphs to complex multi-panel figures. 

Installing and Importing Matplotlib

Matplotlib comes pre-installed with Anaconda. To install manually:

Import the plotting module using the standard alias:

The Basic Plot Structure

Every Matplotlib chart follows the same simple pattern — create data, plot it, add labels, and display it.

Line Chart

A line chart is used to show trends over time or a continuous sequence of values. It is one of the most common charts in data analysis.



1. marker="o" adds a dot at each data point.
2. grid(True) adds a background grid for readability.
3. linewidth controls the thickness of the line.

Bar Chart

A bar chart compares values across categories. It is ideal for comparing groups, scores, or frequencies side by side.


For a horizontal bar chart, replace plt.bar() with plt.barh().


Histogram

A histogram shows the distribution of a dataset, how frequently values fall within certain ranges. It is widely used in AI to understand data spread and detect outliers.



1. bins controls how many bars appear.
2. edgecolor adds borders between bars for clarity.

Scatter Plot

A scatter plot displays the relationship between two numerical variables. It is heavily used in AI to visualize correlations and spot clusters in data.



Pie Chart

A pie chart shows proportions of a whole. Use it when you want to display percentage breakdowns of categories.



1. autopct="%1.1f%%" displays percentage values inside each slice.
2. startangle=90 rotates the chart so it starts from the top.

Customizing Charts

Matplotlib gives you full control over the appearance of your plots.


python

x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 30, 25]

plt.plot(x, y,
         color="red",
         linestyle="--",       # Dashed line
         linewidth=2,
         marker="s",           # Square markers
         markersize=8,
         label="Trend Line")

plt.title("Customized Chart", fontsize=14)
plt.xlabel("X Axis", fontsize=11)
plt.ylabel("Y Axis", fontsize=11)
plt.legend()                   # Shows the label
plt.grid(True, linestyle=":")
plt.show()



Multiple Plots — Subplots

When you need to display several charts together, use plt.subplot() to arrange them in a grid layout.


python

x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 30, 25]

plt.figure(figsize=(10, 4))         # Set overall figure size

# First chart
plt.subplot(1, 2, 1)                # 1 row, 2 columns, position 1
plt.plot(x, y, color="blue")
plt.title("Line Chart")

# Second chart
plt.subplot(1, 2, 2)                # 1 row, 2 columns, position 2
plt.bar(x, y, color="orange")
plt.title("Bar Chart")

plt.tight_layout()                  # Prevents overlapping
plt.show()

Saving a Chart

Instead of just displaying, you can save charts as image files for reports or presentations.


1. dpi controls image quality (150–300 for print quality).

2. bbox_inches="tight" removes excess whitespace around the chart.

Chart Types — When to Use Which

Sales Campaign

Sales Campaign

We have a sales campaign on our promoted courses and products. You can purchase 1 products at a discounted price up to 15% discount.