Matplotlib Plotting
Learn the fundamentals of creating plots with matplotlib
📈 Basic Plotting
Learn how to create your first plots with matplotlib. We'll cover the essential plotting functions and how to customize your visualizations.
import matplotlib.pyplot as plt
# Basic line plot
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.title('Basic Line Plot')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.show()
📊 Basic Plot Types
Line Plot
Connect data points with lines
plt.plot(x, y)
plt.show()
Scatter Plot
Show individual data points
plt.scatter(x, y)
plt.show()
Bar Chart
Compare categories with bars
plt.bar(categories, values)
plt.show()
Multiple Lines
Plot several data series
plt.plot(x, y1, x, y2)
plt.show()
🔹 Simple Line Plot
The most basic plot - connecting points with a line
import matplotlib.pyplot as plt
# Create data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Create line plot
plt.plot(x, y)
# Add labels
plt.title('Simple Line Plot')
plt.xlabel('X values')
plt.ylabel('Y values')
# Show the plot
plt.show()
# You can also plot without x values
y_only = [1, 4, 9, 16, 25]
plt.plot(y_only) # x will be [0, 1, 2, 3, 4]
plt.title('Y values only')
plt.show()
🔹 Multiple Lines
Plot several data series on the same graph
# Method 1: Multiple plot() calls
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
y2 = [1, 2, 3, 4, 5]
plt.plot(x, y1)
plt.plot(x, y2)
plt.title('Two Lines - Method 1')
plt.legend(['Squares', 'Linear'])
plt.show()
# Method 2: Single plot() call
plt.plot(x, y1, x, y2)
plt.title('Two Lines - Method 2')
plt.legend(['Squares', 'Linear'])
plt.show()
# Method 3: With labels
plt.plot(x, y1, label='Squares')
plt.plot(x, y2, label='Linear')
plt.title('Two Lines - Method 3')
plt.legend()
plt.show()
🔹 Plot Customization
Basic ways to customize your plots
# Colors and styles
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Different colors
plt.plot(x, y, color='red') # or 'r'
plt.plot(x, y, color='blue') # or 'b'
plt.plot(x, y, color='green') # or 'g'
# Line styles
plt.plot(x, y, linestyle='-') # solid line
plt.plot(x, y, linestyle='--') # dashed line
plt.plot(x, y, linestyle=':') # dotted line
# Combine color and style
plt.plot(x, y, 'r--') # red dashed line
plt.plot(x, y, 'bo') # blue circles
plt.plot(x, y, 'g^') # green triangles
plt.title('Styled Lines')
plt.show()
🔹 Working with NumPy
Using NumPy arrays for more advanced plotting
💡 Why NumPy?
- More efficient for large datasets
- Mathematical functions work element-wise
- Easy to create ranges and sequences
import matplotlib.pyplot as plt
import numpy as np
# Create smooth curves
x = np.linspace(0, 10, 100) # 100 points from 0 to 10
y = np.sin(x)
plt.plot(x, y)
plt.title('Sine Wave')
plt.xlabel('X')
plt.ylabel('sin(X)')
plt.grid(True)
plt.show()
# Multiple mathematical functions
x = np.linspace(-5, 5, 100)
y1 = x**2
y2 = x**3
y3 = np.sin(x)
plt.plot(x, y1, label='x²')
plt.plot(x, y2, label='x³')
plt.plot(x, y3, label='sin(x)')
plt.legend()
plt.title('Mathematical Functions')
plt.show()
🔹 Figure Size and DPI
Control the size and quality of your plots
# Set figure size
plt.figure(figsize=(10, 6)) # width=10, height=6 inches
plt.plot([1, 2, 3], [1, 4, 9])
plt.title('Large Figure')
plt.show()
# Set DPI for higher quality
plt.figure(figsize=(8, 6), dpi=100)
plt.plot([1, 2, 3], [1, 4, 9])
plt.title('High DPI Figure')
plt.show()
# Default figure size
plt.rcParams['figure.figsize'] = [8, 6] # Set default size
plt.plot([1, 2, 3], [1, 4, 9])
plt.title('Default Size Changed')
plt.show()
🔹 Saving Plots
Save your plots to files
# Basic save
plt.plot([1, 2, 3], [1, 4, 9])
plt.title('My Plot')
plt.savefig('my_plot.png')
plt.show()
# Save with options
plt.plot([1, 2, 3], [1, 4, 9])
plt.title('High Quality Plot')
plt.savefig('high_quality.png',
dpi=300, # High resolution
bbox_inches='tight', # Remove extra whitespace
facecolor='white') # White background
plt.show()
# Different formats
plt.plot([1, 2, 3], [1, 4, 9])
plt.savefig('plot.pdf') # PDF format
plt.savefig('plot.svg') # SVG format
plt.savefig('plot.jpg') # JPEG format
plt.show()