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.
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
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
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.
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
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.
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 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.
-
markeredgecolorchanges the marker border color. -
markeredgewidthchanges 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.
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 |
|---|---|
0 | Fully transparent. |
0.25 | Mostly transparent. |
0.5 | 50% opacity. |
0.75 | Mostly opaque. |
1 | Fully 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.
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
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
- What is line styling in Matplotlib?
- Which parameter changes the color of a line?
- What is the purpose of the linewidth parameter?
- Which parameter controls line patterns?
- What is the purpose of the marker parameter?
- How can you increase marker size?
- What is the difference between markerfacecolor and markeredgecolor?
- Which parameter changes marker border thickness?
- What does alpha control?
- How can you display markers without connecting lines?
- What is the difference between plt.plot() and ax.plot()?
- 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.
colorchanges the line color.linewidthcontrols line thickness.linestylecontrols the line pattern.markerdefines the marker shape.markersizecontrols marker size.markerfacecolorchanges the marker fill color.markeredgecolorchanges the marker border color.markeredgewidthcontrols marker border thickness.alphacontrols 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.