Matplotlib Line Styles
Customize your plot lines with different styles, colors, and widths
📈 Styling Your Lines
Line styles help you differentiate between multiple data series and make your plots more visually appealing and informative.
import matplotlib.pyplot as plt
# Basic line with custom style
x = [1, 2, 3, 4]
y = [1, 4, 2, 3]
plt.plot(x, y, linestyle='--', color='red', linewidth=2)
plt.show()
Line Style Options
Matplotlib offers various line styles to make your plots distinctive:
Solid Line
Default continuous line style
Dashed Line
Broken line with equal dashes
Dotted Line
Line made of small dots
Dash-Dot Line
Alternating dashes and dots
🔹 Basic Line Styles
Create different line styles using simple parameters
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y1 = [1, 4, 2, 3, 5]
y2 = [2, 3, 1, 4, 2]
y3 = [3, 1, 4, 2, 1]
y4 = [1, 2, 5, 3, 4]
# Different line styles
plt.plot(x, y1, '-', label='Solid')
plt.plot(x, y2, '--', label='Dashed')
plt.plot(x, y3, ':', label='Dotted')
plt.plot(x, y4, '-.', label='Dash-dot')
plt.legend()
plt.title('Different Line Styles')
plt.show()
🔹 Line Colors
Customize line colors using various methods
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 2, 3]
# Different ways to specify colors
plt.figure(figsize=(10, 6))
# Using color names
plt.subplot(2, 2, 1)
plt.plot(x, y, color='red')
plt.title('Named Color')
# Using single letters
plt.subplot(2, 2, 2)
plt.plot(x, y, color='b') # blue
plt.title('Letter Code')
# Using hex codes
plt.subplot(2, 2, 3)
plt.plot(x, y, color='#FF5733')
plt.title('Hex Color')
# Using RGB tuples
plt.subplot(2, 2, 4)
plt.plot(x, y, color=(0.2, 0.8, 0.4))
plt.title('RGB Tuple')
plt.tight_layout()
plt.show()
🔹 Line Width
Control the thickness of your lines
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 4, 2, 3, 5]
# Different line widths
plt.plot(x, y, linewidth=0.5, label='Thin (0.5)')
plt.plot(x, [i+1 for i in y], linewidth=2, label='Normal (2)')
plt.plot(x, [i+2 for i in y], linewidth=4, label='Thick (4)')
plt.plot(x, [i+3 for i in y], linewidth=6, label='Very Thick (6)')
plt.legend()
plt.title('Different Line Widths')
plt.show()
🔹 Combining Styles
Mix line styles, colors, and widths for custom looks
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
sales_2022 = [100, 150, 120, 180, 200]
sales_2023 = [120, 160, 140, 190, 220]
# Combine multiple style properties
plt.plot(x, sales_2022,
linestyle='--',
color='blue',
linewidth=2,
label='2022 Sales')
plt.plot(x, sales_2023,
linestyle='-',
color='red',
linewidth=3,
label='2023 Sales')
plt.xlabel('Month')
plt.ylabel('Sales ($)')
plt.title('Sales Comparison')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
🔹 Shorthand Format Strings
Use format strings for quick styling
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y1 = [1, 4, 2, 3]
y2 = [2, 3, 1, 4]
y3 = [3, 1, 4, 2]
# Format string: [color][marker][line]
plt.plot(x, y1, 'r-', label='Red solid') # red solid line
plt.plot(x, y2, 'g--', label='Green dashed') # green dashed
plt.plot(x, y3, 'b:', label='Blue dotted') # blue dotted
plt.legend()
plt.title('Format String Examples')
plt.show()
# More examples
plt.figure()
plt.plot(x, y1, 'ro-', label='Red circles with line')
plt.plot(x, y2, 'bs--', label='Blue squares dashed')
plt.plot(x, y3, 'g^:', label='Green triangles dotted')
plt.legend()
plt.title('Markers with Lines')
plt.show()