Python Matplotlib Full Notes for Beginners

Introduction
Matplotlib is a data visualization library in Python. It helps you create various charts like line plots, bar graphs, scatter plots, pie charts, and more.
Installation
pip install matplotlib
Importing
import matplotlib.pyplot as plt
Basic Structure of a Plot
x = [1, 2, 3, 4]
y = [10, 20, 25, 30]
plt.plot(x, y)
plt.title("Sample Line Chart")
plt.xlabel("X-axis Label")
plt.ylabel("Y-axis Label")
plt.xticks([1, 2, 3, 4], ["One", "Two", "Three", "Four"])
plt.yticks([10, 20, 30])
plt.grid(True)
plt.legend(["Data"], loc='upper left')
plt.savefig("plot.png") # Save plot as image
plt.show()

Line Plot
x = [1, 2, 3, 4, 5]
y = [5, 7, 4, 6, 8]
plt.plot(x, y, color='blue', linestyle='--', linewidth=2, marker='o')
plt.title("Line Plot Example")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.xticks(x)
plt.grid(True)
plt.show()

Bar Chart
categories = ['A', 'B', 'C', 'D']
values = [10, 20, 15, 30]
plt.bar(categories, values, color='green', edgecolor='black')
plt.title("Bar Chart")
plt.xlabel("Categories")
plt.ylabel("Values")
plt.ylim(0, 35)
plt.grid(axis='y')
plt.show()

Horizontal Bar Chart
plt.barh(categories, values, color='orange')
plt.title("Horizontal Bar Chart")
plt.ylabel("Categories")
plt.xlabel("Values")
plt.xlim(0, 35)
plt.grid(axis='x')
plt.show()

Histogram
data = [22, 87, 5, 43, 56, 73, 55, 54, 11, 20, 51, 5, 79, 31, 27]
plt.hist(data, bins=5, color='purple', edgecolor='black')
plt.title("Histogram")
plt.xlabel("Ranges")
plt.ylabel("Frequency")
plt.grid(axis='y')
plt.show()

Pie Chart
labels = ['Python', 'Java', 'C++', 'Ruby']
sizes = [215, 130, 245, 210]
colors = ['gold', 'lightblue', 'lightgreen', 'pink']
plt.pie(sizes, labels=labels, colors=colors, startangle=90, shadow=True, autopct='%1.1f%%')
plt.title("Pie Chart")
plt.axis('equal') # Equal aspect ratio makes the pie circular
plt.show()

Scatter Plot
x = [5, 7, 8, 7, 2, 17, 2, 9]
y = [99, 86, 87, 88, 100, 86, 103, 87]
plt.scatter(x, y, color='red', s=100) # s is size of dots
plt.title("Scatter Plot")
plt.xlabel("X")
plt.ylabel("Y")
plt.grid(True)
plt.show()

Stacked Bar Chart
x = ['Q1', 'Q2', 'Q3', 'Q4']
A = [3, 4, 5, 6]
B = [1, 3, 4, 5]
plt.bar(x, A, label='Product A', color='blue')
plt.bar(x, B, bottom=A, label='Product B', color='orange')
plt.title("Stacked Bar Chart")
plt.xlabel("Quarter")
plt.ylabel("Sales")
plt.legend()
plt.show()

Box Plot
data = [7, 15, 13, 18, 9, 10, 22, 30]
plt.boxplot(data)
plt.title("Box Plot")
plt.ylabel("Values")
plt.grid(True)
plt.show()

Area Chart
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 6, 8]
plt.fill_between(x, y, color='skyblue', alpha=0.4)
plt.plot(x, y, color='blue')
plt.title("Area Chart")
plt.xlabel("X")
plt.ylabel("Y")
plt.grid(True)
plt.show()

Multiple Line Plot
x = [1, 2, 3, 4]
y1 = [1, 4, 9, 16]
y2 = [2, 5, 10, 17]
plt.plot(x, y1, label='Line 1', color='blue')
plt.plot(x, y2, label='Line 2', color='green')
plt.title("Multiple Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.legend()
plt.grid(True)
plt.show()

Real-Life Example: Student Marks
subjects = ['Math', 'Science', 'English', 'History']
marks = [88, 75, 90, 70]
plt.plot(subjects, marks, marker='o', color='blue')
plt.title("Student Marks")
plt.ylabel("Marks")
plt.ylim(0, 100)
plt.grid(True)
plt.show()

Customizations Summary
Feature | Description | Example Code |
---|---|---|
Title | Adds a title to the plot | plt.title(“Chart Title”) |
Axis Labels | Labels the X and Y axes | plt.xlabel(“X”), plt.ylabel(“Y”) |
Grid Lines | Adds grid to background | plt.grid(True) |
Ticks | Custom values on axis | plt.xticks([…]), plt.yticks([…]) |
Legend | Labels for plotted lines or bars | plt.legend() |
Style | Changes overall plot style | plt.style.use(‘ggplot’) |
Save Plot | Save the plot as an image | plt.savefig(“file.png”) |
Axis Limits | Fix min and max of axes | plt.xlim(), plt.ylim() |
Recommended Styles in Matplotlib
plt.style.use('ggplot')
plt.style.use('seaborn')
plt.style.use('classic')
To see all available styles:
print(plt.style.available)