5 min read min read
Customizing Plots
Learn to style your charts with colors, fonts, and themes
Customizing Plots
Why Customize?
Default plots work, but custom plots:
- Look more professional
- Match your brand
- Are easier to read
Change Font Sizes
code.py
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 9])
ax.set_title('My Chart', fontsize=16)
ax.set_xlabel('X Axis', fontsize=12)
ax.set_ylabel('Y Axis', fontsize=12)
plt.show()Change Tick Label Size
code.py
ax.tick_params(axis='both', labelsize=10)Use Different Colors
code.py
# Named colors
ax.plot(x, y, color='red')
ax.plot(x, y, color='steelblue')
# Hex colors
ax.plot(x, y, color='#3498db')
# RGB (0-1 scale)
ax.plot(x, y, color=(0.2, 0.6, 0.8))Use Color Palettes
code.py
# Built-in colors
colors = plt.cm.tab10.colors # 10 distinct colors
for i, color in enumerate(colors[:5]):
ax.plot([1, 2, 3], [i, i+1, i+2], color=color)Add Grid
code.py
ax.grid(True)
# Customize grid
ax.grid(True, linestyle='--', alpha=0.5, color='gray')Change Line Width
code.py
ax.plot(x, y, linewidth=2) # or lw=2Add Legend
code.py
ax.plot(x, y1, label='Series 1')
ax.plot(x, y2, label='Series 2')
ax.legend(loc='upper right')Locations: 'upper right', 'upper left', 'lower right', 'lower left', 'center'
Set Background Color
code.py
ax.set_facecolor('#f0f0f0') # Light gray backgroundRemove Borders (Spines)
code.py
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)Use Built-in Styles
code.py
# See available styles
print(plt.style.available)
# Use a style
plt.style.use('seaborn-v0_8')
# or
plt.style.use('ggplot')Popular styles: 'ggplot', 'seaborn-v0_8', 'fivethirtyeight', 'dark_background'
Complete Example
code.py
import matplotlib.pyplot as plt
# Use a nice style
plt.style.use('seaborn-v0_8-whitegrid')
# Data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
sales = [100, 120, 115, 140, 160]
# Create figure
fig, ax = plt.subplots(figsize=(10, 6))
# Plot with custom styling
ax.plot(months, sales, marker='o', color='#2ecc71',
linewidth=2, markersize=8)
# Titles and labels
ax.set_title('Monthly Sales', fontsize=16, fontweight='bold')
ax.set_xlabel('Month', fontsize=12)
ax.set_ylabel('Sales ($K)', fontsize=12)
# Remove top and right borders
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# Customize ticks
ax.tick_params(axis='both', labelsize=11)
plt.tight_layout()
plt.show()Key Customization Options
| What | How |
|---|---|
| Title size | fontsize=16 |
| Line width | linewidth=2 |
| Marker size | markersize=8 |
| Transparency | alpha=0.5 |
| Grid | grid(True) |
| Legend | legend() |
| Style | plt.style.use() |
Key Points
- fontsize changes text size
- linewidth changes line thickness
- alpha adds transparency
- plt.style.use() for quick themes
- Remove spines for cleaner look
- Always use tight_layout() to prevent overlap
What's Next?
Learn to create multiple charts in one figure with subplots.