18 Matplotlib
18 Matplotlib
High Quality
Key Features
visualizations
Highly Customizable
tune
Created By Control every aspect of your plots - colors, fonts,
Publication Quality
verified
ad Generate high-resolution, professional-grade
visualizations for research and presentations
Installation and Setup
Basic Import & Setup
Installation Methods
1 Import pyplot
2 Create figure
Matplotlib Architecture
3 Create axes
Figure
4 Plot data
crop_square
The overall canvas container for all plots 5 Display plot
# Display
[Link]()
Line Plots
Creating Line Plots
Line Plot Visualization
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
[Link](x, y)
[Link]('X Axis')
[Link]('Y Axis')
[Link]('Line Plot Example')
[Link]()
[Link](categories, values)
[Link]('Categories')
[Link]('Values')
[Link]('Bar Chart Example')
[Link]()
bar_chart Grouped & Stacked Bars
# Grouped bars
[Link]() [Link]() [Link](x1, y1, width=0.4, label='Group 1')
Vertical Bars Horizontal Bars [Link](x2, y2, width=0.4, label='Group 2')
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
[Link](x, y)
[Link]('X Axis')
[Link]('Y Axis')
[Link]('Scatter Plot Example')
[Link]()
s=100 c='red'
[Link](x, y, s=200, c='purple',
Marker Size Marker Color
marker='s', alpha=0.7,
label='Data Points')
marker='o' alpha=0.6 [Link]()
Marker Shape Transparency
Histograms
Creating Histograms
explode = (0.1, 0, 0, 0, 0)
import [Link] as plt colors = ['#ff9999', '#66b3ff',
'#99ff99', '#ffcc99', '#c2c2f0']
sizes = [30, 20, 25, 15, 10]
labels = ['A', 'B', 'C', 'D', 'E']
[Link](sizes, explode=explode,
labels=labels, colors=colors,
[Link](sizes, labels=labels)
autopct='%1.1f%%',
[Link]('Pie Chart Example') shadow=True, startangle=90)
[Link]()
code
1 2
2x2 Subplots
plt.tight_layout() 3 4
figsize=(12, 8) sharex=True
Figure Size Share X Axis
sharey=True tight_layout()
dashboard Common Grid Arrangements
Share Y Axis Adjust Spacing
subplots(1, 2) subplots(2, 1) subplots(2, 2)
1×2 Grid 2×1 Grid 2×2 Grid
Customizing Plots
Customization Elements
linewidth=2)
[Link](x, y2, 'r--', label='Line 2')
grid_on [Link]() crop_landscape [Link]()
Grid Lines X-axis Limits
[Link]('Time (seconds)',
fontsize=12)
[Link]('Value', fontsize=12)
crop_portrait [Link]() text_fields [Link]()
[Link]('Customized Plot',
Y-axis Limits Tick Labels
fontsize=16, fontweight='bold')
[Link](fontsize=10,
loc='upper right') add_comment Annotations
[Link](True, alpha=0.3)
[Link](0, 100) [Link]('Peak', xy=(50, 40),
[Link](0, 50) xytext=(60, 45),
arrowprops=dict(
facecolor='red'))
Styling and Themes
Applying Styles
[Link]('seaborn')
trending_up fivethirtyeight grid_on dark_background
# Create plot with style
Data Journalism Dark Theme
[Link](x, y)
[Link]('Styled Plot')
[Link]()
more_horiz More Available Styles
dates = [[Link](2024, 1, i)
text_fields DateFormatter view_week DateLocator
for i in range(1, 8)]
Format Dates Locate Dates
values = [10, 15, 12, 18, 14, 20, 16]
[Link](dates, values)
[Link]('Weekly Data') auto_awesome Date Format Codes
[Link]()
%Y %m
Year (2024) Month (01-12)
%d %H:%M:%S
date_range Date Formatting Day (01-31) Time
fig = [Link]()
ax = fig.add_subplot(111, projection='3d')
landscape plot_surface() waves plot_wireframe()
Surface Plots Wireframe Plots
[Link](x, y, z, c='blue',
marker='o', s=100)
ax.set_xlabel('X')
ax.set_ylabel('Y') 3d_rotation View Control
ax.set_zlabel('Z')
[Link]() view_init(elev=30, azim=45) set_xlim3d()
Set View Angle X Axis Limits
set_ylim3d() set_zlim3d()
Y Axis Limits Z Axis Limits
terrain 3D Surface Plot
X, Y = [Link](x, y)
tips_and_updates Tip: Use projection='3d' to enable 3D plotting
Z = [Link]([Link](X**2 + Y**2))
ax.plot_surface(X, Y, Z, cmap='viridis')
Saving and Exporting / Best Practices
Saving Plots
Best Practices
save Using savefig()
Keep It Simple
check_circle
import [Link] as plt Avoid clutter, focus on key insights
# Basic save
Choose Appropriate Chart Types
[Link]('[Link]') check_circle
Match visualization to data type
format='png',
bbox_inches='tight')
Use Consistent Colors
check_circle
Maintain visual consistency across plots
# Generate data
x = [Link](0, 10, 100)
y = [Link](x)
# Define data
labels = ['Apple', 'Banana', 'Orange', 'Mango']
sizes = [30, 25, 25, 20]
colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99']
explode = (0.1, 0, 0, 0)
groups Target Identification campaign Marketing Focus • Secondary: 35-44 group (28%)
Pinpoint key age groups Tailor campaigns effectively
• Growing: 18-24 segment (18%)