📊 Matplotlib – Brief Notes
1. Introduction
• Matplotlib is a popular Python library used for data visualization.
• It helps to create different types of charts and graphs like line charts, bar charts,
histograms, scatter plots, and pie charts.
• Module used: matplotlib.pyplot (commonly imported as plt).
import matplotlib.pyplot as plt
2. Basic Plot
x = [1, 2, 3, 4]
y = [2, 4, 6, 8]
plt.plot(x, y) # creates a line chart
plt.show()
3. Common Functions
• plt.plot(x, y) → Line chart
• plt.bar(x, y) → Bar chart
• plt.pie(values) → Pie chart
• plt.scatter(x, y) → Scatter plot
• plt.hist(data) → Histogram
• plt.show() → Displays the graph
4. Adding Labels and Title
plt.plot(x, y)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Simple Line Graph")
plt.show()
5. Customization
• Markers & Colors in line chart:
plt.plot(x, y, marker='o', color='red', linestyle='--')
• marker='o' → circle points
• color='red' → red line
• linestyle='--' → dashed line
6. Multiple Lines
x = [1,2,3,4]
y1 = [1,4,9,16]
y2 = [2,4,6,8]
plt.plot(x, y1, label="Squares")
plt.plot(x, y2, label="Doubles")
plt.legend()
plt.show()
7. Pie Chart Example
sizes = [20, 30, 25, 25]
labels = ['A', 'B', 'C', 'D']
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.show()
8. Key Points
• Matplotlib is mainly used for 2D visualizations.
• Helps in understanding data patterns and trends.
• Works well with NumPy and Pandas for data handling.
✨ Shortcut for remembering: L-B-P-S-H (Line, Bar, Pie, Scatter, Histogram) → 5 main chart
types.