Matplotlib Markers
Customize data points with different marker styles
🎯 Understanding Markers
Markers are symbols used to highlight individual data points in your plots. They help make data points more visible and can convey additional information through different shapes, sizes, and colors.
import matplotlib.pyplot as plt
# Basic markers
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y, 'o') # Circle markers
plt.plot(x, y, 's') # Square markers
plt.plot(x, y, '^') # Triangle markers
plt.show()
🎨 Common Marker Types
Basic Shapes
Circles, squares, triangles
plt.plot(x, y, 'o') # Circle
plt.plot(x, y, 's') # Square
plt.plot(x, y, '^') # Triangle
Cross Markers
Plus signs and X marks
plt.plot(x, y, '+') # Plus
plt.plot(x, y, 'x') # X mark
plt.plot(x, y, '*') # Star
Special Shapes
Diamonds, pentagons, hexagons
plt.plot(x, y, 'D') # Diamond
plt.plot(x, y, 'p') # Pentagon
plt.plot(x, y, 'h') # Hexagon
Filled/Unfilled
Control marker fill
plt.plot(x, y, 'o') # Filled
plt.plot(x, y, 'o',
fillstyle='none') # Unfilled
🔹 Basic Marker Usage
How to add markers to your plots
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# Just markers (no line)
plt.plot(x, y, 'o')
plt.title('Only Markers')
plt.show()
# Line with markers
plt.plot(x, y, 'o-') # Circle markers with line
plt.title('Line with Markers')
plt.show()
# Different marker for each line
y2 = [1, 3, 5, 7, 9]
plt.plot(x, y, 'o-', label='Circles')
plt.plot(x, y2, 's-', label='Squares')
plt.legend()
plt.title('Different Markers')
plt.show()
🔹 Marker Reference Guide
Complete list of available markers
📍 Point Markers
-
'.'- Point -
','- Pixel -
'o'- Circle -
'v'- Triangle down -
'^'- Triangle up -
'<'- Triangle left -
'>'- Triangle right
🔷 Shape Markers
-
's'- Square -
'p'- Pentagon -
'*'- Star -
'h'- Hexagon1 -
'H'- Hexagon2 -
'+'- Plus -
'x'- X -
'D'- Diamond -
'd'- Thin diamond
# Show different markers
import matplotlib.pyplot as plt
markers = ['o', 's', '^', 'v', '<', '>', 'D', 'p', '*', 'h', '+', 'x']
x = range(len(markers))
y = [1] * len(markers)
plt.figure(figsize=(12, 3))
for i, marker in enumerate(markers):
plt.plot(i, 1, marker, markersize=10, label=f"'{marker}'")
plt.xlim(-0.5, len(markers)-0.5)
plt.ylim(0.5, 1.5)
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
plt.title('Marker Types')
plt.show()
🔹 Marker Customization
Control marker size, color, and style
# Marker size
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y, 'o', markersize=5) # Small
plt.plot(x, y, 's', markersize=10) # Medium
plt.plot(x, y, '^', markersize=15) # Large
plt.title('Different Marker Sizes')
plt.show()
# Marker colors
plt.plot(x, y, 'ro') # Red circles
plt.plot(x, y, 'bs') # Blue squares
plt.plot(x, y, 'g^') # Green triangles
plt.title('Colored Markers')
plt.show()
# Advanced marker styling
plt.plot(x, y, 'o',
markersize=10,
markerfacecolor='red',
markeredgecolor='black',
markeredgewidth=2)
plt.title('Custom Marker Style')
plt.show()
🔹 Marker Fill Styles
Control how markers are filled
# Different fill styles
x = [1, 2, 3, 4]
y = [1, 1, 1, 1]
plt.figure(figsize=(10, 3))
# Full fill (default)
plt.plot(x[0], y[0], 'o', markersize=15,
fillstyle='full', label='full')
# No fill
plt.plot(x[1], y[1], 'o', markersize=15,
fillstyle='none', label='none')
# Left half
plt.plot(x[2], y[2], 'o', markersize=15,
fillstyle='left', label='left')
# Right half
plt.plot(x[3], y[3], 'o', markersize=15,
fillstyle='right', label='right')
plt.legend()
plt.title('Marker Fill Styles')
plt.ylim(0.5, 1.5)
plt.show()
🔹 Combining Markers with Lines
Create plots with both lines and markers
# Method 1: Format string
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y, 'o-') # Circles with solid line
plt.plot(x, y, 's--') # Squares with dashed line
plt.plot(x, y, '^:') # Triangles with dotted line
plt.title('Markers with Lines')
plt.show()
# Method 2: Separate parameters
plt.plot(x, y,
marker='o', # Marker type
linestyle='-', # Line style
color='blue', # Color
markersize=8, # Marker size
linewidth=2) # Line width
plt.title('Detailed Styling')
plt.show()
# Method 3: Only markers at specific points
import numpy as np
x_smooth = np.linspace(1, 5, 50)
y_smooth = 2 * x_smooth
plt.plot(x_smooth, y_smooth, '-', color='blue') # Smooth line
plt.plot(x, y, 'ro', markersize=8) # Data points
plt.title('Smooth Line with Data Points')
plt.show()