Is it possible to control Matplotlib marker orientation?

To control matplotlib marker orientation, we can use marker tuples that contain the number of sides, style, and rotation angle of the marker. This technique is particularly useful for creating custom arrow-like markers or rotating polygonal shapes.

Understanding Marker Tuples

A marker tuple follows the format (numsides, style, angle) where ?

  • numsides ? Number of sides for the polygon marker

  • style ? Style of the marker (0 for regular polygon)

  • angle ? Rotation angle in degrees

Basic Example with Fixed Rotation

Let's start with a simple example showing triangular markers with different orientations ?

import numpy as np
import matplotlib.pyplot as plt

# Create sample data points
x = [1, 2, 3, 4]
y = [1, 2, 1, 2]

# Plot with different rotations
plt.figure(figsize=(8, 4))

# Triangle markers with different orientations
plt.plot(x[0], y[0], marker=(3, 0, 0), markersize=15, color='red', linestyle='None', label='0°')
plt.plot(x[1], y[1], marker=(3, 0, 45), markersize=15, color='blue', linestyle='None', label='45°')
plt.plot(x[2], y[2], marker=(3, 0, 90), markersize=15, color='green', linestyle='None', label='90°')
plt.plot(x[3], y[3], marker=(3, 0, 135), markersize=15, color='orange', linestyle='None', label='135°')

plt.legend()
plt.title('Triangle Markers with Different Orientations')
plt.grid(True, alpha=0.3)
plt.show()

Dynamic Rotation Example

Here's an example that creates markers with continuously varying rotations ?

import numpy as np
import matplotlib.pyplot as plt

plt.figure(figsize=(8, 6))

# Create random data points
x = np.random.rand(10)
y = np.random.rand(10)

# Create rotation angles from 0 to 360 degrees
angles = np.linspace(0, 360, 10)

# Plot each point with its corresponding rotation
for xi, yi, angle in zip(x, y, angles):
    plt.plot(xi, yi, marker=(3, 0, angle), linestyle='None', markersize=20, color='red')

plt.title('Triangular Markers with Progressive Rotation')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.grid(True, alpha=0.3)
plt.show()

Different Polygon Shapes

You can create various polygon shapes by changing the number of sides ?

import numpy as np
import matplotlib.pyplot as plt

plt.figure(figsize=(10, 6))

# Create data points
shapes = [3, 4, 5, 6, 8]  # Triangle, square, pentagon, hexagon, octagon
x_pos = [1, 2, 3, 4, 5]
y_pos = [1, 1, 1, 1, 1]

# Plot different polygon shapes with rotation
for i, (sides, x, y) in enumerate(zip(shapes, x_pos, y_pos)):
    plt.plot(x, y, marker=(sides, 0, 45), markersize=20, 
             linestyle='None', label=f'{sides} sides')

plt.legend()
plt.title('Different Polygon Markers with 45° Rotation')
plt.xlim(0.5, 5.5)
plt.ylim(0.5, 1.5)
plt.grid(True, alpha=0.3)
plt.show()

Key Parameters

Parameter Description Example Values
numsides Number of polygon sides 3 (triangle), 4 (square), 5 (pentagon)
style Marker style 0 (regular), 1 (star-like), 2 (asterisk)
angle Rotation in degrees 0-360

Conclusion

Matplotlib marker tuples provide precise control over marker orientation using the format (numsides, style, angle). This technique is ideal for creating directional indicators, custom symbols, or visually appealing plots with rotated polygonal markers.

Updated on: 2026-03-25T21:11:34+05:30

666 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements