0

I have a barplot of occurrence statistics for small objects.
These objects can be represented as images let's say 64x64.

I am trying to replace the seaborn/matplotlib barplot xtick labels identifier/index 1, 2, 3, with the actual image object corresponding, so that at position:

  • 1 -> im[1]
  • 2 -> im[2]
  • etc.

where im[i] is that image that corresponds to identifier/index i

It's a very common use-case for which I couldn't find a solution and thus I decided to open it here.

ysig
  • 447
  • 4
  • 18

1 Answers1

1

You can use annotation boxes with offset images.
In the following example I use these two images: enter image description here and enter image description here:

import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox

img = [plt.imread("1.png"), plt.imread("2.png")]

plt.bar([0,1], [1,2])
plt.xticks([0,1], ['', ''])

ax = plt.gca()
tick_labels = ax.xaxis.get_ticklabels()

for i,im in enumerate(img):
    ib = OffsetImage(im, zoom=.4)
    ib.image.axes = ax
    ab = AnnotationBbox(ib,
                    tick_labels[i].get_position(),
                    frameon=False,
                    box_alignment=(0.5, 1.2)
                    )
    ax.add_artist(ab)

enter image description here

Stef
  • 28,728
  • 2
  • 24
  • 52