Master Matplotlib From Scratch

Learn Matplotlib fundamentals, figure anatomy, basic plotting, chart customization, subplots, statistical visualizations, color mapping, annotations, 3D plotting, animations, and interactive visualizations through structured practical lessons.

COURSE SYLLABUS
1. Introduction & Environment Setup
What is Matplotlib? Installation Environment Setup Imports and Conventions
2. Core Figure Anatomy & Structures
Figure vs Axes Creating Canvas Dimensions and Resolution Saving Visuals
3. Basic Plotting
Line Plots Scatter Plots Bar Charts Histograms
4. Basic Visual Formatting & Labels
Titles and Labels Text Customization Legends Grid Lines
5. Advanced Plot Customization
Format Strings Line and Marker Styling Color Systems Transparency
6. Axis Control, Ticks & Limits
Axis Limits Custom Ticks Tick Labels Axis Scaling
7. Multi-Plot Layouts & Subplots
Basic Subplots Indexing Subplots Layout Tuning Advanced Grids
8. Advanced Statistical & Data-Specific Plots
Pie Charts Box and Whisker Plots Violin Plots Error Bars
9. Color Mapping & 2D Data Visualizations
Colormaps Heatmaps and Images Colorbars Contour Plots
10. Annotations & Shapes
Basic Text Smart Annotations Drawing Shapes Shading Areas
11. Customizing Styles & Themes
Built-in Styles Custom Style Sheets Global Configurations
12. Object-Oriented API vs Pyplot
Pyplot vs Object-Oriented API Transitioning Methods Dual Axis Plots
13. Deep Integration with Pandas & Seaborn
Pandas Plotting Backend Seaborn Overlays Passing Axes
14. 3D Plotting
3D Setup 3D Lines and Scatter Surface and Wireframes Camera View
15. Dynamic & Live Plots
Interactive Mode Animation Module Real-time Updates Exporting Animations
16. Performance Optimization & Interactive Backends
Plotting Speed Interactive Backends Event Handling

Line and Marker Styling

Learn how to customize Matplotlib lines and markers using line width, colors, marker sizes, marker faces, marker edges, transparency, and different line styles.

1. Learning Objectives

After completing this lesson, you will be able to:

  • Understand line and marker styling in Matplotlib.
  • Change line colors and line thickness.
  • Customize solid, dashed, dotted, and dash-dot lines.
  • Change marker shape and marker size.
  • Customize marker face and edge colors.
  • Control marker edge width.
  • Apply transparency to plotted lines and markers.
  • Combine different styling properties in one chart.
  • Use styling properties with the object-oriented API.

2. Introduction to Line and Marker Styling

Line and marker styling is an important part of data visualization. Matplotlib allows you to change the appearance of lines and data point markers to make charts more attractive and easier to understand.

A line connects data points, while markers identify the individual positions of those data points.

X-axis Y-axis Connecting Line Marker

A line connects data points, while markers highlight individual observations.

Basic Syntax

plt.plot(
    x,
    y,
    color="blue",
    linewidth=2,
    marker="o",
    markersize=8
)

These properties allow you to control the appearance of the plotted line and its markers.

3. Changing Line Colors

The color parameter changes the color of a plotted line. You can use named colors, shorthand colors, hexadecimal color codes, and RGB or RGBA values.

Common Color Examples

Color Value Description
"red"Red color.
"blue"Blue color.
"green"Green color.
"orange"Orange color.
"purple"Purple color.
"#FF5733"Hexadecimal color.
(0.2, 0.5, 0.8)RGB color tuple.

Example: Changing Line Colors

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]

y1 = [10, 20, 30, 40, 50]
y2 = [15, 25, 35, 45, 55]
y3 = [20, 30, 40, 50, 60]

plt.plot(x, y1, color="red", label="Red Line")
plt.plot(x, y2, color="blue", label="Blue Line")
plt.plot(x, y3, color="green", label="Green Line")

plt.title("Line Color Customization")
plt.xlabel("X Values")
plt.ylabel("Y Values")

plt.legend()
plt.show()

Expected Output

Chart Title: Line Color Customization

Three Lines:
1. Red Line
2. Blue Line
3. Green Line

Legend:
Red Line
Blue Line
Green Line
Tip: Choose contrasting colors when displaying multiple data series in the same chart.

4. Changing Line Width

The linewidth parameter controls the thickness of a plotted line. A higher value creates a thicker line, while a lower value creates a thinner line.

Line Width Comparison

Width = 1 Width = 3 Width = 6 Width = 10

Increasing linewidth makes the plotted line thicker.

Example

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]

plt.plot(x, [10, 20, 30, 40, 50],
         linewidth=1,
         label="Thin Line")

plt.plot(x, [15, 25, 35, 45, 55],
         linewidth=3,
         label="Medium Line")

plt.plot(x, [20, 30, 40, 50, 60],
         linewidth=6,
         label="Thick Line")

plt.title("Line Width Comparison")
plt.legend()

plt.show()

Expected Output

Chart Title: Line Width Comparison

Three Lines:
Thin Line   : Width 1
Medium Line : Width 3
Thick Line  : Width 6

The third line appears thicker than the others.
Tip: Use linewidth or its shorter alias lw to control line thickness.

5. Customizing Line Styles

The linestyle parameter controls the pattern used to draw a line.

Value Line Style Example
"-" Solid Continuous line.
"--" Dashed Dashed pattern.
"-." Dash-dot Dash and dot pattern.
":" Dotted Dotted pattern.
"None" No line Only markers can be displayed.

Example: Different Line Styles

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]

plt.plot(x, [10, 20, 30, 40, 50],
         linestyle="-", label="Solid")

plt.plot(x, [15, 25, 35, 45, 55],
         linestyle="--", label="Dashed")

plt.plot(x, [20, 30, 40, 50, 60],
         linestyle="-.", label="Dash Dot")

plt.plot(x, [25, 35, 45, 55, 65],
         linestyle=":", label="Dotted")

plt.title("Line Style Comparison")
plt.legend()

plt.show()

Expected Output

Chart Title: Line Style Comparison

Four Lines:
1. Solid
2. Dashed
3. Dash Dot
4. Dotted

Each line uses a different line pattern.

6. Changing Marker Styles

Markers represent individual data points. The marker parameter specifies which symbol should be used for each point.

Marker Description
"o"Circle.
"s"Square.
"^"Triangle up.
"v"Triangle down.
"D"Diamond.
"*"Star.
"+"Plus.
"x"Cross.
"p"Pentagon.

Example: Marker Styles

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]

plt.plot(x, [10, 20, 30, 40, 50],
         marker="o", label="Circle")

plt.plot(x, [15, 25, 35, 45, 55],
         marker="s", label="Square")

plt.plot(x, [20, 30, 40, 50, 60],
         marker="^", label="Triangle")

plt.title("Marker Style Comparison")
plt.legend()

plt.show()

Expected Output

Chart Title: Marker Style Comparison

Data Series:
Circle markers
Square markers
Triangle markers

Each data series displays its own marker shape.

7. Changing Marker Size

The markersize parameter controls the size of markers displayed on a chart.

Larger markers make data points more visible, while smaller markers are useful when displaying many observations.

Marker Size Comparison

Size 4 Size 8 Size 14 Size 20 Small Medium Large Very Large

Larger markers make individual data points easier to identify.

Example

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]

plt.plot(x, [10, 20, 30, 40, 50],
         marker="o",
         markersize=4,
         label="Small")

plt.plot(x, [15, 25, 35, 45, 55],
         marker="o",
         markersize=10,
         label="Medium")

plt.plot(x, [20, 30, 40, 50, 60],
         marker="o",
         markersize=16,
         label="Large")

plt.title("Marker Size Comparison")
plt.legend()

plt.show()

Expected Output

Chart Title: Marker Size Comparison

Small Markers  : Size 4
Medium Markers : Size 10
Large Markers  : Size 16

The chart displays markers with three
different sizes.
Tip: Use markersize or its alias ms to change marker size.

8. Marker Face Color

The markerfacecolor parameter changes the inner fill color of a marker.

For example, you can create a blue circular marker with a yellow center.

Marker Face Color Diagram

Marker Edge Blue outline Marker Face Yellow fill

Marker face represents the inner fill, while marker edge represents the outer border.

Example

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 20, 30, 40, 50]

plt.plot(
    x,
    y,
    marker="o",
    markersize=12,
    markerfacecolor="yellow",
    color="blue"
)

plt.title("Marker Face Color")

plt.show()

Expected Output

Chart Title: Marker Face Color

Line Color:
Blue

Marker:
Circular

Marker Face Color:
Yellow

The chart displays yellow-filled circular markers
connected by a blue line.

9. Marker Edge Color and Width

Matplotlib provides separate parameters for customizing the outer border of a marker.

  • markeredgecolor changes the marker border color.
  • markeredgewidth changes the border thickness.

Example: Marker Edge Customization

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [15, 25, 35, 45, 55]

plt.plot(
    x,
    y,
    color="green",
    marker="o",
    markersize=12,
    markerfacecolor="yellow",
    markeredgecolor="red",
    markeredgewidth=3
)

plt.title("Marker Edge Customization")

plt.show()

Expected Output

Chart Title: Marker Edge Customization

Line Color:
Green

Marker Face:
Yellow

Marker Edge:
Red

Marker Edge Width:
3

The chart displays yellow circular markers
with a thick red border and a green line.
Tip: Use marker face and edge colors together to create clear and visually distinct data points.

10. Transparency in Lines and Markers

The alpha parameter controls the transparency of plotted objects. Its value ranges from 0 to 1.

Alpha Value Appearance
0Fully transparent.
0.25Mostly transparent.
0.550% opacity.
0.75Mostly opaque.
1Fully opaque.

Example

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]

plt.plot(
    x,
    [10, 20, 30, 40, 50],
    color="blue",
    linewidth=4,
    alpha=0.3,
    label="Transparent"
)

plt.plot(
    x,
    [15, 25, 35, 45, 55],
    color="red",
    linewidth=4,
    alpha=1,
    label="Opaque"
)

plt.title("Line Transparency")
plt.legend()

plt.show()

Expected Output

Chart Title: Line Transparency

First Line:
Blue with alpha 0.3

Second Line:
Red with alpha 1

The blue line appears transparent,
while the red line appears fully opaque.
Important: An alpha value of 0 makes a plotted object completely transparent. An alpha value of 1 makes it fully opaque.

11. Displaying Markers Without Connecting Lines

Sometimes you may want to display only markers without drawing connecting lines between data points.

You can use linestyle="None" to hide the connecting line while keeping the markers visible.

Example

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [10, 25, 15, 35, 30]

plt.plot(
    x,
    y,
    linestyle="None",
    marker="o",
    markersize=10,
    markerfacecolor="orange",
    markeredgecolor="black"
)

plt.title("Markers Without Connecting Lines")
plt.xlabel("X Values")
plt.ylabel("Y Values")

plt.show()

Expected Output

Chart Title: Markers Without Connecting Lines

Data:
Five individual points.

Marker:
Orange circular markers with black edges.

Connecting Line:
Not displayed.

12. Line and Marker Styling with Object-Oriented API

The object-oriented API provides the same styling options through the Axes object. You can use ax.plot() instead of plt.plot().

Example

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

x = [1, 2, 3, 4, 5]
y = [10, 20, 30, 40, 50]

ax.plot(
    x,
    y,
    color="purple",
    linewidth=3,
    linestyle="--",
    marker="o",
    markersize=10,
    markerfacecolor="yellow",
    markeredgecolor="black",
    markeredgewidth=2,
    label="Performance"
)

ax.set_title("Object-Oriented Styling")
ax.set_xlabel("X Values")
ax.set_ylabel("Y Values")

ax.legend()
ax.grid(True)

plt.show()

Expected Output

Chart Title: Object-Oriented Styling

Line:
Purple dashed line.

Line Width:
3

Marker:
Circular, size 10.

Marker Face:
Yellow.

Marker Edge:
Black, width 2.

Legend:
Performance
Tip: The object-oriented approach is useful when working with multiple subplots and complex figures.

13. Practical Activity

Task: Create a Monthly Sales Performance Chart

Create a line chart to compare the monthly sales of three products.

Given Data:

months = [1, 2, 3, 4, 5, 6]

laptop = [100, 120, 150, 170, 190, 220]
mobile = [80, 100, 130, 140, 180, 200]
tablet = [50, 70, 90, 110, 120, 150]

Requirements:

  • Create three line plots.
  • Use blue color for Laptop.
  • Use red color for Mobile.
  • Use green color for Tablet.
  • Set line width to 2.5.
  • Use circle markers for Laptop.
  • Use square markers for Mobile.
  • Use triangle markers for Tablet.
  • Set marker size to 8.
  • Customize marker face and edge colors.
  • Add a title, axis labels, and legend.
  • Enable grid lines.

14. Practice Questions

  1. What is line styling in Matplotlib?
  2. Which parameter changes the color of a line?
  3. What is the purpose of the linewidth parameter?
  4. Which parameter controls line patterns?
  5. What is the purpose of the marker parameter?
  6. How can you increase marker size?
  7. What is the difference between markerfacecolor and markeredgecolor?
  8. Which parameter changes marker border thickness?
  9. What does alpha control?
  10. How can you display markers without connecting lines?
  11. What is the difference between plt.plot() and ax.plot()?
  12. How can you combine multiple styling properties in one plot?

Summary

In this lesson, you learned how to customize line and marker appearance in Matplotlib. You explored line colors, line widths, line styles, marker shapes, marker sizes, marker faces, marker edges, and transparency.

  • color changes the line color.
  • linewidth controls line thickness.
  • linestyle controls the line pattern.
  • marker defines the marker shape.
  • markersize controls marker size.
  • markerfacecolor changes the marker fill color.
  • markeredgecolor changes the marker border color.
  • markeredgewidth controls marker border thickness.
  • alpha controls transparency.
  • linestyle="None" displays markers without connecting lines.
  • ax.plot() supports the same styling with the object-oriented API.

You are now ready to create customized Matplotlib charts with different line and marker styles.