Home » Matplotlib Markers Tutorial

Matplotlib Markers Tutorial

Java SE 11 Programmer II [1Z0-816] Practice Tests
Java SE 11 Programmer I [1Z0-815] Practice Tests
Spring Framework Basics Video Course
1 Year Subscription
Java SE 11 Developer (Upgrade) [1Z0-817]
Oracle Java Certification

Matplotlib provides a wide range of markers to represent data points in plots, making it easy to customize the appearance of scatter plots, line plots, and other chart types.

Markers can help highlight specific data points and enhance the readability of a chart.

In this tutorial, we’ll cover various markers in Matplotlib, how to customize them, and examples for using them in different types of plots.

1. Importing Matplotlib

Before we begin, ensure that you have Matplotlib installed. If not, install it using:

pip install matplotlib

Then, import it into your Python script.

import matplotlib.pyplot as plt
import numpy as np

2. Basic Scatter Plot with Markers

Markers are specified using the marker parameter in plt.plot() or plt.scatter().

# Sample data
x = np.array([1, 2, 3, 4, 5])
y = np.array([10, 20, 25, 30, 35])

# Basic scatter plot with circular markers
plt.plot(x, y, marker='o')
plt.title("Scatter Plot with Circular Markers")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

In this example, the ‘o' marker indicates circular markers. We’ll explore more marker options below.

3. Common Marker Styles

Matplotlib offers a variety of marker styles to use in your plots. Below is a table listing some common markers and their corresponding symbols.

Marker Symbol Description
‘.' Point
‘,' Pixel
‘o' Circle
‘v' Triangle Down
‘^' Triangle Up
‘<‘ Triangle Left
‘>' Triangle Right
‘s' Square
‘p' Pentagon
‘*' Star
‘h' Hexagon1
‘H' Hexagon2
‘+' Plus
‘x' X
‘D' Diamond
‘d' Thin Diamond
`' ‘`
‘_' Horizontal Line

Let’s look at some examples.

4. Plotting with Different Markers

Example: Using Various Marker Styles in a Single Plot

# Sample data
x = np.arange(5)
y = np.array([5, 15, 10, 20, 25])

# Using different markers
plt.plot(x, y, marker='o', label='Circle')
plt.plot(x, y + 5, marker='s', label='Square')
plt.plot(x, y + 10, marker='^', label='Triangle Up')
plt.plot(x, y + 15, marker='*', label='Star')
plt.plot(x, y + 20, marker='D', label='Diamond')

# Adding labels and legend
plt.title("Plot with Different Marker Styles")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.legend()
plt.show()

In this example, we plotted several lines using different markers.

5. Customizing Marker Size and Color

You can adjust the size and color of markers using the markersize (or ms) and markerfacecolor (or mfc) parameters.

# Sample data
x = np.array([1, 2, 3, 4, 5])
y = np.array([5, 10, 15, 20, 25])

# Customizing marker size and color
plt.plot(x, y, marker='o', markersize=10, markerfacecolor='red', markeredgecolor='black', markeredgewidth=2)
plt.title("Customized Marker Size and Color")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

Here:

  • markersize=10 controls the size of the marker.
  • markerfacecolor='red' sets the fill color.
  • markeredgecolor='black' and markeredgewidth=2 customize the marker’s outline.

6. Scatter Plots with Markers

In scatter plots, each data point is marked individually. The plt.scatter() function provides additional options for customizing markers, such as varying marker size and color based on data.

# Sample data
x = np.array([5, 10, 15, 20, 25])
y = np.array([10, 15, 20, 25, 30])
sizes = np.array([100, 200, 300, 400, 500])  # Marker sizes
colors = np.array([10, 20, 30, 40, 50])      # Marker colors

# Scatter plot with varying marker size and color
plt.scatter(x, y, s=sizes, c=colors, cmap='viridis', alpha=0.7)
plt.colorbar(label="Color Intensity")
plt.title("Scatter Plot with Varying Marker Size and Color")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

In this example:

  • s=sizes specifies the size of each marker.
  • c=colors specifies the color of each marker, with cmap='viridis' controlling the colormap.
  • alpha=0.7 sets the transparency of markers.

7. Line Plot with Markers Only

If you want to show only markers without connecting lines, set the linestyle parameter to an empty string.

# Sample data
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 6, 8, 10])

# Line plot with markers only
plt.plot(x, y, marker='s', linestyle='', markersize=10, markerfacecolor='blue', markeredgecolor='darkblue')
plt.title("Markers Only (No Lines)")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

8. Using Custom Markers

Matplotlib also allows custom marker shapes by passing a single-character string or a custom path.

Example: Using Custom Shapes for Markers

# Sample data
x = np.array([1, 2, 3, 4, 5])
y = np.array([1, 4, 9, 16, 25])

# Using a custom marker shape (filled plus sign)
plt.plot(x, y, marker=(5, 1), markersize=15, linestyle='', color='purple')
plt.title("Custom Marker Shape")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

In this example, (5, 1) creates a filled plus sign marker with five points.

9. Using Marker Styles from the MarkerStyle Class

The matplotlib.markers.MarkerStyle class provides access to more marker properties.

from matplotlib.markers import MarkerStyle

# Sample data
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 3, 5, 7, 11])

# Using MarkerStyle to create markers with different properties
plt.plot(x, y, marker=MarkerStyle('p'), markersize=12, color='orange', linestyle='-', label='Pentagon Marker')
plt.plot(x, y - 2, marker=MarkerStyle('H'), markersize=12, color='green', linestyle='-', label='Hexagon Marker')

plt.title("Using MarkerStyle Class")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.legend()
plt.show()

In this example:

  • MarkerStyle(‘p') and MarkerStyle(‘H') set the marker to a pentagon and hexagon, respectively.

10. Complete Example: Customizing Markers in a Line Plot

Let’s create a more complex plot where we use different markers, sizes, colors, and styles for each data series.

# Sample data
x = np.linspace(0, 10, 10)
y1 = np.sin(x)
y2 = np.cos(x)

# Plot with different markers and customization
plt.plot(x, y1, marker='o', linestyle='-', color='blue', markersize=8, label='Sine Wave')
plt.plot(x, y2, marker='^', linestyle='--', color='green', markersize=10, label='Cosine Wave')

# Customize plot
plt.title("Line Plot with Custom Markers")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.legend()
plt.grid(True)
plt.show()

In this example:

  • marker='o' and marker='^' set circular and triangular markers for y1 and y2.
  • linestyle='-‘ and linestyle='–‘ specify solid and dashed lines.
  • markersize controls the size of the markers.

Summary of Marker Customization Options

Parameter Description
marker Specifies the marker style (e.g., ‘o', ‘s', ‘^')
markersize (or ms) Controls the size of the markers
markerfacecolor (or mfc) Sets the fill color of the markers
markeredgecolor (or mec) Sets the edge color of the markers
markeredgewidth (or mew) Sets the width of the marker edge
linestyle Sets the line style (e.g., ‘-‘, ‘–‘, or empty string for no line)

Matplotlib offers flexibility with markers, from setting styles and colors to customizing sizes and edges.

This tutorial should help you start using markers confidently in your data visualizations

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More