How can I make the xtick labels of a plot be simple drawings using Matplotlib?

Creating custom xtick labels with simple drawings in Matplotlib allows you to replace standard text labels with visual elements like circles and rectangles. This technique uses patches to create geometric shapes positioned at specific tick locations.

Basic Setup

First, we need to import the required modules and set up the figure parameters ?

import matplotlib.pyplot as plt
import matplotlib.patches as patches

# Set figure size and layout
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create a simple plot
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10))

plt.show()

Adding Custom Drawing Labels

To replace xtick labels with drawings, we set custom tick positions, remove text labels, and add patches at specific coordinates ?

import matplotlib.pyplot as plt
import matplotlib.patches as patches

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Y position for the drawings (below x-axis)
y = -0.75

# Create figure and plot
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10))

# Set custom tick positions and remove text labels
ax.get_xaxis().set_ticks([2, 4, 6, 8])
ax.get_xaxis().set_ticklabels([])

# Add circle patches as custom labels
ax.add_patch(patches.Circle((2, y), radius=0.2,
                           clip_on=False, facecolor='red'))
ax.add_patch(patches.Circle((4, y), radius=0.2,
                           clip_on=False, facecolor='yellow'))

# Add rectangle patches as custom labels
ax.add_patch(patches.Rectangle((6 - 0.1, y - 0.05), 0.2, 0.2,
                              clip_on=False, facecolor='blue'))
ax.add_patch(patches.Rectangle((8 - 0.1, y - 0.05), 0.2, 0.2,
                              clip_on=False, facecolor='green'))

plt.show()

Understanding the Parameters

Key parameters for creating custom drawing labels:

  • clip_on=False - Allows shapes to be drawn outside the plot area
  • radius - Controls the size of circles
  • facecolor - Sets the fill color of the shapes
  • Position coordinates - Align shapes with tick positions

Advanced Example with More Shapes

You can create more complex custom labels using different shapes and colors ?

import matplotlib.pyplot as plt
import matplotlib.patches as patches

plt.rcParams["figure.figsize"] = [8.00, 4.00]
plt.rcParams["figure.autolayout"] = True

y = -1.0

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1, 2, 3, 2, 4, 3, 2, 4, 5])

# Set tick positions
ax.get_xaxis().set_ticks([1, 3, 5, 7])
ax.get_xaxis().set_ticklabels([])

# Add various shapes as labels
# Triangle (using Polygon)
triangle = patches.Polygon([(1, y-0.1), (0.8, y+0.1), (1.2, y+0.1)], 
                          closed=True, facecolor='purple', clip_on=False)
ax.add_patch(triangle)

# Circle
ax.add_patch(patches.Circle((3, y), radius=0.15,
                           clip_on=False, facecolor='orange'))

# Diamond (rotated square)
diamond = patches.Rectangle((5-0.1, y-0.1), 0.2, 0.2,
                           angle=45, clip_on=False, facecolor='cyan')
ax.add_patch(diamond)

# Star shape (using Polygon)
import numpy as np
angles = np.linspace(0, 2*np.pi, 11)
star_x = 0.1 * np.cos(angles) + 7
star_y = 0.1 * np.sin(angles) + y
star = patches.Polygon(list(zip(star_x[::2], star_y[::2])), 
                      closed=True, facecolor='gold', clip_on=False)
ax.add_patch(star)

plt.show()

Key Points

  • Use set_ticks() to define custom tick positions
  • Set empty tick labels with set_ticklabels([])
  • Position shapes using plot coordinates
  • Set clip_on=False to draw outside the plot area
  • Experiment with different patches classes for various shapes

Conclusion

Custom drawing labels in Matplotlib provide a creative way to enhance plot readability and visual appeal. Use patches to create geometric shapes positioned at tick locations, replacing traditional text labels with meaningful visual elements.

Updated on: 2026-03-25T22:31:05+05:30

212 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements