If you're new to Python and want to learn how to create beautiful charts and graphs, Matplotlib is the perfect place to start. This guide walks you through the basics of data visualization using Matplotlib with simple explanations, code examples, and outputs.
📦 Installing Matplotlib
Install Matplotlib using pip:
pip install matplotlib
Import it in your Python script:
import matplotlib.pyplot as plt
✅ Line Plot
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 40]
plt.plot(x, y, marker='o')
plt.title('Simple Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid(True)
plt.show()
Output:
Use Case:
- Ideal for showing trends over time or continuous data.
- Common in time series analysis, stock prices, temperature changes, or performance metrics.
✅ Bar Chart
categories = ['A', 'B', 'C', 'D']
values = [10, 15, 7, 12]
plt.bar(categories, values, color='skyblue')
plt.title('Bar Chart Example')
plt.xlabel('Categories')
plt.ylabel('Values')
plt.show()
Output:
Use Case:
- Best for comparing discrete categories.
- Useful in survey results, sales by region, frequency of items, or task completion status.
✅ Pie Chart
labels = ['Apples', 'Bananas', 'Cherries', 'Dates']
sizes = [30, 25, 25, 20]
plt.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
plt.title('Fruit Distribution')
plt.show()
Output:
Use Case:
- Great for showing proportional data or percentage breakdowns.
- Common in market share, budget allocation, or resource distribution.
✅ 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='green')
plt.title('Scatter Plot Example')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid(True)
plt.show()
Output:
Use Case:
- Used to show relationships or correlations between two variables.
- Helpful in regression analysis, outlier detection, or distribution of data points.
✅ Histogram
data = [22, 87, 5, 43, 56, 73, 55, 54, 11, 20, 51, 5, 79, 31, 27]
plt.hist(data, bins=5, color='orange', edgecolor='black')
plt.title('Histogram Example')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.grid(True)
plt.show()
Output:
Use Case:
- Ideal for showing the distribution of a dataset.
- Common in exam scores, age distribution, or frequency of values.
🔧 Customizing Plots
- Add titles:
plt.title('My Plot') - Add labels:
plt.xlabel('X-axis'),plt.ylabel('Y-axis') - Add legend:
plt.legend(['Line 1'])
📑 Multiple Plots
# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 40]
categories = ['A', 'B', 'C', 'D']
values = [10, 15, 7, 12]
plt.subplot(1, 2, 1)
plt.plot(x, y)
plt.subplot(1, 2, 2)
plt.bar(categories, values)
plt.show()
Output:
Left Plot: A line plot showing values increasing across X-axis points.
Right Plot: A bar chart comparing four categories (A, B, C, D) with their respective values.
Use Case:
- Useful for comparing multiple visualizations side-by-side.
- Great for dashboards or reports where you want to show different perspectives of the same data.
Here is the 2x2 grid layout with four plots:
import matplotlib.pyplot as plt
# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 20, 25, 30, 40]
categories = ['A', 'B', 'C', 'D']
values = [10, 15, 7, 12]
labels = ['Apples', 'Bananas', 'Cherries', 'Dates']
sizes = [30, 25, 25, 20]
scatter_x = [5, 7, 8, 7, 2, 17, 2, 9]
scatter_y = [99, 86, 87, 88, 100, 86, 103, 87]
# Create a 2x2 grid layout
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
# Line Plot
axs[0, 0].plot(x, y, marker='o')
axs[0, 0].set_title('Line Plot')
axs[0, 0].set_xlabel('X-axis')
axs[0, 0].set_ylabel('Y-axis')
axs[0, 0].grid(True)
# Bar Chart
axs[0, 1].bar(categories, values, color='skyblue')
axs[0, 1].set_title('Bar Chart')
axs[0, 1].set_xlabel('Categories')
axs[0, 1].set_ylabel('Values')
# Pie Chart
axs[1, 0].pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
axs[1, 0].set_title('Pie Chart')
# Scatter Plot
axs[1, 1].scatter(scatter_x, scatter_y, color='green')
axs[1, 1].set_title('Scatter Plot')
axs[1, 1].set_xlabel('X-axis')
axs[1, 1].set_ylabel('Y-axis')
axs[1, 1].grid(True)
# Adjust layout
plt.tight_layout()
plt.show()
Plots Included:
- Top Left: Line Plot – Shows trend across X-axis.
- Top Right: Bar Chart – Compares categories A, B, C, D.
- Bottom Left: Pie Chart – Displays fruit distribution percentages.
- Bottom Right: Scatter Plot – Highlights relationships between two variables.
💾 Saving Figures
plt.savefig('my_plot.png')
Use Case:
- Save plots for presentations, reports, or sharing with others.








Top comments (0)